Button does not appear in LinearLayout

I am trying to add a Button to LinearLayout after a TextView , but it is not displayed.

Here is my layout code

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="45dip"> <TextView android:layout_width="fill_parent" android:layout_height="45dip" android:paddingLeft="5dip" android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip" android:gravity="center_vertical" android:id="@+id/tvChild" android:text="Children" android:textColor="#ffCCCC22" /> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Submit" /> </LinearLayout> 

TextView displays correctly with the correct text, but instead of Button I get a large empty space of three to four lines in length.

What am I missing?

+6
source share
2 answers

Try it.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="45dip" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="wrap_content" android:layout_height="45dip" android:paddingLeft="5dip" android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip" android:gravity="center_vertical" android:id="@+id/tvChild" android:text="Children" android:textColor="#ffCCCC22" /> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Submit" /> </LinearLayout> 

Use android:layout_width="wrap_content" instead of android:layout_width="fill_parent" for TextView and Button

+6
source

The problem was that you set android:layout_width="fill_parent" for the TextView , so it took a full screen width to display the TextView . And he could not display Button .

Here are two options for you:

  • Adding TextView and Button on the same line.

    Change the layout_width attribute in the TextView to wrap_content .

  • Adding TextView and Button vertically.

    Change the orientation attribute of LinearLayout to vertical .

+4
source

Source: https://habr.com/ru/post/894262/


All Articles