Search code examples
androidandroid-actionbaractionbarsherlock

Change text dynamically for Android Actionbar Button


I have an Action Bar (using actionbarsherlock) that has two items in it. One of them is the amount of points the user has collected in my app. I want to change this Action Bar Button when the amount of points changes.

I can't figure out how to set the TextView of that Button. What can I do to set that via code?


Solution

  • First of all you don't set "TextView of a Button", because Button extends TextView, you set text of the Button itself. You can try to do this the following way in your activity's onCreate():

    //I suppose you create custom ActionBar like this
    final View addView = getActivity().getLayoutInflater().inflate(R.layout.my_action_bar, null);
    bar.setCustomView(addView);
    button=(Button)addView.findViewById(R.id.button);
    button.setText("Whatever");
    

    If you use menu items, then you should try this approach:

    getSupportMenuInflater().inflate(R.menu.activitymenu, menu);
    MenuItem menuItem=menu.findItem(R.id.menuSearch);
    

    Also try looking here,here and here. It should be helpful.