home top ad

How to increase and decrease screen brightness programmatically in android studio

Hello friends today we are learn how to increase and decrease brightness of device programmatically.whose code is given below.


                                                                    
increase and decrease screen brightness programmatically


 
 activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="16dp"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <SeekBar
            android:id="@+id/brightnessControl"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="6dp"
            android:max="100"
            android:progress="50" />

        <TextView
            android:id="@+id/brightnessValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/brightnessControl"
            android:layout_centerInParent="true"
            android:layout_marginTop="20dp"
            android:padding="16dp"
            android:text="0.5"
            android:textColor="#000000"
            android:textSize="22sp" />
    </RelativeLayout>
</RelativeLayout>
Here you are shown a mainactivity.xml layout in which I use a textview and a seekbar.

     MainActivity.java 
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.WindowManager;
    import android.widget.SeekBar;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
        SeekBar seekBar;
        TextView textView;
        Float backLightValue = 0.5f;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            seekBar = findViewById(R.id.brightnessControl);
            textView = findViewById(R.id.brightnessValue);
            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
    
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    backLightValue = (float) progress / 100;
                    textView.setText(String.valueOf(backLightValue));
                    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
                    layoutParams.screenBrightness = backLightValue;
                    getWindow().setAttributes(layoutParams);
    
                }
    
                @Override
    
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
    
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
            });
        }
    }
    Here I fetch ui attributes like seekBar and Textview where we performed some oprations
    like seekbar.setOnSeekBarChangeListener whose contain two override method 
    onStartTrackingTouch(SeekBar seekBar) and onStopTrackingTouch.
    Here is the screenshot of how the app will work.
    Thank you

Post a Comment

0 Comments