0

I followed the procedure explained on the link bellow but for some reason the switch is never displayed. I changed the visibility to match my app namespace as suggested in other threads to no avail. The procedure works fine if I try adding Icon's or regular buttons.

Thanks a lot.

I'm running it on a Nexus 4 with Android 4.4.4

How to add a switch to android action bar?

Here is my 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>
Community
  • 1
  • 1
Andre
  • 371
  • 4
  • 12
  • Did you try using a `RelativeLayout` instead of `LinearLayout`? – Sash_KP Sep 10 '14 at 18:24
  • here is a different approch to add switch into [the actionbar](http://stackoverflow.com/q/15338471/3326331) – Sagar Pilkhwal Sep 10 '14 at 18:26
  • @Sash_KP - Yes. First I tried the RelativeLayout as from the link. – Andre Sep 10 '14 at 18:31
  • @Sagar - I have tried that too and the menu only displays "Service". Is it possible that there is a problem with the Action Bar layout itself? (assuming that this makes sense). – Andre Sep 10 '14 at 18:37

1 Answers1

0

Ok, So I finally found something that works for me.

I created a 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 my 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 you go.

It's impressive how simple things can be so complicated on Android sometimes.

Thanks for the suggestions.

Andre
  • 371
  • 4
  • 12