Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set width of buttons in android to cover screen width?

I have following three button in a Linear Layout with width fill_parent.

How can I set the width of these buttons to equally cover the whole screen area?

<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnReplyMessage" 
   android:gravity="left"
   android:text="Reply" 
/>
    
<Button 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:id="@+id/btnMarkAsUnread"
   android:gravity="left" 
   android:text="Mark as unread" 
/>
            
<ImageButton 
   android:id="@+id/btnDeleteMessage"
   android:src="@drawable/imgsearch"
   android:gravity="right" 
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_alignParentRight="true"
 />
like image 757
UMAR-MOBITSOLUTIONS Avatar asked Mar 01 '10 09:03

UMAR-MOBITSOLUTIONS


People also ask

What should be size of button in Android?

Consider making touch targets at least 48 x 48 dp, separated by 8 dp of space or more, to ensure balanced information density and usability. A touch target of 48 x 48 dp results in a physical size of about 9 mm, regardless of screen size.

How can I get mobile screen width and height in android?

Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.


2 Answers

Give all buttons the following properties

android:layout_width="fill_parent"
android:layout_weight="1"

fill_parent tells them to consume as much width as possible, and weight determines how that width shall be distributed, when more than one control are competing for the same space. (Try playing around with different values of weight for each button to see how that works)

like image 78
David Hedlund Avatar answered Oct 03 '22 16:10

David Hedlund


You should just specify these attributes for each button:

android:layout_width="fill_parent"
android:layout_weight="1"

So it should be something like that:

<LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>
like image 32
Fedor Avatar answered Oct 03 '22 17:10

Fedor