[ACCEPTED]-How to align center the title or label in activity?-title

Accepted answer
Score: 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); 

0

Score: 5

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

Score: 5

I found another way of setting the title 2 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 1 this helps. Thanks!

Score: 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

0

More Related questions