Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Toolbar back enable and set subtitle

I'm using android.support.v7.widget.Toolbar and AppCompatActivity. I've enabled back up button like this supportActionBar.setDisplayHomeAsUpEnabled(true);.

The fragment in side the activity will set the title and subtitle in onResume() like this

AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setTitle(title);
activity.getSupportActionBar().setSubtitle("Bingo");

The problem is, when the fragment shows up onResume is called but subtitle is not shown. When I press power OFF and ON, means fragment goes to pause and resumes again. Now, subtitle is visible. I've tested on other android phone as well.

Can you please help me finding out the problem?

like image 418
Sundeep1501 Avatar asked May 23 '26 21:05

Sundeep1501


1 Answers

This is because the toolbar is not rendered when you are setting the subtitle. Try this code,Set title and subtitle inside this method

private void setupToolbar(){
    toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
    if(toolbar != null){
        setSupportActionBar(toolbar);
     }

     toolbar.post(new Runnable()
    {
        @Override
        public void run()
        {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(mTitle);
            getSupportActionBar().setSubtitle("Subtitle);
        }
    });
}
like image 70
Phillen Avatar answered May 25 '26 11:05

Phillen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!