Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout - Two views next to each other, equal width, use full screen-width

I have a problem with a layout in android. I want that two views should have the equal width, and they should almost use the full width of the screen. Each view should contain a centered label.

It should look like this when it's done:

Example

Here is what I have so far:

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

        <View
            android:id="@+id/view1"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <View
            android:id="@+id/view2"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp" />

    </RelativeLayout>

I just have placeholder values for the width right now.

Thanks.

like image 422
Mike_NotGuilty Avatar asked Dec 19 '22 09:12

Mike_NotGuilty


1 Answers

You have to use android:layout_weight attribute in xml so try below code hope it will resolve your problem :-

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

     <Button
        android:text="Register"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_margin="5dp"
        android:layout_weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_margin="5dp"
        android:layout_weight="1" />

  </LinearLayout>

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1"

For more info you need to read Linear layout.

like image 170
duggu Avatar answered Dec 28 '22 06:12

duggu