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>
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.