Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change custom actionbar title

I'm using a custom layout to center the title of the actiobar.

  ActionBar actionBar = getActionBar(); 
  actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
  actionBar.setCustomView(R.layout.action_bar);

where action_bar is a simple linear layout with a textview

My problem is that I need every activity to have different names - and I need to change theme programmatically. So I'm trying something like this:

private void setActiobarTitle(String title)
{
   LayoutInflater inflator  = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View v = inflator.inflate(R.layout.action_bar, null);
   TextView titleTxtView = (TextView) v.findViewById(R.id.actionbarTitle);
   titleTxtView.setText(title);
}

When debuging the textview is found and initialized and everything seems to be okay - but the title doesn't change. Can anyone please tell me what seems to be the problem?

like image 300
Alin Avatar asked Nov 30 '22 12:11

Alin


1 Answers

Modify the setActionbarTitle() as below

 private void setActiobarTitle(String title)
 {

    View v = getActionbar().getCustomView();
    TextView titleTxtView = (TextView) v.findViewById(R.id.actionbarTitle);
    titleTxtView.setText(title);
 }
like image 82
SathMK Avatar answered Jan 04 '23 11:01

SathMK