Android Change Screen Brightness Use Seekbar Example

This example will tell you how to use the android Seekbar to adjust the screen brightness. This example only takes effect on real physical android device, it will not take effect in the android emulator.

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

  1. When you drag the Seekbar, the android device screen brightness will be changed. And the current screen brightness value will be shown in the text view above the seekbar.

1. Layout XML File.

  1. activity_change_screen_brightness_use_seekbar.xml
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/change_screen_brightness_value_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="100dp"/>
    
        <SeekBar
            android:id="@+id/change_screen_brightness_seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"/>
    
    </LinearLayout>

2. Activity Java File.

  1. ChangeScreenBrightnessUseSeekbarActivity.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.widget.SeekBar;
    import android.widget.TextView;
    
    import com.dev2qa.example.R;
    
    public class ChangeScreenBrightnessUseSeekbarActivity extends AppCompatActivity {
    
        private static final String SCREEN_BRIGHTNESS_VALUE_PREFIX = "Current device screen brightness value is ";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_change_screen_brightness_use_seekbar);
    
            setTitle("dev2qa.com - Seekbar Change Screen Brightness Example.");
    
            // Get display screen brightness value text view object.
            final TextView screenBrightnessValueTextView = (TextView)findViewById(R.id.change_screen_brightness_value_text_view);
    
            // Get the seekbar instance.
            SeekBar seekBar = (SeekBar)findViewById(R.id.change_screen_brightness_seekbar);
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
    
                    Context context = getApplicationContext();
    
                    boolean canWriteSettings = Settings.System.canWrite(context);
    
                    if(canWriteSettings) {
    
                        // Because max screen brightness value is 255
                        // But max seekbar value is 100, so need to convert.
                        int screenBrightnessValue = i*255/100;
    
                        // Set seekbar adjust screen brightness value in the text view.
                        screenBrightnessValueTextView.setText(SCREEN_BRIGHTNESS_VALUE_PREFIX + 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);
                    }else
                    {
                        // Show Can modify system settings panel to let user add WRITE_SETTINGS permission for this app.
                        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        context.startActivity(intent);
                    }
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
            });
    
            //Getting Current screen brightness.
            int currBrightness = Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,0);
            // Set current screen brightness value in the text view.
            screenBrightnessValueTextView.setText( SCREEN_BRIGHTNESS_VALUE_PREFIX + currBrightness);
            // Set current screen brightness value to seekbar progress.
            seekBar.setProgress(currBrightness);
        }
    }

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.ChangeScreenBrightnessUseSeekbarActivity">
                <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
  3. 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.