MyNewName MyNewName - 1 month ago 6
Android Question

Android button orientation vertical width 100%

Yesterday I started with android programming. Now I want to create a simple layout. Here is an image:

image

How can I give the buttons a width of 100%? When I write

android:layout_width="0dp"


will the button no longer displayed. What is my mistake?

Here is the code again.

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />

</LinearLayout>

Answer

Set the buttons' layout_width property to match_parent:

android:layout_width="match_parent"

PS: fill_parent & match_parent are the same , but the fill_parent constant is deprecated starting from API Level 8 and is now replaced by match_parent.

(Both constants resolve to -1 eventually)