0

I am trying to add title + button in titlebar in Android. But somehow i am not happy with the kind of result i am getting. May be something wrong with code. Has anyone tried this earlier ? My .xml is below

code.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:background="#484848"
            android:orientation="horizontal">

        <TextView
            android:id="@+id/windowtitle"
            android:layout_width="match_parent"
            android:layout_marginTop="2dip"
            android:layout_height="wrap_content"
            android:text="Support"
            android:gravity="center_horizontal"
            android:textSize="30sp" ></TextView>

        <Button
            android:id="@+id/syncbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="3dip"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:gravity="left"
            android:text="Sync" />

       </RelativeLayout>


    </RelativeLayout>
2
  • How exactly you want your titlebar to look like.Please add image Commented Oct 31, 2013 at 7:11
  • @Manishika - It is as shown in picture just that i am not able to do proper alignment Commented Oct 31, 2013 at 7:13

7 Answers 7

2

Please mention exact issue you are facing, I assume the alignment of the two views is not proper is the issue. You can give the relative position of the button with respect to the text view to the button xml,

android:layout_toRightOf="@+id/windowtitle"

Also, add the alignment to the textview

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

Comments

1

Replace your XML with below code..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#484848"

            android:orientation="horizontal">

        <TextView
            android:id="@+id/windowtitle"
            android:layout_width="fill_parent"
           android:layout_centerInParent="true"
            android:layout_height="wrap_content"
            android:text="Support"
            android:gravity="center_horizontal|center_vertical"
            android:textSize="30sp" ></TextView>

        <Button
            android:id="@+id/syncbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="3dip"
            android:layout_alignParentRight="true"


            android:text="Sync" />

       </RelativeLayout>

</RelativeLayout>

Hope this work.....

1 Comment

Thanks Arvind for your effort but it is giving the same result :(
1

Try this..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:background="#484848">

        <TextView
            android:id="@+id/windowtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Support"
            android:layout_centerHorizontal="true"
            android:textSize="30sp" ></TextView>

        <Button
            android:id="@+id/syncbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="Sync" />

       </RelativeLayout>


    </RelativeLayout>

Here is my screen

enter image description here

8 Comments

@Tamilian we are very close. I posted screenshot in question again. The button is not yet properly aligned
@user45678 add this line android:layout_centerVertical="true" in button it must work and also remove android:layout_alignParentTop="true"
M talking about your code only. I posted the screenshot in question. Check that again. The button is not coming in center
I am using the same code which u mentioned and it is giving me result as i posted in second screenshot. Everything is working fine only if you can observe button is not in center, it is little up. I tried "layout:margin_Top" but it won't work. The button should be little in the center
@user45678 remove margin
|
0

Remove android:gravity="center_horizontal" from windowtitle Textview and add android:layout_centerInParent="true" even change width to wrap_content.

and add android:layout_centerVertical="true" to syncbutton button and there is no need of android:layout_alignParentTop="true"

It all centered your views.

check below xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="#484848"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/windowtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginTop="2dip"
            android:text="Support"
            android:textSize="30sp" >
        </TextView>

        <Button
            android:id="@+id/syncbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:layout_marginTop="3dip"
            android:gravity="left"
            android:text="Sync" />
    </RelativeLayout>

</RelativeLayout>

Comments

0

Use this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="#484848" >

    <TextView
        android:id="@+id/windowtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Support"
        android:textSize="30sp" >
    </TextView>

    <Button
        android:id="@+id/syncbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Sync" />
</RelativeLayout>

Comments

0

Use the following Code this might fullfill your requirement:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#484848"
            android:orientation="horizontal">

        <TextView
            android:id="@+id/windowtitle"
            android:layout_width="match_parent"
          android:layout_centerVertical="true"
            android:layout_height="wrap_content"
            android:text="Support"
            android:gravity="center_horizontal"
            android:textSize="30sp" ></TextView>

        <Button
            android:id="@+id/syncbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  

            android:layout_alignParentRight="true"            
            android:text="Sync" />

       </RelativeLayout>


    </RelativeLayout>

Comments

0

use this

 <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="40dip"
            android:background="#484848"
            android:orientation="horizontal">

        <TextView
            android:id="@+id/windowtitle"
            android:layout_width="wrap_content"
            android:layout_marginTop="2dip"
            android:layout_height="wrap_content"
            android:text="Support"
            android:gravity="center_horizontal"
            android:textSize="30sp" ></TextView>

        <Button
            android:id="@+id/syncbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
           android:layout_centerVertical="true"
            android:layout_alignParentRight="true"

            android:text="Sync" />

       </RelativeLayout>

Comments

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.