1

I am using following code to add checkbox in Android Action Bar:

 <item
    android:id="@+id/action_checkbox"
    app:actionViewClass="android.widget.CheckBox"
    android:title="@string/action_check"
    app:showAsAction="always"
    />

How can I get the value of above checkbox whether it is selected or not as Menu item in android?

3 Answers3

6

You can use isChecked() to get the checked state of a CheckBox:

CheckBox checkBox = (CheckBox) findViewById(R.id.action_checkbox);

if(checkBox.isChecked())
   // do something
else 
   // do something else
Mazen Elian
  • 228
  • 1
  • 4
  • 19
  • Thanks for your answer but where in onOptionsItemSelected ? –  Oct 25 '15 at 05:28
  • If you are asking about where to use this code, then you can use it anywhere u want, as the `findViewById()` will work in any context. – Mazen Elian Oct 25 '15 at 05:31
  • It's an Action bar Menu Item. So, It will not work anywhere :) –  Oct 25 '15 at 05:32
  • From my knowledge the `findViewById()` can get a view from anywhere, as it gets views from the `R` file which is pretty much accessible from all contexts in your application. – Mazen Elian Oct 25 '15 at 05:37
3

your java code where you manage checkbox click event

 @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu_main, menu);
       MenuItem check = menu.findItem(R.id.action_checkbox);
       CheckBox c_box =(CheckBox) check.getActionView();
       c_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
                Toast.makeText(getActivity(), "checked",
                        Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(getActivity(), "unchecked",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
      return true;
   }

your menu looks like ok

<item
android:id="@+id/action_checkbox"
app:actionViewClass="android.widget.CheckBox"
android:title="@string/action_check"
app:showAsAction="always"
/>

Hope it works.. let me know if you face any problem.

Tanim reja
  • 2,120
  • 1
  • 16
  • 23
-1

Please Refer to Documentation for simple questions like these.

Also onOptionsItemSelected is part of Menu.

Muhammad Ali
  • 3,478
  • 5
  • 19
  • 30
  • Even then refer to the documentation. Don't waste people's time if you have't done research. SO is for problems not to teach. – Muhammad Ali Oct 26 '15 at 07:11
  • Ah! Never mind guys :) @SnowTauren Actually you also didn't search it on official doc because there is no information available about "How to use checkboxes in menu_item" and "What is the problem with it"? "Why these checkboxes are not showing up on App bar". did you? But that's ok :) – Amit Pal Oct 26 '15 at 07:29
  • @Def I think you should try google first :) But it's ok Please refer this question : http://stackoverflow.com/questions/8148746/android-action-bar-checkable-menu-item-does-not-work-show-properly – Amit Pal Oct 26 '15 at 07:29
  • @AmitPal I know these comments are old. But this simple question is good for future people. Anyone looking for this will find the SO question (like me) and see the answer faster than looking through the documentation – Ajay Apr 01 '17 at 14:34