brightness control in android source code
hello, android developer today I will tell you how to make a brightness control app using android studio. or how to use seekbar with your android activity.
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<android.support.constraint.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:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <SeekBar android:id="@+id/backlightcontrol" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10px" android:layout_marginTop="32dp" android:max="100" android:progress="50" app:layout_constraintBottom_toTopOf="@+id/backlightsetting" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/backlightsetting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginBottom="32dp" android:text="0.50" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/backlightcontrol" /> </android.support.constraint.ConstraintLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; import android.view.WindowManager; public class Changescreenbrightness extends AppCompatActivity { float BackLightValue = 0.5f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_changescreenbrightness); SeekBar BackLightControl = (SeekBar)findViewById(R.id.backlightcontrol); final TextView BackLightSetting = (TextView)findViewById(R.id.backlightsetting); BackLightControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { BackLightValue = (float)arg1/100; BackLightSetting.setText(String.valueOf(BackLightValue)); WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenBrightness = BackLightValue; getWindow().setAttributes(layoutParams); } @Override public void onStartTrackingTouch(SeekBar arg0) { } @Override public void onStopTrackingTouch(SeekBar arg0) { }}); } } |