0

I want to change the title and icon when the connection state changes... the updateMenuTitle() is from here and works because I've test it but is not working when I call the updateMenuTitle() from handler the app crashes at start

 private void updateMenuTitle(int x) {
    MenuItem btstatus = menu.findItem(R.id.btstatus);
    if (x == 1) {
        btstatus.setTitle("Connected");
        btstatus.setIcon(R.drawable.ic_btstatus_on);
    } else if (x == 2){
        btstatus.setTitle("Connecting");
        btstatus.setIcon(R.drawable.ic_btstatus_idle);
    }else if (x == 0){
        btstatus.setTitle("Disconnected");
        btstatus.setIcon(R.drawable.ic_btstatus_off);
    }
}

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_STATE_CHANGE:
            switch (msg.arg1) {
            case BluetoothRfcommClient.STATE_CONNECTED:
                mTxtStatus.setText(R.string.title_connected_to);
                mTxtStatus.append(" " + mConnectedDeviceName);
                updateMenuTitle(1);//not working if I call it from here... app force close
                break;
            case BluetoothRfcommClient.STATE_CONNECTING:
                mTxtStatus.setText(R.string.title_connecting);
                updateMenuTitle(2);//not working if I call it from here... app force close
                break;
            case BluetoothRfcommClient.STATE_NONE:
                mTxtStatus.setText(R.string.title_not_connected);
                updateMenuTitle(0);//not working if I call it from here... app force close
                break;
            }
            break;
            .......

Solution:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (mChangedStat && mChangedStat2) {
            menu.findItem(R.id.btstatus).setTitle(R.string.title_connected);
            menu.findItem(R.id.btstatus).setIcon(R.drawable.ic_btstatus_on);
        } else if (!mChangedStat && mChangedStat2) {
            menu.findItem(R.id.btstatus).setTitle(R.string.title_connecting);
            menu.findItem(R.id.btstatus).setIcon(R.drawable.ic_btstatus_idle);
        } else if (!mChangedStat && !mChangedStat2) {
            menu.findItem(R.id.btstatus).setTitle(R.string.title_not_connected);
            menu.findItem(R.id.btstatus).setIcon(R.drawable.ic_btstatus_off);
        }
}

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_STATE_CHANGE:
                                switch (msg.arg1) {
            case BluetoothServiceClient.STATE_CONNECTED:
                mChangedStat = true;
                mChangedStat2 = true;
                supportInvalidateOptionsMenu();
                break;
            case BluetoothServiceClient.STATE_CONNECTING:
                mChangedStat = false;
                mChangedStat2 = true;
                supportInvalidateOptionsMenu();
                break;
            case BluetoothServiceClient.STATE_NONE:
                mChangedStat = false;
                mChangedStat2 = false;
                supportInvalidateOptionsMenu();
                break;
            }
                break;
                .......
Community
  • 1
  • 1
Laurentiu
  • 47
  • 1
  • 9

2 Answers2

0

you can implement onPrepareOptionMenu() for this as this was more efficient and user open option menu and you can display required menu with title and icon.

check this example

http://thedevelopersinfo.wordpress.com/2009/10/20/dynamically-change-options-menu-items-in-android/

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • I don't understand can you please explain... as an obs: the menu item is not doing anything when is clicked is just like a state notification in the action bar – Laurentiu Feb 11 '13 at 10:49
  • Oh u used actionbar menu ok then call invalidateOptionsMenu(); after change the title and icon of menu item – Pratik Feb 11 '13 at 10:57
  • when I call the updateMenuTitle() from handler the app crashes at start so I need to know how to change the title and icon when the state changes because in current setup is not working... – Laurentiu Feb 11 '13 at 11:04
0

Im doing a little different:

private boolean flag = false;

void someMethod() {

   flag = true;
   invalidateOptionsMenu();

}

public boolean onCreateOptionsMenu (Menu menu) {

    if (flag) {
      // inflate menu 1
    } else {
      // inflate menu 2
    }

}

On this way you dont need to handle messages or something. You only need to manage menu items on onCreateOptionsMenu.

PaNaVTEC
  • 2,505
  • 1
  • 23
  • 36
  • the app is not crashing and the initial state change the menu item unfortunately I can't fully test until I have the device which I'm connecting with – Laurentiu Feb 11 '13 at 12:11