0

I have a simple linear layout with 2 buttons in it.
I set the layout_width="fill_parent"

But i want the buttons to occupy 70% of screen size and at the same time i want them to be centred.
How do i achieve this.?

This is how it looks now.

enter image description here

i want something like this.

enter image description here

Thank You.

EDIT : The XML code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"        
    android:layout_height="wrap_content"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>
Pradeep
  • 1,193
  • 6
  • 27
  • 44

5 Answers5

5

Try this:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="1" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:text="button 1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="1" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:text="button 1" />
    </LinearLayout>
</LinearLayout>
Sonali8890
  • 2,005
  • 12
  • 16
  • This code works but you changed the **orientation** property. Is it possible without doing that ..?? – Pradeep Feb 17 '14 at 12:54
  • No it won't work in case of vertical orientation. If you want layout like image you shown above then u have to use nested layout – Sonali8890 Feb 17 '14 at 12:59
4
  • Firstly get screen size in the following way and dynamically set the width of the button as 0.7*screenWidth
 DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
1

Try below code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="1" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:text="button 1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:weightSum="1" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.7"
            android:text="button 2" />
    </LinearLayout>

</LinearLayout>

Hope this helps!!!

Sanket
  • 3,094
  • 1
  • 16
  • 19
1

Try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:weightSum="100" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="70"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

</LinearLayout>

this layout file gives you what you want.

Rahul
  • 41
  • 1
  • 1
  • 5
0

Assign android:weightSum="1" to the parent layout and android:layout_weight="0.7" to the Buttons like this:

 <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:weightSum="1" >
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:text="BUTTON 1" />
</LinearLayout>
 <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:weightSum="1" >
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:text="BUTTON 1" />
</LinearLayout>

or you can assign Margins for the root layout like this

android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"

But this will not be exactly 70% of the screen. Let me know if it works for you.

SMR
  • 6,628
  • 2
  • 35
  • 56