2

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.

Sameer Z.
  • 3,265
  • 9
  • 48
  • 72

3 Answers3

5

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.

Sameer Z.
  • 3,265
  • 9
  • 48
  • 72
  • +1 for posting a correct answer that doesn't rely on undocumented structures. However, if you ever want to add to your custom title bar a [progress bar](http://stackoverflow.com/q/11254366/869501), you won't be able. – ateiob Jun 29 '12 at 12:32
2

This page can help you?

This:

<TextView android:id="@android:id/title" 
   style="?android:attr/windowTitleStyle"
   android:background="@null"
   android:fadingEdge="horizontal"
   android:gravity="center_vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />

And:

ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
LinearLayout root = (LinearLayout) decorView.getChildAt(0);
FrameLayout titleContainer = (FrameLayout) root.getChildAt(0);
TextView title = (TextView) titleContainer.getChildAt(0);
title.setGravity(Gravity.CENTER);

This should be called in the Activity.onCreate(...) after setting the content view. From source.

David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
2

just put the code under the setContentView(R.layout.new)...

((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);

To be very frank this code is copied from another stackoverflow question: How to align center the title or label in activity?

I am sure it can help you mate!!!!

;)

Community
  • 1
  • 1
necixy
  • 4,964
  • 5
  • 38
  • 54
  • Thank for response but i get the error Caused by: java.lang.ClassCastException: android.widget.RelativeLayout com.vvseksperten.HomeScreenActivity.onCreate(HomeScreenActivity.java:28) may be due to my layout please check it out here http://pastebin.com/VffL6Mng – Sameer Z. May 20 '11 at 06:44
  • Hi,i have not error when there is only textView but when i set the icon in title bar it give the error – Sameer Z. May 20 '11 at 08:23