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 in real physical android device, if you run it in an android emulator, it will not take effect.

1. Change Screen Brightness Example Demo.

First let us look at the video. There are two buttons in 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.

Because change screen brightness action need 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 article How To Grant Write Settings Permission In Android for more information.

After click the button, you can also click Settings —> Display —> Brightness Level to see the changed value.

android min screen brightness valueandroid max screen brightness value

2. Change Android Screen Brightness Source Code.

2.1 Layout Xml File.

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.

The key code which is used to change the device screen brightness value is :

Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, screenBrightnessValue);

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.

<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
(Visited 5,483 times, 1 visits today)
READ :   Android ListActivity Example

Leave a Reply

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.