7

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?

iamtheexp01
  • 3,446
  • 9
  • 35
  • 35

4 Answers4

21

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); 
Soph
  • 702
  • 5
  • 11
Chaitu
  • 907
  • 2
  • 13
  • 27
  • @Chloe If you use ActionBarSherlock (what you should do), you can use com.actionbarsherlock.app.ActionBar.LayoutParams for supporting API levels below 11. – Christian Brüggemann Jun 10 '13 at 10:53
  • I am using minSdk 14. Yet this is not working for me. See my questions: http://stackoverflow.com/questions/17605282/center-title-of-activity-in-android-action-bar – Cote Mounyo Jul 12 '13 at 00:41
  • @Chaitu how to tackle back navigation?? – Mitesh Shah Jan 09 '14 at 14:02
  • Cant 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? – Chaitu Feb 20 '20 at 12:54
5
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"
   />

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/2.2_r1.1/com/example/android/apis/app/CustomTitle.java

Chloe
  • 25,162
  • 40
  • 190
  • 357
  • I am using minSdk 14. Yet this is not working for me. See my questions: http://stackoverflow.com/questions/17605282/center-title-of-activity-in-android-action-bar – Cote Mounyo Jul 12 '13 at 00:42
5

You will need to define a custom title style/theme. Look in the samples for com.example.android.apis.app.CustomTitle.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • [AndroidRuntimeException](http://stackoverflow.com/q/11254366): You cannot combine custom titles with other title features. – ateiob Jun 29 '12 at 12:05
  • @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. – Ted Hopp Jun 29 '12 at 15:43
5

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!

iamtheexp01
  • 3,446
  • 9
  • 35
  • 35
  • 15
    The 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. – Ted Hopp Aug 14 '11 at 05:03
  • 3
    Well, 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); – HardCoder Jun 09 '12 at 10:58
  • 2
    @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 Jun 29 '12 at 12:04
  • -1 it's a hack that relies on the layout structure always being the same and surprise, surprise no longer works. – weston Feb 27 '14 at 16:52
  • I 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. – VikramV Mar 14 '14 at 15:46