Introduction
This article explains how to use a seek bar to change the screen brightness in Android Studio.
First, you need to use a SeekBar in an XML file and create its id in a Java class file. Now get the content resolver object by calling getContentResolver() and the current window by calling the getWindow() methods. To get the brightness of a screen use the getint() method that returns the current brightness of a system. You will the brightness to the setProgress() method as an argument. Now set the seekbar on setOnSeekBarChangeListener() to change the brightness of a screen.
In the onProgress() method set the progress of the seek bar in percentage.
Step 1
Now create an XML file and write this:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#475d5d"
- android:orientation="vertical" >
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_margin="5dp"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#354d5d"
- android:orientation="vertical">
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="BrighnessControl"
- android:layout_marginLeft="5dp"
- android:layout_marginTop="10dp"
- android:textColor="#ffffff"
- android:textStyle="bold">
- </TextView>
- <SeekBar
- android:layout_width="300dp"
- android:layout_height="wrap_content"
- android:id="@+id/seekBar"
- android:progress="0"
- android:max="100"
- android:layout_marginTop="20dp"
- android:indeterminate="false"
- />
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="25dp"
- android:background="#354d5d"
- android:orientation="horizontal"
- android:layout_marginTop="20dp">
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="0"
- android:textStyle="bold"
- android:textSize="12dp"
- android:textColor="#ffffff"
- android:layout_marginLeft="25dp"/>
- <TextView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:textSize="12dp"
- android:text="20"
- android:textColor="#ffffff"
- android:layout_marginLeft="30dp"/>
- <TextView
- android:layout_height="wrap_content"
- android:textSize="12dp"
- android:layout_width="wrap_content"
- android:text="40"
- android:textColor="#ffffff"
- android:layout_marginLeft="32dp"/>
- <TextView
- android:layout_height="wrap_content"
- android:textSize="12dp"
- android:layout_width="wrap_content"
- android:text="60"
- android:textColor="#ffffff"
- android:layout_marginLeft="36dp"/>
- <TextView
- android:textSize="12dp"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="80"
- android:textColor="#ffffff"
- android:layout_marginLeft="38dp"/>
- <TextView
- android:textSize="12dp"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="100"
- android:textColor="#ffffff"
- android:layout_marginLeft="42dp"/>
- </LinearLayout>
- </LinearLayout>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=""
- android:id="@+id/txtPercentage"/>
- </LinearLayout>
Step 2
Now create a Java file and write this.
First, you need to use the SeekBar in the XML file and create its id in the Java class file. Now get the content resolver object by calling getContentResolver() and the current window by calling the getWindow() methods. To get the brightness of a screen use the getint() method that returns the current brightness of a system. You will the brightness to the setProgress() method as an argument. Now set the seekbar on setOnSeekBarChangeListener() to change the brightness of a screen.
In the onProgress() method set the progress of the seek bar in percentage.
Step 3
Add permission to the Android Manifest.xml file.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.androidseekbarexample"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
- <uses-sdk
- android:minSdkVersion="7"
- android:targetSdkVersion="16" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.androidseekbarexample.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>
Step 4
When you run your project:
Step 5
When you change the position of a bar:
Step 6
When you set it to 100%:
SubasG PradhanPosted Apr 27, 2014, 1:59 PM
I changed the brightness from the app, it also updated in notification area, but when i change the brightness from notification area it dont updated in the app