Set/Change screen brightness in android programmatically using seekbar

How to create app to increase-decrease current mobile device brightness with seekbar movement.

In this tutorial we are changing the android mobile phone device brightness dynamically with the use of seekbar. First we are detecting the current brightness status using Settings.System.getInt() method. Then we are adding setOnSeekBarChangeListener() to seekbar and on seekbar moment we have changing the brightness of android mobile phone. So here is the complete step by step tutorial for Set/Change screen brightness in android programmatically using seekbar.

android-project-download-code-button

How to Set/Change screen brightness in android programmatically using seekbar.

Note: Please add WRITE_SETTINGS permission to your AndroidManifest.xml file.

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

Code for MainActivity.java file.

package com.setscreenbrightness_android_examples.com;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.provider.Settings;

public class MainActivity extends Activity {

 SeekBar seekbar;
 TextView progress;
 Context context;
 int Brightness;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 seekbar = (SeekBar)findViewById(R.id.seekBar1);
 progress = (TextView)findViewById(R.id.textView1);
 context = getApplicationContext();
 
 //Getting Current screen brightness.
 Brightness = Settings.System.getInt(context.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,0
 );
 
 //Setting up current screen brightness to seekbar;
 seekbar.setProgress(Brightness);
 
 //Setting up current screen brightness to TextView;
 progress.setText("Screen Brightness : " + Brightness);
 
 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override
 public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
 
 progress.setText("Screen Brightness : " + i);

 //Changing Brightness on seekbar movement.

 Settings.System.putInt(context.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,i
 );
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {

 }

 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {

 }
 });
 
 }
}

Code for activity_main.xml file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.setscreenbrightness_android_examples.com.MainActivity"
 android:id="@+id/relativelayout" >

 <SeekBar
 android:id="@+id/seekBar1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:max="255" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/seekBar1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="16dp"
 android:text="Progress"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.setscreenbrightness_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshot:

Set/Change screen brightness in android programmatically using seekbar

Click here to download Set/Change screen brightness in android programmatically using seekbar project with source code.

Leave a Reply

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