0

I can achieve this center-aligned text on Android action bar enter image description here

with these codes:

Activity

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.custom_toolbar); } 

custom_toolbar.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:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Awesome App\nBlah Blah Blah" android:textStyle="bold" android:textSize="20dp" android:textColor="@color/gold_yellow" /> </LinearLayout> 

Of course, that means every activity shows the same title:

Awesome App

Blah Blah Blah

Is there a way to change the text on custom_toolbar.xml programmatically, so I can show different titles on different activities?

1
  • 1
    This question is similar to: How to change the text on the action bar. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented May 11 at 11:48

1 Answer 1

0

Set the text of the TextView in the custom view.

((TextView)((LinearLayout)getSupportActionBar().getCustomView()).getChildAt(0)).setText("title"); 

https://developer.android.com/reference/android/widget/TextView#setText(java.lang.CharSequence)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.