How to Add a Switch to Android Action Bar

How to add a switch to android action bar?

Create a layout for the switch switch_layout.xml. Custom layouts for menu should always be RelativeLayout

<?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/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

</RelativeLayout>

Then, in your mainmenu.xml add the item as follows

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="@layout/switch_layout"
/>
</menu>

And in your activity, inflate the mainmenu.xml as you always do

getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;

Add Switch widget to ActionBar and respond to change event

You need to call MenuItem.getActionView, here's an example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate your Menu
getMenuInflater().inflate(R.menu.your_menu, menu);

// Get the action view used in your toggleservice item
final MenuItem toggleservice = menu.findItem(R.id.toggleservice);
final Switch actionView = (Switch) toggleservice.getActionView();
actionView.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Start or stop your Service
}
});
return super.onCreateOptionsMenu(menu);
}

How to add switch listener in action bar android?

Switch mainSwitchOnOffSw = (Switch) view.findViewById(R.id.switchForActionBar);

Should be

SwitchCompat mainSwitchOnOffSw = (SwitchCompat) view.findViewById(R.id.switchForActionBar);

as you are using SwitchCompat in your layout.

Put switch inside action bar on Android

Add android:actionLayout to your <item> in your menu XML resource, pointing to a layout XML resource that has your Switch. Then, use getActionView() on the MenuItem to register listeners on changes in the switch.

Note that Switch only works on API Level 14 and higher.

How to get/set action event in Android actionbar switch

I found my error. I have to use

View view = MenuItemCompat.getActionView(menuItem);

extra line in onCreateOptionsMenu to find out the switch button
. Here is my full method

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);

MenuItem menuItem = menu.findItem(R.id.myswitch);
View view = MenuItemCompat.getActionView(menuItem);
Switch switcha = (Switch) view.findViewById(R.id.switchForActionBar);
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do anything here on check changed
}
});
return super.onCreateOptionsMenu(menu);
}

How to add material design library switch in the action bar

Replace your Switch code with below code

<com.gc.materialdesign.views.Switch
android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#1E88E5" />

and Replace item code with

<item
android:id="@+id/menu_switch"
android:title="off/on"
app:showAsAction="always"
app:actionLayout="@layout/switchlayout"/>

Switch button not showing up in ActionBar?

your are getting error because your actionView is null. change your Switch menu code

<item
android:id="@+id/toggle_test"
android:title=""
app:showAsAction="ifRoom"
android:orderInCategory="1"
android:actionViewClass="android.widget.Switch" />

to app:actionViewClass="android.widget.Switch" look carefully it would be app not android like this...

<item
android:id="@+id/toggle_test"
android:title=""
app:showAsAction="ifRoom"
android:orderInCategory="1"
app:actionViewClass="android.widget.Switch" />

and now change your java code like this

final MenuItem toggleservice = menu.findItem(R.id.toggle_test);
Switch actionView=(Switch) MenuItemCompat.getActionView(toggleservice );
actionView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Start or stop your Service
}
});

Add listener to switch in action bar

Wrong:

Because you can't find View directly as you have taken Action layout in your item.

switch1 = (Switch) findViewById(R.id.switch1);

Correct:

Switch switch1= (Switch)menu.findItem(R.id.myswitch).getActionView().findViewById(R.id.switch1);


Related Topics



Leave a reply



Submit