Reputation: 61
So I am trying to add a switch into the menu and I want to at (least at this point) make a Toast when the switch is on or off. Currently nothing is happening and I'm not too sure why.
I have added the code for my menu item and switch layout.
Please advise, Thank you!!
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.switch, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.myswitch:
Switch simpleSwitch = (Switch) findViewById(R.id.menuSwitch);
simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Toast.makeText(ModularActivity.this, "This is on", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ModularActivity.this, "This is off", Toast.LENGTH_SHORT).show();
}
}
});
return true;
case android.R.id.home:
// Navigate back to parent activity (CatalogActivity)
NavUtils.navigateUpFromSameTask(this);
return true;
}
return true;
}
Here is the menu item:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/activity_switch_layout"
/>
Here is the activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:textStyle="bold"
android:textColor="@android:color/white"
android:text="WHITE"/>
<Switch
android:id="@+id/menuSwitch"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:text="GREEN"
android:layout_marginRight="5dp"
android:textStyle="bold"
android:textColor="@color/green"/>
</LinearLayout>
Upvotes: 0
Views: 2079
Reputation: 83
Try moving your code to onCreateOptionsMenu like this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem menuItem = menu.findItem(R.id.myswitch);
View view = MenuItemCompat.getActionView(menuItem);
Switch simpleSwitch = (Switch) view.findViewById(R.id.menuSwitch);
simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Toast.makeText(MainActivity.this, "This is on", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "This is off", Toast.LENGTH_SHORT).show();
}
}
});
return super.onCreateOptionsMenu(menu);
}
And also, note how I've introduced the MenuItem and View in frist two lines. You basically have to look for your switch not everywhere, but exactly in your menu view.
After that everything works perfectly.
Upvotes: 1
Reputation: 144
You cannot add the switch or toggleButton in menu items. But you can do the same on toolbar if you please.
Before that I need to get your attention to the menu item you have added. You can add android:checkable="true"
<item
android:id="@+id/checkable_menu"
android:checkable="true"
android:title="@string/checkable" />
Then you can change the state of the same in method onOptionsItemSelected
case R.id.checkable_menu:
isChecked = !item.isChecked();
item.setChecked(isChecked);
return true;
But still use the toolbar concept if you want to show the switch or toggle button...
<item
android:id="@+id/show_secure"
android:enabled="true"
android:title=""
android:visible="true"
app:actionLayout="@layout/show_protected_switch"
app:showAsAction="ifRoom" />
And this is your switch.xml layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ToggleButton
android:id="@+id/switch_show_protected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/switch_ptotected_btn_selector"
android:textOff=""
android:textOn=""/>
</RelativeLayout>
And in your activity
ToggleButton mSwitchShowSecure;
mSwitchShowSecure = (ToggleButton) menu.findItem(R.id.show_secure).getActionView().findViewById(R.id.switch_show_protected);
mSwitchShowSecure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
//Your code when checked
} else {
//Your code when unchecked
}
}
});
Upvotes: 0