Skip to main content

Change system screen brightness, using android.provider.Settings.System.SCREEN_BRIGHTNESS

Change system screen brightness, using android.provider.Settings.System.SCREEN_BRIGHTNESS



android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);

Change system screen brightness

To access Settings.System.SCREEN_BRIGHTNESS, we have to modify AndroidManifest.xml to grant premission of "android.permission.WRITE_SETTINGS".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exercise.AndroidScreenBrightness"
    android:versionCode="1"
    android:versionName="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".AndroidScreenBrightness"
                android:label="@string/app_name">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>

  </application>
  <uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
</manifest>


main code
package com.exercise.AndroidScreenBrightness;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class AndroidScreenBrightness extends Activity {
  /** Called when the activity is first created. */

float BackLightValue = 0.5f; //dummy default value

  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    
      SeekBar BackLightControl = (SeekBar)findViewById(R.id.backlightcontrol);
      final TextView BackLightSetting = (TextView)findViewById(R.id.backlightsetting);
      Button UpdateSystemSetting = (Button)findViewById(R.id.updatesystemsetting);
    
      UpdateSystemSetting.setOnClickListener(new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
  
   int SysBackLightValue = (int)(BackLightValue * 255);
  
   android.provider.Settings.System.putInt(getContentResolver(),
     android.provider.Settings.System.SCREEN_BRIGHTNESS,
     SysBackLightValue);
  
  }});
    
      BackLightControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

  @Override
  public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
   // TODO Auto-generated method stub
   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) {
   // TODO Auto-generated method stub
  
  }

  @Override
  public void onStopTrackingTouch(SeekBar arg0) {
   // TODO Auto-generated method stub
  
  }});
  }
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Set BackLight of the App"
  />
<SeekBar
  android:id="@+id/backlightcontrol"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_margin="10px"
  android:max="100"
  android:progress="50"
  />
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/backlightsetting"
  android:text="0.50"
  />
<Button
  android:id="@+id/updatesystemsetting"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Update Settings.System.SCREEN_BRIGHTNESS"
  />
</LinearLayout>

Comments

Popular posts from this blog

ProgressDialog example using handler android

final ProgressDialog dialog = ProgressDialog . show ( this , "Title" , "Message" , true ); final Handler handler = new Handler () { public void handleMessage ( Message msg ) { dialog . dismiss (); } } ; Thread t = new Thread () { public void run () { // write process code here. handler . sendEmptyMessage ( 0 ); } } ; t . start ();

How to set image button click effect ?

I have one simple solution: using that, you can set the default press effect to the button. // Create button and add event Button addImageView = (Button) bottomView.findViewById(R.id. addImage ); addImageView.setOnClickListener( new OnClickListener () { @Override public void onClick(View v) { // click event } }); //Using this code set touch listener addImageView.setOnTouchListener( touchEffect ); //touch listner OnTouchListener touchEffect = new OnTouchListener() {         public boolean onTouch(View v, MotionEvent event) {             switch (event.getAction()) {                 case MotionEvent. ACTION_DOWN : {                     v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode. SRC_ATOP );                     v....

Store array of String or int or list in SharedPreferences android

 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences( this );         JSONArray arr = new JSONArray ();         arr.put( "Viren" );         arr.put( "Android" );         prefs.edit().putString( "key" , arr.toString());         prefs.edit().commit();         try {             arr = new JSONArray (prefs.getString( "key" , "{}" ));             System. out .println(arr.getString(1));          } catch ( JSONException e) {             e.printStackTrace();         }