How to align center the title or label in activity?
50,295
Solution 1
You will need to define a custom title style/theme. Look in the samples for com.example.android.apis.app.CustomTitle.
Solution 2
It will work fine..
TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER );
customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);
Solution 3
I found another way of setting the title center, 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);
Hope this helps. Thanks!
Solution 4
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // must come before setContentView
setContentView(R.layout.activity_calculator);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
Put your layout in layout/title_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="Custom Centered Title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/black"
android:gravity="center"
/>
Comments
-
iamtheexp01 almost 2 years
I know there are two ways of setting the title of activity. One way is the setting it in the android manifest like this android:label="@string/app_name". Second is programmatically setting in activity class like setTitle("Hello World!"). Both ways are positioned in the left side but how can I put it in the center?
-
Ted Hopp almost 13 yearsThe only problem with this is that it relies on undocumented aspects of how the window contents are structured. Google has been known to radically change undocumented data structures, and there's no guarantee that this particular technique will work in the future.
-
HardCoder almost 12 yearsWell, google sucks (at os design) and it doesn't work for me. But this does: ((TextView)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
-
ateiob almost 12 years@TedHopp Google has been known to radically change documented data structures as well... ignoring backward compatibility and making my life miserable every time I decide to upgrade any of the Android SDKs or dev tools. I would say: Do what works now. It's going to break anyway, and you'll need to fix it anyway. Regardless of whether it's documented or not.
-
ateiob almost 12 yearsAndroidRuntimeException: You cannot combine custom titles with other title features.
-
Ted Hopp almost 12 years@ateiob - Yes, the idea is that if you want "other title features" in your custom title, you'll have to build those features into your custom title yourself. Android can't know how to combine the stock features unless it also controls the title itself.
-
Kid24 about 11 yearsYou are awesome! Thanks for sharing.
-
Christian Brüggemann almost 11 years@Chloe If you use ActionBarSherlock (what you should do), you can use com.actionbarsherlock.app.ActionBar.LayoutParams for supporting API levels below 11.
-
Cote Mounyo almost 11 yearsI am using minSdk 14. Yet this is not working for me. See my questions: stackoverflow.com/questions/17605282/…
-
Cote Mounyo almost 11 yearsI am using minSdk 14. Yet this is not working for me. See my questions: stackoverflow.com/questions/17605282/…
-
Mitesh Shah over 10 years@Chaitu how to tackle back navigation??
-
weston about 10 years-1 it's a hack that relies on the layout structure always being the same and surprise, surprise no longer works.
-
VikramV about 10 yearsI get a error -
Caused by: java.lang.ClassCastException: com.android.internal.widget.ActionBarView cannot be cast to android.widget.TextView
when I use the code in the above mentioned answer. -
Chaitu about 4 yearsCant tell you the reason unless I know what theme you are using. For time being can you check by taking ToolBar and align the title textview center?