Martin Erlic
Martin Erlic

Reputation: 5667

Programatically change the title text of my AppCompatActivity SupportActionBar

Why does this seem so difficult to accomplish? I have a condition in which I want to dynamically change the action bar of my activity.

android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setTitle(getString(R.string.voted_on_by));

Why doesn't this change the title of my action bar to the string I want?

In onCreate I do this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

assert getSupportActionBar() != null;

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Upvotes: 2

Views: 4921

Answers (1)

santosh kumar
santosh kumar

Reputation: 2962

you can use this

((MainActivity) this).getSupportActionBar().setTitle(R.string.voted_on_by);

or

((AppCompatActivity)this).getSupportActionBar().setTitle("sub categories");

Upvotes: 7

Related Questions