Paresh Mayani
Paresh Mayani

Reputation: 128458

android - problem in Layout

I am trying to prepare a layout which is same as the below attached image.

My problems are indicated in image:

  1. I try to set-up buttons exactly same(width) as indicated as 1 in image
  2. I try to set "save" and "Cancel" buttons at below with equal width.

enter image description here

I have tried it with the below XML Layout:

<RelativeLayout
    android:id="@+id/widget38"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dp">

    <TextView 
        android:layout_width="wrap_content" 
        android:id="@+id/textView1" 
        android:text="Event" 
        android:layout_alignParentLeft="true" 
        android:layout_height="wrap_content">
    </TextView>

    <EditText 
        android:layout_width="fill_parent" 
        android:id="@+id/editText1" 
        android:layout_alignParentLeft="true" 
        android:hint="Tap to enter title"
        android:layout_marginTop="5dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1">
    </EditText>

    <View 
        android:id="@+id/separator"         
        android:background="#ffffff"        
        android:layout_width = "fill_parent"
        android:layout_height="1dip"
        android:layout_centerVertical ="true"
        android:layout_below="@+id/editText1"/>

    <TextView 
        android:layout_width="wrap_content" 
        android:id="@+id/textView2" 
        android:text="From" 
        android:layout_alignParentLeft="true" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/separator"
        android:layout_marginTop="5dp">
    </TextView>

    <Button 
        android:layout_width="wrap_content" 
        android:id="@+id/button1" 
        android:layout_alignParentLeft="true" 
        android:text="Button"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2">
    </Button>

    <Button 
        android:layout_width="wrap_content" 
        android:id="@+id/button2" 
        android:text="Button"
        android:layout_marginLeft="10dp"
        android:layout_alignTop="@+id/button1" 
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button1"
        android:layout_below="@+id/button1">
    </Button>

    <View 
        android:id="@+id/separator1"        
        android:background="#ffffff"        
        android:layout_width = "fill_parent"
        android:layout_height="1dip"
        android:layout_centerVertical ="true"
        android:layout_below="@+id/button2"/>

    <TextView 
        android:layout_width="wrap_content" 
        android:id="@+id/textView3" 
        android:text="To" 
        android:layout_alignParentLeft="true" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/separator1"
        android:layout_marginTop="5dp">
    </TextView>

    <Button 
        android:layout_width="wrap_content" 
        android:id="@+id/button3" 
        android:layout_alignParentLeft="true" 
        android:text="Button"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3">
    </Button>

    <Button 
        android:layout_width="wrap_content" 
        android:id="@+id/button4" 
        android:text="Button"
        android:layout_marginLeft="10dp"
        android:layout_alignTop="@+id/button3" 
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button3"
        android:layout_below="@+id/button3">
    </Button>

    <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/relativeLayout1" 
        android:layout_alignParentBottom="true">

        <Button android:layout_width="wrap_content" android:id="@+id/button5" android:layout_alignParentBottom="true" android:text="Save" android:layout_height="wrap_content"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button6" android:layout_alignParentBottom="true" android:text="Cancel" android:layout_height="wrap_content" android:layout_toRightOf="@+id/button5"></Button>
    </RelativeLayout>


</RelativeLayout>

Upvotes: 1

Views: 569

Answers (5)

In the Activity's onCreate() method after setting the content view do the following.

findViewById(R.id.button1).setWidth(findViewById(R.id.button5).getWidth());
findViewById(R.id.button2).setWidth(findViewById(R.id.button6).getWidth());

and let me know if this works.

Upvotes: 0

Nanis
Nanis

Reputation: 361

I think your problem is that you haven't the same with your xml code ?

You must use the android:layout_weight

if you want the button take the same place (cancel and valid button on your picture), you must put android:layout_weight="1" for both for example

Upvotes: 0

Franco
Franco

Reputation: 7475

for buttons marked with 'image 1' you can set a fixed width like '16dip' i.e.

<Button 
        android:layout_width="16dip" 
        android:id="@+id/button1" 
        android:layout_alignParentLeft="true" 
        android:text="Button"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2">
    </Button>

for buttons below you can add into an LinearLayout horizontally oriented, with 0 width and a weight value

    <Button 
            android:layout_width="0dip" 
            android:layout_weight="1" 
            android:id="@+id/button4" 
            android:text="Button"
            android:layout_marginLeft="10dp"
            android:layout_alignTop="@+id/button3" 
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/button3"
            android:layout_below="@+id/button3">
        </Button>

Upvotes: 1

Phonon
Phonon

Reputation: 12737

You can adjust the weight of a button by using android:layout_weight

Here's an example of how to use it: http://android.amberfog.com/?p=328

Upvotes: 1

Dmitry Ryadnenko
Dmitry Ryadnenko

Reputation: 22512

You can just use horizontal linear layout. For the first issue you should set "weight" values for you buttons in .xml layout file

Upvotes: 0

Related Questions