In this tutorial, we will be doing three operations:
1) Increase screen brightness,
2) Decrease screen brightness,
3) Keeping the screen awake (no permissions are needed in manifest)
----------------------------------- play_with_device_screen.xml ----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:gravity="center"
android:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:weightSum="3"
android:gravity="center">
<Button
android:id="@+id/pwdca_increaseBTN"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textSize="20sp"
android:text="+"
android:padding="5dp"/>
<Button
android:id="@+id/pwdca_awakeBTN"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textSize="20sp"
android:text="Stay Awake"
android:padding="5dp"/>
<Button
android:id="@+id/pwdca_decreaseBTN"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="-"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
-------------------------------------------------------------------
------------------------------------- PlayWithDeviceScreenActivity.java ------------------------------------
package com.app.yourappname;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class PlayWithDeviceScreenActivity extends AppCompatActivity implements View.OnClickListener {
private WindowManager.LayoutParams winLayoutParam;
private float brightness = 0.4f;
private boolean keepScreenAwake = false;
private Button stayAwakeBTN;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.play_with_device_screen_activity);
winLayoutParam = getWindow().getAttributes();
Button increaseBrightnessBTN = findViewById(R.id.pwdca_increaseBTN);
increaseBrightnessBTN.setOnClickListener(this);
Button decreaseBrightnessBTN = findViewById(R.id.pwdca_decreaseBTN);
decreaseBrightnessBTN.setOnClickListener(this);
stayAwakeBTN = findViewById(R.id.pwdca_awakeBTN);
stayAwakeBTN.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.pwdca_awakeBTN:
if(!keepScreenAwake) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
keepScreenAwake = true;
stayAwakeBTN.setVisibility(View.GONE);
}
break;
case R.id.pwdca_decreaseBTN:
if(brightness>=0)
brightness-=0.1f;
else
brightness=0.0f;
winLayoutParam.screenBrightness = brightness;
getWindow().setAttributes(winLayoutParam);
Toast.makeText(this, "Brightness: "+brightness, Toast.LENGTH_SHORT).show();
break;
case R.id.pwdca_increaseBTN:
if(brightness<=1.0f)
brightness+=0.1f;
else
brightness=1.0f;
winLayoutParam.screenBrightness = brightness;
getWindow().setAttributes(winLayoutParam);
Toast.makeText(this, "Brightness: "+brightness, Toast.LENGTH_SHORT).show();
break;
}
}
}
------------------------------------------------------
-------------------------------------- Declare Activity name in Manifest -------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourappname">
<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=".PlayWithDeviceScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------------------------------------