Reputation: 6709
I have this menu layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/actionBarSave"
android:showAsAction="always"
android:scaleType="fitXY"
android:icon="@drawable/save_33x26"
style="@style/ActionButtonStyle"
android:title="save" />
<item android:id="@+id/actionBarLoad"
android:showAsAction="always"
android:icon="@drawable/load70x24"
android:scaleType="fitXY"
android:title="load" />
<item android:id="@+id/actionBarDelete"
android:showAsAction="always"
android:scaleType="fitXY"
android:icon="@drawable/delete_enabled"
android:title="delete" />
<item
android:id="@+id/actionBarSoundSwitch"
android:title="soundswitch"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:showAsAction="always"
android:actionLayout="@layout/sound_switch" />
</menu>
The sound_switch layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="@+id/soundSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SFX"
android:layout_centerInParent="true" />
</RelativeLayout>
Here is my java code where I am attempting to add a OnCheckedChangeListener to the switch:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
soundSwitch = (Switch)findViewById(R.id.actionBarSoundSwitch);
soundSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
soundOn = arg1;
}
});
switch (item.getItemId()) {
case R.id.actionBarSave:
onSave();
return true;
case R.id.actionBarLoad:
onLoad();
return true;
case R.id.actionBarDelete:
onDelete();
return true;
case R.id.actionBarSoundSwitch:
onSound();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
There is no logcat error but I've checked and the listener is not working. Any ideas why?
Upvotes: 3
Views: 1796
Reputation: 349
<item
android:title="Switch"
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
If we go to "@layout/switch_item" we will find
<androidx.appcompat.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
I added an id tag to the SwitchCompat as
android:id="@+id/switch_darkmode"
and then added CheckedChanged Listener in the activity
SwitchCompat darkmode=findViewById(R.id.switch_darkmode);
darkmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
System.out.println("SWITCH TOGGLED");
}
});
Upvotes: -1
Reputation: 1866
Try this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/actionBarSave"
android:showAsAction="always"
android:scaleType="fitXY"
android:icon="@drawable/save_33x26"
style="@style/ActionButtonStyle"
android:title="save" />
<item android:id="@+id/actionBarLoad"
android:showAsAction="always"
android:icon="@drawable/load70x24"
android:scaleType="fitXY"
android:title="load" />
<item android:id="@+id/actionBarDelete"
android:showAsAction="always"
android:scaleType="fitXY"
android:icon="@drawable/delete_enabled"
android:title="delete" />
<item
android:id="@+id/actionBarSoundSwitch"
android:title="soundswitch"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:actionViewClass="android.widget.ImageButton"
android:showAsAction="always"
android:actionLayout="@layout/sound_switch" />
</menu>
//----------------------------------------------------------
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_search, menu);
soundSwitch = (Switch)findViewById(R.id.actionBarSoundSwitch);
soundSwitch .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return true;
}
Upvotes: 0
Reputation: 5515
try this in onPrepareOptionMenu
MenuItem item = menu.findItem(R.id.actionBarSoundSwitch);
soundSwitch = (Switch)item.getActionView().findViewById(R.id.soundSwitch);
Upvotes: 0
Reputation: 21183
Since there is no information where exactly you are adding the ActionBar
switch button and what API levels you are targeting, I am simply posting how I would add one.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionbarswitch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.actionbarswitch.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.actionbarswitch;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.Switch;
public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
private static final String TAG = "ActionBarSwitch";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch actionBarSwitch = new Switch(this);
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END));
actionBarSwitch.setOnCheckedChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, "isChecked? " + isChecked);
}
}
LogCat output:
I/ActionBarSwitch(10081): isChecked? true
I/ActionBarSwitch(10081): isChecked? false
Screenshot (taken from Nexus5):
Hope this helps.
Upvotes: 3
Reputation: 1287
Be sure you inflate the menu first in onPrepareOptionsMenu
using R.menu.mymenuxml
then find your views using the menu, not the activity context
soundSwitch = (Switch)menu.findViewById(R.id.actionBarSoundSwitch);
Upvotes: 0