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?

  • Get details about Intent of a PendingIntent
  • Can I pass a Javascript object out to an Android WebView?
  • Is it possible to define Activity inside Android test project and run a test against it?
  • Android Fullscreen dialog confirmation and dismissive actions
  • Password protecting my android app (the simple way)
  • How to use an existing database with an Android application
  • Related posts:

    How to make a LinearLayout scrollable
    Android accelerometer accuracy (Inertial navigation)
    java.exe finished with non-zero exit value 2 when using Facebook SDK
    Using SurfaceTexture in combination with RenderScript
    Start service after force stop
    Picasso loads into wrong imageview when using view holder pattern
  • How to create shape with solid, corner, stroke in Java Code?
  • Make ImageView with Round Corner Using picasso
  • Android Maven build gives trouble processing “javax/xml/namespace/QName.class”:
  • Android draw ball trail
  • Convert a Bundle to JSON
  • Intent for selecting wallpaper with wallpaper region highlighting
  • 3 Solutions collect form web for “Change text dynamically for Android Actionbar Button”

    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.

    ActionBar items are from the options menu, rendered as buttons when shown on the bar. If you don’t supply an icon, the title of the option MenuItem is used as the title of the button,

    So, unless you are using a custom view, and you are simply using a menu item title as the score indicator, you can change that in onPrepareOptionsMenu(). When you need to update, call invalidateOptionsMenu(), and onPrepareOptionsMenu will be called for you.

    eg

    //user scores some points
    mUserScore += points;
    invalidateOptionsMenu();
    

    then in the callback:

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        MenuItem score = menu.findItem(R.id.score_menu_item);
        score.setTitle(String.valueOf(mUserScore));
    }
    

    This code worked for me

    actionBar.setCustomView(R.layout.my_action_bar);
    button=(Button)actionBar.getCustomView().findViewById(R.id.button);
    button.setText("text");
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.