I searched a lot but can not find a way, How can I set then title at center of ActionBar instead of left aligned. I used below code to set the title at center :
ViewGroup decorView= (ViewGroup) this.getWindow().getDecorView(); LinearLayout root= (LinearLayout) decorView.getChildAt(0); FrameLayout titleContainer= (FrameLayout) root.getChildAt(0); TextView title= (TextView) titleContainer.getChildAt(0); title.setGravity(Gravity.CENTER);
But it gives error as below :
ClassCastException : com.android.internal.widget.ActionBarView can not be cast to android.widget.TextView.
Any other solution ..Any help will be appreciated.
You can create a custom layout and apply it to the actionBar.
To do so, follow those 2 simple steps:
Java Code
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.actionbar);
Where R.layout.actionbar
is the following layout.
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/action_bar_title" android:text="YOUR ACTIVITY TITLE" android:textColor="#ffffff" android:textSize="24sp" /> </LinearLayout>
It can be as complex as you want. Try it out!
EDIT:
To set the background
you can use the property android:background
in the container layout (LinearLayout in that case). You may need to set the layout height android:layout_height
to match_parent
instead of wrap_content
.
Moreover, you can also add a LOGO / ICON to it. To do so, simply add an ImageView inside your layout, and set layout orientation property android:orientation
to horizontal (or simply use a RelativeLayout and manage it by yourself).
To change the title of above custom action bar dynamically, do this:
TextView title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName())); title.setText("Your Text Here");
It seems there is no way to do this without custom view. You can get the title view:
View decor = getWindow().getDecorView(); TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));
But changing of gravity
or layout_gravity
doesn't have an effect. The problem in the ActionBarView
, which layout its children by itself so changing of layout params of its children also doesn't have an effect. To see this excecute following code:
ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android")); View v = actionBar.getChildAt(0); ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); p.gravity= Gravity.CENTER; v.setLayoutParams(p); v.setBackgroundColor(Color.BLACK);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With