Hello Everybody,
this sample help us to create Screen Brightness Controller.
1) Add sample Activity:
public class MainActivity extends Activity {
private static final String BRIGHTNESS_PREFERENCE_KEY = "brightness";
private View brightnessView;
private SeekBar brightnessControl;
private int brightness = 50;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.brightness);
brightnessView = findViewById(R.id.brightnessView);
brightnessControl = (SeekBar) findViewById(R.id.seek);
Button btn = (Button) findViewById(R.id.btnShowBrightness);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showBrightness();
}
});
brightnessControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setBrightness(progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
hideBrightness();
}
});
}
private void showBrightnessView() {
Animation animation = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
brightnessControl.setProgress(this.brightness);
brightnessView.setVisibility(View.VISIBLE);
brightnessView.startAnimation(animation);
}
private void setBrightness(int value) {
if (value < 10) {
value = 10;
} else if (value > 100) {
value = 100;
}
brightness = value;
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = (float) value / 100;
lp.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(lp);
}
private void hideBrightnessView() {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_out_right);
brightnessView.startAnimation(animation);
brightnessView.setVisibility(View.GONE);
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(BRIGHTNESS_PREFERENCE_KEY, brightnessControl.getProgress()).commit();
}
}
2) Add layout to your brightness.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Show Brightness Control"
android:id="@+id/btnShowBrightness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/brightnessView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="10dip"
android:paddingBottom="30dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:gravity="center_horizontal"
android:visibility="visible">
<TextView
android:text="Brightness Level"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<SeekBar
android:id="@+id/seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="100"/>
</LinearLayout>
</LinearLayout>
3) Also please add
<uses-permission android:name="android.permission.HARDWARE_TEST"></uses-permission>
to your Manifest file.
Hello Pavel,
ReplyDeleteGreate article!
Thanks a lot for sharing knowledges!
Best regards,
Yahor
Great Article .... Thank you for sharing
ReplyDelete