Android - Adding Switch to the Action Bar not working -
i followed procedure explained on link bellow reason switch never displayed. changed visibility match app namespace suggested in other threads no avail. procedure works fine if try adding icon's or regular buttons.
thanks lot.
i'm running on nexus 4 android 4.4.4
how add switch android action bar?
here current xml contents.
/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.testapp.mainactivity" > <item android:id="@+id/myswitch" android:title="" app:showasaction="always" android:actionlayout="@layout/switch_layout" /> </menu>
/layout/switch_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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="" /> </linearlayout>
ok, found works me.
i created layout file :/layout/text_layout.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" > <!-- android:layout_height="50dp" --> <switch android:id="@+id/switchforactionbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" /> </relativelayout>
then on mainactivity.java
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); actionbar mactionbar = getactionbar(); layoutinflater minflater = layoutinflater.from(this); view mcustomview = minflater.inflate(r.layout.test_layout, null); mactionbar.setcustomview(mcustomview); mactionbar.setdisplayshowcustomenabled(true); }
booommm, there go.
it's impressive how simple things can complicated on android sometimes.
thanks suggestions.
Comments
Post a Comment