android - How to add material design library switch in the action bar -


how insert com.gc.materialdesign.views.switch in actionbar using menuinflater??

below code inflating android default switch.

this oncreateoptionsmenu

@override public boolean oncreateoptionsmenu(menu menu)    {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.menu_main, menu);     menuitem item = menu.finditem(r.id.myswitch);     switchbutton = (switch)item.getactionview();    } 

this menu_main.xml

<menu       xmlns:app="http://schemas.android.com/apk/res-auto"                          xmlns:android="http://schemas.android.com/apk/res/android"     >      <item         android:id="@+id/myswitch"         android:title="off/on"         app:showasaction="always"         app:actionlayout="@layout/switchlayout"         app:actionviewclass="android.widget.switch"         /> </menu> 

this switch layout

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="fill_parent"     android:layout_height="match_parent"     android:orientation="horizontal">      <switch         android:layout_centervertical="true"         android:id="@+id/myswitch"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="off/on toasts" /> </relativelayout> 

any appreciated!

replace switch code 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"/> 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -