Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MenuItem Toggle Button

In my Android application, I want a setting to be toggleable in it's menu like the Dialer app does for speaker and mute. You can see a picture below:

http://www.isaacwaller.com/images/acall.png

You see how the Speaker, Mute and Hold options are toggle buttons - you can tap them again and they will toggle the green color. They may do this in a custom way, but I suspect it is a option (I tried setting the Checkable attribute).

like image 561
Isaac Waller Avatar asked Feb 20 '09 06:02

Isaac Waller


People also ask

What is Android system menu?

The Android System Settings menu allows you to control most aspects of your device—everything from establishing a new Wi-Fi or Bluetooth connection, to installing a third-party onscreen keyboard, to adjusting system sounds and screen brightness.

What types of menus does Android support?

There are three types of menus in Android: Popup, Contextual and Options. Each one has a specific use case and code that goes along with it. To learn how to use them, read on.

How do we hide the menu on the toolbar in one fragment?

How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.


2 Answers

You could do something like the snippet below as well, originally sourced from anddev.org

public boolean onPrepareOptionsMenu(final Menu menu) {       
      if(super.mMapView.isTraffic()) 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_off_48); 
      else 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_on_48); 

      return super.onPrepareOptionsMenu(menu); 
 }
like image 137
Quintin Robinson Avatar answered Oct 21 '22 17:10

Quintin Robinson


It's looks like this menu item is implemented as a custom view.

In the android source code you can take a look at com.android.phone.InCallMenuView.java to see how this is implemented.

It doesn't look like it is part of the public API, but it looks pretty self-contained. If your project has a compatible license, you may be able to copy it into your project and use and modify it as you see fit.

like image 27
Brian Pellin Avatar answered Oct 21 '22 17:10

Brian Pellin