Tehleel Mir
Tehleel Mir

Reputation: 853

How to set a switch in android disabled and also have a grayish color so that user can understand this switch is disabled

I tried

isEnabled = false 
isFocsable = false 
isClickabe = false 
isActivited = false

but nothing is giving that grayish color.

Upvotes: 0

Views: 617

Answers (2)

Sumit_VE
Sumit_VE

Reputation: 497

You can try this with the style. Like define a style in themes.xml

<style name="Switchtheme" parent="Theme.AppCompat.Light">
    
    <item name="colorControlActivated">Your color code</item>

    <!-- inactive thumb color -->
    <item name="colorSwitchThumbNormal">Your color code
    </item>

   
    <item name="android:colorForeground">Your color code
    </item>

In XML

<Switch
            android:layout_width="match_parent"
            android:checked="false"
            android:theme="@style/Switchtheme"
            android:layout_height="wrap_content"/>

Upvotes: 0

Karunesh Palekar
Karunesh Palekar

Reputation: 2345

You just need to make use of Material Switch from the Material Design Library . Add the material design dependency and make use of Switches from the Material Design library . The library automatically takes care of the scenario you have described . More information about the Switches by the Material Design library can be found here : https://material.io/components/switches

You just have to include the dependency of Material Design and use the following in your xml file

<com.google.android.material.switchmaterial.SwitchMaterial
        ...
    />

Upvotes: 2

Related Questions