I'm a newbie to this field of android development. These days i'm developing an app and I want to set the screen brightness to maximum level once I open the app, and set it back to the previous level once I exit the app. Can someone come up with the full source code for this? I read almost every thread in stackoverflow regarding this issue. And I couldn't understand where to put those suggested codes. But if u can come up with the full code, then I'll be able to understand everything. Thanks!
📲 On Android: Settings > Display > Tap the slider next to Adaptive brightness and switch it to the off position. Then, adjust the brightness bar until you've reached your desired level of brightness.
Adjust brightness on Android device On an unlocked device, swipe your finger down from the top of the screen twice. Press and hold your finger on the brightness slider (shown below) and drag left or right to adjust the brightness.
If you notice that your phone's screen gets dark and then brightens, it's due to the Adaptive brightness feature which changes the screen's brightness based on the amount of light detected. This feature is designed to help save battery power, but you can shut it off if it bothers you.
One of my friends sent me a better, simple code to fix this issue which he has found from the internet
For amateurs (like me), you have to enter below code after "setContentView(R.layout.activity_main);" in "protected void onCreate(Bundle savedInstanceState) {" method in your MainActivity.java
WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);
Btw, Thank you @AbdulKawee for your time and the support that u gave me with your code. really appreciate it :)
You can use this
public class MainActivity extends AppCompatActivity {
private int brightness=255;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cResolver = getContentResolver();
//Get the current window
window = getWindow();
try
{
// To handle the auto
Settings.System.putInt(cResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//Get the current system brightness
brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
}
catch (Settings.SettingNotFoundException e)
{
//Throw an error case it couldn't be retrieved
Log.e("Error", "Cannot access system brightness");
e.printStackTrace();
}
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
//Get the current window attributes
WindowManager.LayoutParams layoutpars = window.getAttributes();
//Set the brightness of this window
layoutpars.screenBrightness = brightness / (float)100;
//Apply attribute changes to this window
window.setAttributes(layoutpars);
}
}
And the most important permission in the manifest
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Asking for Write Settings permission in API 23
private boolean checkSystemWritePermission() {
boolean retVal = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
retVal = Settings.System.canWrite(this);
Log.d(TAG, "Can Write Settings: " + retVal);
if(retVal){
Toast.makeText(this, "Write allowed :-)", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
startActivity(intent);
}
}
return retVal;
}
Kotlin version of Sankha Rathnayake's answer:
val attributes = window.attributes
attributes.screenBrightness = 1f
window.attributes = attributes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With