14

I need to implement a TextView in the ActionBar. This TextView shows the status of the bluetooth connection so it will update depending of this status.

I'm not talking about changing ActionBar's title, but adding a textview f.e. under the title or at the right side.

At the moment, what I have done is create an item in the menu.xml:

<item
    android:id="@+id/statusTextview"
    android:actionViewClass="android.widget.TextView"
    android:showAsAction="ifRoom"
    android:title="Disconected" />

Then Add it on the mainActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.bluetooth, menu);
    return true;
}
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    final MenuItem menuItem = menu.findItem(R.id.statusTextview);
    tvStatus = (TextView) menuItem.getActionView();
    return super.onPrepareOptionsMenu(menu);
}

But It isn't working correctly. At first time should show Disconnected status and doesn't show nothing. Then when the status changes and the textview must update, it throws a NPE.

masmic
  • 3,526
  • 11
  • 52
  • 105

2 Answers2

22

You can Add TextView manually in

@Override
public boolean onCreateOptionsMenu(Menu menu) {

By following code.

TextView tv = new TextView(this);
            tv.setText(getString(R.string.matchmacking)+"  ");
            tv.setTextColor(getResources().getColor(R.color.WHITE));
            tv.setOnClickListener(this);
            tv.setPadding(5, 0, 5, 0);
            tv.setTypeface(null, Typeface.BOLD);
            tv.setTextSize(14);
            menu.add(0, FILTER_ID, 1, R.string.matchmacking).setActionView(tv).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

and if you want to access text view then make tv as class variable.

Yashdeep Patel
  • 3,090
  • 1
  • 17
  • 21
  • If I do this, I still have to mantain the definiton of the item in the menu.xml? Cause this way I'm creating the item programmatically, and i don't know what refereces the FILTER_ID def – masmic Mar 21 '14 at 08:55
  • 1
    Thank you. Very precise and exact solution – sid_09 Jan 08 '16 at 15:10
  • 3
    what is the 'FILTER_ID'? – Ajay Sivan Sep 12 '16 at 18:22
  • 1
    `FILTER_ID` doesn't seem to be a pre-defined constant. Checking the [doc](https://developer.android.com/reference/android/view/Menu.html#add(int,%20int,%20int,%20int)). You can pass `0` as the second argument to `add()`, if you do not need a unique ID for the item. – Sridhar Sep 18 '17 at 09:30
1

Just in case anyone still needs this:

You can dynamically set the text property of menu item by calling the setTitle("Your new string or String resource id") method on the menu item. That way, you do not have to cast the ActionView into a TextView. For example:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    String bluetoothStatus = "Connected"; //get from source

   menu.findItem(R.id.the_id_of_the_menu_item).setTitle(bluetoothStatus);
    return super.onPrepareOptionsMenu(menu);
}
Pens Naku
  • 11
  • 1
  • 5