0

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>

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

for title bar, I think 35dip is a requirement.
Are you trying to fit an image and a button within 35 dip, with an image bigger than 35dip? That's my point.
0

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.

Comments

0

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>

`

1 Comment

He seems to want the ImageView to be in the center. I think it's better to have android:layout_centerInParent="true" for the header ImageView, instead of using android:layout_gravity. But overall, RelativeLayout is more appropriate for this it seems. Will need some dummy views thrown in if he insist on a LinearLayout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.