1

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>
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
MyNewName
  • 1,035
  • 2
  • 18
  • 34

3 Answers3

4

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)

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
2

use match_parent in Button width

android:layout_width="match_parent"

Before using match_parent, make sure parent layout must consist full width of screen, that means parent layout must have 'match_parent' for it's width

Ravi
  • 34,851
  • 21
  • 122
  • 183
2

Just use match_parent or fill_parent

android:layout_width="match_parent"

OR

android:layout_width="fill_parent"
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96