Janak
Janak

Reputation: 615

How to set screen brightness inside android application locally

I want to set the brightness of my android application only and not my phone. How do I change the brightness of my android application such that it does not effect my mobile phone brightness?

Upvotes: 2

Views: 6999

Answers (2)

Sagar Pilkhwal
Sagar Pilkhwal

Reputation: 3993

Get and Save the current brightness of your device, then change the brightness of the device(when your app starts running), and when your application closes revert back to the original brightness using the saved brightness level.

To Get Screen Brightness Level:

int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);

To Set Screen Brightness Level:

android.provider.Settings.System.putInt(getContext().getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, value); //<-- 1-225

App manifest permissions:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

or

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = curBrightnessValue/100.0f; //<-- your value here
getWindow().setAttributes(layoutParams);

here is a tutorial link

Another SO Post Link

P.S: you will have to handle all events like onPause(), onResume(), onBackPressed() etc

Upvotes: 3

SANU
SANU

Reputation: 202

No need to give any permission only set Following Params in your Seekbar's Method

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
      // TODO Auto-generated method stub
      float BackLightValue = (float)arg1/100;
      BackLightSetting.setText(String.valueOf(BackLightValue)); // BackLignt is Textview to display value

      WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); // Get Params
      layoutParams.screenBrightness = BackLightValue; // Set Value
      getWindow().setAttributes(layoutParams); // Set params


     }

Upvotes: 8

Related Questions