1

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

SANDHYA
  • 771
  • 7
  • 18
  • 27
  • Maybe [this](http://www.tutorialforandroid.com/2009/01/changing-screen-brightness.html) tutorial will help you. – Praveenkumar May 30 '12 at 05:45
  • 1
    Possible duplicate : http://stackoverflow.com/questions/7646865/changing-screen-brightness-programmatically-as-with-the-power-widget – Kazekage Gaara May 30 '12 at 05:46
  • 1
    another possible duplicate http://stackoverflow.com/questions/1791340/adding-screen-brightness-controls-to-android-application – Tschegewara May 30 '12 at 05:51
  • possible duplicate of [changing screen brightness programmatically in android](http://stackoverflow.com/questions/3737579/changing-screen-brightness-programmatically-in-android) – gideon May 30 '12 at 06:08

2 Answers2

3
WindowManager.LayoutParams lparams = getWindow().getAttributes();  
lparams.dimAmount=1.0f; 
dialog.getWindow().setAttributes(lparams);  

The dim amount 0 means no dimming, and the dim amount 1.0f means complete dimming. Any value in between is the corresponding percentage of dim.

Just add this code to the button you want.

cytinus
  • 5,467
  • 8
  • 36
  • 47
  • @SherazKhilji call dialog.setContentView before setting dimAmount – jjyao Feb 16 '14 at 13:56
  • @jjyao i have solved this problem. The thing was that i was in the background thread when i was calling this but once i moved this piece of code into the UI thread then it worked. Thanks – Sheraz Ahmad Khilji Feb 17 '14 at 04:25
0

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!

Dhruv Gairola
  • 9,102
  • 5
  • 39
  • 43