This video shows the steps to write your code to control and set the brightness level of your phone screen through your custom Android App. The brightness level of your phone screen is an integer between 0 and 255.
For any query, suggestions or appreciations, we will be glad to hear from you at: programmerworld1990@gmail.com
Source Code:
package com.example.mybrightnesscontrol;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.EditText;public class MainActivity extends AppCompatActivity {
private EditText editText;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);if(!Settings.System.canWrite(this)){
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse(“package:” + this.getPackageName()));
startActivity(intent);
}
editText = findViewById(R.id.editText);
}public void setButton(View view){
ContentResolver contentResolver = this.getApplicationContext().getContentResolver();
Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, Integer.parseInt(editText.getText().toString()));
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mybrightnesscontrol”><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=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”><EditText
android:id=”@+id/editText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”108dp”
android:layout_marginTop=”99dp”
android:layout_marginEnd=”90dp”
android:layout_marginBottom=”301dp”
android:ems=”10″
android:hint=”@string/enter_the_brightness_level”
android:inputType=”textPersonName”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/button”
android:autofillHints=”” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”158dp”
android:layout_marginTop=”239dp”
android:layout_marginEnd=”165dp”
android:layout_marginBottom=”98dp”
android:onClick=”setButton”
android:text=”@string/set”
app:layout_constraintBottom_toTopOf=”@+id/editText”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /></androidx.constraintlayout.widget.ConstraintLayout>