Search code examples
androidbuttonscreen-brightness

How to dim a screen on click of a button in android?


How do i darken/dim the current screen on click of a button. PLease help me.


Solution

  • Here's one solution, though it may not be the best:

    create styles.xml under res/values. Add the following code:

    <style name="Theme.Translucent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@color/cache_color</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
     </style>
    

    Note that @color/cache_color is <color name="cache_color">#00000000</color>. Now clicking your custom button should send an intent to an activity (e.g., FooActivity). So declare this in your manifest:

    <activity android:name="com.FooActivity" android:theme="@style/Theme.Translucent"></activity>
    

    and voila, the screen is dimmed when your button's listener is called!