Android Change Screen Brightness Programmatically Example

This example will show you how to change the android device screen brightness programmatically. You can only see the effect with this example on a real physical android device, if you run it in an android emulator, it will not take effect.

1. Change Screen Brightness Example Demo.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/0bRp8srWt-k

  1. First, let us look at the video. There are two buttons on the screen, when you click the first button, the screen brightness will be turn off, when you click the second button, the screen brightness will be changed to max value.
  2. Because change screen brightness action needs android.permission.WRITE_SETTINGS permission, so when you click the button for the first time, it will popup Can modify system settings panel to let you change the app permission manually. You can read the article How To Grant Write Settings Permission In Android for more information.
  3. After clicking the button, you can also click Settings —> Display —> Brightness Level to see the changed value.

2. Change Android Screen Brightness Programmatically Example Source Code.

2.1 Layout XML File.

  1. activity_change_screen_brightness.xml
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/turn_off_screen_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Turn Off Screen"/>
    
        <Button
            android:id="@+id/turn_on_screen_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Turn On Screen"/>
    
    </LinearLayout>

2.2 Activity Java File.

  1. The key code which is used to change the device screen brightness value is like below.
    Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, screenBrightnessValue);
  2. ChangeScreenBrightnessActivity.java
    package com.dev2qa.example.screen.brightness;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    
    import com.dev2qa.example.R;
    
    public class ChangeScreenBrightnessActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_change_screen_brightness);
    
            setTitle("dev2qa.com - Change Screen Brightness Example");
    
            // Get turn off screen button
            final Button turnOffScreenButton = (Button)findViewById(R.id.turn_off_screen_button);
            turnOffScreenButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    // Get app context object.
                    Context context = getApplicationContext();
    
                    // Check whether has the write settings permission or not.
                    boolean settingsCanWrite = hasWriteSettingsPermission(context);
    
                    // If do not have then open the Can modify system settings panel.
                    if(!settingsCanWrite) {
                        changeWriteSettingsPermission(context);
                    }else {
                        changeScreenBrightness(context, 1);
                    }
                }
            });
    
            // Get turn on screen button
            final Button turnOnScreenButton = (Button)findViewById(R.id.turn_on_screen_button);
            turnOnScreenButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    Context context = getApplicationContext();
    
                    // Check whether has the write settings permission or not.
                    boolean settingsCanWrite = hasWriteSettingsPermission(context);
    
                    // If do not have then open the Can modify system settings panel.
                    if(!settingsCanWrite) {
                        changeWriteSettingsPermission(context);
                    }else {
                        changeScreenBrightness(context, 255);
                    }
                }
            });
        }
    
        // Check whether this app has android write settings permission.
        private boolean hasWriteSettingsPermission(Context context)
        {
            boolean ret = true;
            // Get the result from below code.
            ret = Settings.System.canWrite(context);
            return ret;
        }
    
        // Start can modify system settings panel to let user change the write settings permission.
        private void changeWriteSettingsPermission(Context context)
        {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
            context.startActivity(intent);
        }
    
        // This function only take effect in real physical android device,
        // it can not take effect in android emulator.
        private void changeScreenBrightness(Context context, int screenBrightnessValue)
        {
            // Change the screen brightness change mode to manual.
            Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            // Apply the screen brightness value to the system, this will change the value in Settings ---> Display ---> Brightness level.
            // It will also change the screen brightness for the device.
            Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, screenBrightnessValue);
    
            /*
            Window window = getWindow();
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.screenBrightness = screenBrightnessValue / 255f;
            window.setAttributes(layoutParams);
            */
        }
    }

2.3 AndroidManifest.xml File.

  1. AndroidManifest.xml
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.dev2qa.example">
    
        <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".screen.brightness.ChangeScreenBrightnessActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
           
        </application>
    
    </manifest>

References:

  1. How To Grant Write Settings Permission In Android
  2. How To Enable USB Debugging Mode On Android Device

1 thought on “Android Change Screen Brightness Programmatically Example”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.