18
votes

I am trying to use the latest appcompat which was updated for material design in my app to show the switch view as displayed in Lollipop(http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html), the problem is the switch is not getting displayed. My SDKmin is 14 and max is 21. I am using the below code in the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/fragment_scheduler"
    tools:context="com.stackoverflow.ranjith.androidprojdel.SchedulerUI">

<android.support.v7.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="switch"
        app:showText="false" />

    <Button
        android:layout_width="match_parent"
        android:text="start"
        android:id="@+id/start"
        android:layout_height="wrap_content" />
</LinearLayout>

Build.gradle:

dependencies {
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:palette-v7:+'
    compile 'com.android.support:support-v4:+'
    compile "com.android.support:appcompat-v7:21.0.+"
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
5
Is this the whole layout? What view is the SwitchCompat being added to? - ianhanniballake
No..there is also button within the layout. It is within linearlayout. I have added the entire layout - Psypher
Any views on how to get this to work? - Psypher
Try removing app:showText="false" and use android:text="" - k1slay
This is not working.. - Psypher

5 Answers

24
votes

I had same problem today but somehow it switchcompat worked in my sample. I think that there is problem with app style, it parent should be set to:

Theme.AppCompat
24
votes

I'm not sure if this is a bug in the support library, but you have to ensure that the context for your layout inflater is a themed one.

  1. Make sure your activities theme Theme.AppCompat as parent
  2. If you use inflate SwitchCompat in ListView or RecyclerView you have to ensure that the LayoutInflater you instantiate and use in your adapter is created with the themed context. You can retrieve the themed context by calling: Activity.getSupportActionBar().getThemedContext()
7
votes

Alternatively, you can utilize

android:theme="@style/Theme.AppCompat"

On your control

2
votes

I had a custom theme in my styles.xml and I forgot to specify its parent:

name="CustomTheme.switchState" parent="Theme.AppCompat.Light"

Something like this:

custom_linear_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...

<androidx.appcompat.widget.SwitchCompat
    android:id="@+id/switch1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:text="Hello"
    android:theme="@style/CustomTheme.switchState" />

...
</LinearLayout>

style.xml

<resources>
...

name="CustomTheme.switchState" parent="Theme.AppCompat.Light">
        <item name="colorControlActivated">@color/colorOne</item>
        <item name="android:textOn">On</item>
        <item name="android:textOff">Off</item>
    </style>

...
</resources>
0
votes

Seems like you've encountered https://code.google.com/p/android/issues/detail?id=78262

Copy the layout and pngs, fix the nine-patch, and you should be fine.