Search code examples
androidbuttontitletitlebar

Android Title + Button in titlebar not coming properly aligned


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>

Solution

  • 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.....