android:set title bar in middle

Sameer Z. picture Sameer Z. · May 20, 2011 · Viewed 8.6k times · Source

I create the custom title using the style.I code some from http://labs.makemachine.net/2010/03/custom-android-window-title/

I have also set the icon in title bar using the setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.title_logo); can it is possible to set the title bar in middle?if possible then I want set using the custom style.

EDIT: i have check code http://andmobidev.blogspot.com/2010/01/centering-title-of-window.html it work fine when there no icon in title bar but when set the icon i get the error java.lang.ClassCastException: android.widget.RelativeLayout i have no idea how the window draw the title bar ,i have check the one of blog http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html

Thank you.

Answer

Sameer Z. picture Sameer Z. · Jun 6, 2011

I can find the real answer, at last i load the new layout for center the title bar set title bar code in Oncreate

 final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);
        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.title_bar);
        }
        final TextView myTitleText = (TextView) findViewById(R.id.title_bar);
        if (myTitleText != null) {
            myTitleText.setText(R.string.title_ble_melem);

        }

here is the layout

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    >
    <TextView
        android:id="@+id/title_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="title bar"
        android:layout_centerInParent="true"
        android:layout_marginLeft="5dip"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:textSize="20sp"
       />

     <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/title_bar"
        android:src="@drawable/ic_top_bar"

    />
    </RelativeLayout >

Thank you very much for response.