MyNewName
MyNewName

Reputation: 1055

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>

Upvotes: 1

Views: 860

Answers (3)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17142

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)

Upvotes: 4

Ravi
Ravi

Reputation: 35589

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

Upvotes: 2

Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

Just use match_parent or fill_parent

android:layout_width="match_parent"

OR

android:layout_width="fill_parent"

Upvotes: 2

Related Questions