My layout stopped working

1

I'm setting up a screen for an android application, I made the screen layout and it was working, however after updating the android studio my layout does not work anymore.

The code for this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".actyvities.MainActivity">

<include
    android:id="@+id/tool_bar"
    layout="@layout/tool_bar"
    ></include>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/edit_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="Nova nota..."/>
    <Button
        android:id="@+id/insert_button"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text="Inserir" />

</LinearLayout>-->

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>
</LinearLayout>

Before the screen looked like this:

Butnowitlookslikethis:

I do not understand much of HTML, I tried to arrange the layout, but I only managed to put fixed values in the size of the text box, I did not want to do that because if you change the screen size, the layout does not look good.     

asked by anonymous 04.05.2016 / 01:07

1 answer

2

So the Android layout is not HTML. It's XML, which is a cousin of HTML.

Joking aside, the problem here is that you are setting the width of EditText to match_parent , which causes it to overlap Button .

Correct in this case is to use weights ( layout_weight="1" ) in combination with dynamic width ( layout_width="0dp" ), making EditText occupy the maximum available width by discounting the width of the button. This is a very common Android trick.

The important part of your layout would look like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/edit_box"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text"
        android:hint="Nova nota..."/>

    <Button
        android:id="@+id/insert_button"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text="Inserir" />
</LinearLayout>
    
04.05.2016 / 01:48