android:set title bar in middle
Asked Answered
S

3

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.

Stretch answered 20/5, 2011 at 4:32 Comment(0)
S
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.

Stretch answered 6/6, 2011 at 6:22 Comment(1)
+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, you won't be able.Nubbly
M
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.

Maxine answered 20/5, 2011 at 6:26 Comment(2)
"Click here" sort of answers are not supported/advised here on SO.Forsaken
Thanks! It was before I understand what is SO. :)Maxine
C
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!!!!

;)

Chairman answered 20/5, 2011 at 6:29 Comment(2)
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 pastebin.com/VffL6MngStretch
Hi,i have not error when there is only textView but when i set the icon in title bar it give the errorStretch

© 2022 - 2024 — McMap. All rights reserved.