Issue
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?
Solution
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.
Answered By - ozbek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.