Skip to content

Instantly share code, notes, and snippets.

@yakivmospan
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakivmospan/5e038633333d948e01aa to your computer and use it in GitHub Desktop.
Save yakivmospan/5e038633333d948e01aa to your computer and use it in GitHub Desktop.
Android Custom Actionbar Title

Android Custom Actionbar Title

Important:

  • Use R.layout.ben_toolbar_simple_title if you have menu items in layout, or doesn't have any buttons at all
  • Use R.layout.ben_toolbar_margin_title if you have only one button in left corner (it could be back button for e.g)
public void initActionBar() {
ActionBar supportActionBar = getSupportActionBar();
supportActionBar.setDisplayShowTitleEnabled(false);
supportActionBar.setDisplayHomeAsUpEnabled(true);
supportActionBar.setDisplayShowCustomEnabled(true);
supportActionBar.setCustomView(R.layout.ben_toolbar_simple_title);//have no margin
supportActionBar.setCustomView(R.layout.ben_toolbar_margin_title);//have righth margin
TextView toolbarTitle = (TextView) supportActionBar.getCustomView().findViewById(
R.id.toolbarTitle
);
toolbarTitle.setText(R.string.share);
//supportActionBar.setBackgroundDrawable();
//...
}
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginRight="@dimen/abc_action_button_min_width_material"
android:textSize="20dp"
android:textColor="@android:color/white"
android:singleLine="true"
android:ellipsize="end">
</TextView>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20dp"
android:textColor="@android:color/white"
android:singleLine="true"
android:ellipsize="end">
</TextView>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment