Subcategory: WindowTitle, Style
Android Platform: 1.5, Google APIs 3
Referenced Classes: android.app.Activity, android.view.Gravity, android.view.View, android.widget.FrameLayout, android.widget.LinearLayout, android.widget.TextView
After failing to center the title of my Window (WindowTitle) using android:windowTitleStyle, as discussed in the following thread (http://groups.google.com/group/android-developers/browse_thread/thread/d39ea52ee066dfa8/fccce4564395e08b?lnk=gst&q=center+title+gravity#fccce4564395e08b), I decided to investigate further. After looking at the source code, I narrowed my search to the internal class com.android.internal.policy.impl.PhoneWindow and layout file, screen_title.xml. Due to
the attribute android:gravity being specified on the "title" TextView, it seems to override any android:gravity set on the android:windowTitleStyle:
code snippet
<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" />
To get around this, I came up with the following:code snippet
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.Note, this does not take into account future changes to the internal window layout.
Works like a charm. I have a MainActivity that all other activities are extending thus making this work for all activities - great :)
ReplyDeletei 'm getting error at activity.getWindow(),.,. (activity cannot be resolved) how can fix it??? plz help me
ReplyDelete