The menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/idSwitchColor"
android:title="Switch"
android:actionLayout="@layout/use_switch"
app:actionViewClass="android.widget.Switch"
app:showAsAction="always"/>
</menu>
Activity code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.popupmenu, menu);
inflater.inflate(R.menu.testmenu, menu);
MenuItem itemSwitch = menu.findItem(R.id.idSwitchColor);
itemSwitch.setActionView(R.layout.use_switch);
final Switch swi= (Switch) itemSwitch.getActionView();
swi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(swi.isChecked() == true) {
Toast.makeText(HomePage.this, "Worked", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(HomePage.this, "Bye", Toast.LENGTH_SHORT).show();
}
});
return super.onCreateOptionsMenu(menu);
I tried debugging and it crashes at the OnClickListener part (at least from what I could tell, I'm not very good at this). Thanks in advance to anyone who can try and help me! Would really mean a lot