setting brightness through program


Last Updated: 2010-09-20 06:10:00
  1. anandkgowda

    anandkgowda New Member

    Hi..
    should any one knows how to control screen brightness through program for android device. ..i did one application but it works only for that window of a android device..i need control of brightness for the whole system not for single window..i posted my code here..please do the needful..please post full code..

    package com.anand.brightness;
    import android.app.Activity;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.view.View;
    import android.view.WindowManager;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.SeekBar;

    public class brightness_control extends Activity {
    private static final String BRIGHTNESS_PREFERENCE_KEY = "brightness";
    private View brightnessPanel;
    private SeekBar brightnessControl;
    private int brightness = 50;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    brightnessPanel = findViewById(R.id.panel);
    brightnessControl = (SeekBar) findViewById(R.id.seek);
    Button btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    showBrightnessPanel();
    }
    });
    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) {
    hideBrightnessPanel();
    }
    });
    }
    private void showBrightnessPanel() {
    Animation animation = AnimationUtils.loadAnimation(this,
    android.R.anim.slide_in_left);
    brightnessControl.setProgress(this.brightness);
    brightnessPanel.setVisibility(View.VISIBLE);
    brightnessPanel.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 hideBrightnessPanel() {
    Animation animation = AnimationUtils.loadAnimation(brightness_control.this,
    android.R.anim.slide_out_right);
    brightnessPanel.startAnimation(animation);
    brightnessPanel.setVisibility(View.GONE);
    PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(
    BRIGHTNESS_PREFERENCE_KEY, brightnessControl.getProgress())
    .commit();

    }
    }


    xml code:


    <?​
    xml version="1.0" encoding="utf-8"?>
    <
    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:eek:rientation="vertical" android:layout_width="fill_parent"

    android:layout_height="fill_parent" android:background="#3399CC">



    <Button android:text="Show Brightness Panel" android:id="@+id/Button01"

    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>



    <LinearLayout android:id="@+id/panel" android:eek:rientation="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="gone">


    <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>


    Advertisement

Share This Page