ahsan
ahsan

Reputation: 677

problem with android layout

I have the following layout for a custom title bar. However, the problem is this: both the imageview and the imagebutton are coming at the centre. I was expecting the imagebutton to be at the extreme right. Can anyone kindly let me know what I did wrong here ? Thanks.

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="35dip"
                android:gravity="center_vertical"
                android:paddingLeft="5dip"
                android:background="#323331">

                <ImageView 
                android:src="@drawable/header" 
                android:id="@+id/header" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"
                android:gravity="center_horizontal">
                </ImageView>


                <ImageButton
                android:id="@+id/saveButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/savetap"
                android:background="@null"
                android:gravity="right"
                android:layout_above="@+id/header">
                </ImageButton>

            </LinearLayout>

Upvotes: 0

Views: 105

Answers (3)

Y.A.P.
Y.A.P.

Reputation: 538

At first if width is set to fill parent it really fills parent so it use all the width. Second: why not RelativeLayout? It provides more options and control. Maybe this is what you looking for: `

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:gravity="center_vertical"
        android:paddingLeft="5dip"
        android:background="#323331">

        <ImageView 
            android:src="@drawable/header" 
            android:id="@+id/header" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content"
            android:layout_gravity="left">
        </ImageView>


        <ImageButton
            android:id="@+id/saveButton"
            android:layout_height="wrap_content"
            android:src="@drawable/savetap"
            android:background="@null" 
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content" android:layout_alignParentRight="true">
        </ImageButton>

    </RelativeLayout>

`

Upvotes: 0

Dan S
Dan S

Reputation: 9189

You're using a vertically oriented LinearLayout, all View will be presented in a vertical fashion. Use a RelativeLayout so that you'll have more control over where Views are positioned. If you'd still like to use a LinearLayout, you'll have to use horizontal orientation so that the Views can be on the same "line".

Use layout_gravity to layout the item to the right, just gravity is used by the contents of the view.

Upvotes: 0

K-ballo
K-ballo

Reputation: 81349

Your layout is only 35dip tall, so pressumibly if you show your ImageView the ImageButton gets positioned outside the screen. Consider changing your layout_height to wrap_content, if appropiate.

Upvotes: 1

Related Questions