Hi I am developing android application. In my application I am using action bar. I want to make center align my title of window in action-bar. I know it is possible using xml files. but I want to do it using style so that it will applicable for my every window, how to do this need help. Thank you.
To make the title in the center of an appbar, use centerTitle:true property in the appbar widget.
This example demonstrate about how to create a custom action bar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Simple method to center ActionBarTitle without using custom layout for actionBar.
But before that first hide up Icon as :
myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
private void centerActionBarTitle() {
int titleId = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
titleId = getResources().getIdentifier("action_bar_title", "id", "android");
} else {
// This is the id is from your app's generated R class when
// ActionBarActivity is used for SupportActionBar
titleId = R.id.action_bar_title;
}
// Final check for non-zero invalid id
if (titleId > 0) {
TextView titleTextView = (TextView) findViewById(titleId);
DisplayMetrics metrics = getResources().getDisplayMetrics();
// Fetch layout parameters of titleTextView
// (LinearLayout.LayoutParams : Info from HierarchyViewer)
LinearLayout.LayoutParams txvPars = (LayoutParams) titleTextView.getLayoutParams();
txvPars.gravity = Gravity.CENTER_HORIZONTAL;
txvPars.width = metrics.widthPixels;
titleTextView.setLayoutParams(txvPars);
titleTextView.setGravity(Gravity.CENTER);
}
}
I know it is possible using xml files. but I want to do it using style so that it will applicable for my every window, how to do this need help.
You still use xml to do this. Just apply the style application wide and not just to a single activity. Please see the android documentation for a complete description:
https://developer.android.com/training/basics/actionbar/styling.html
Then apply your theme to your entire app or individual activities:
<application android:theme="@style/CustomActionBarTheme" ... />
EDIT: Here's how you'll have to handle the centering of the the title:
In your action_bar.xml you'll have something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_centerHorizontal="true"
android:text="SOME TITLE"
android:textSize="18sp" />
</RelativeLayout>
Then in your onCreate() you'll have something like:
getSupportActionBar().setCustomView(R.layout.action_bar);
If you need to have this in every activity, you have 2 choices:
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