Breaking News


Thursday, 13 February 2014

Android Example - Adjust Screen Brightness

  Screen Brightness in Android can be controlled programmatically in simple way. This tutorial explains the process with an example. Native brightness control in Android device has a Seekbar that adjust the screen brightness. The below tutorial has UI in a similar way.You take a look at SeekBar tutorial here  if needed.

 android.provider.Settings.System - This class contains miscellaneous  system preferences. There are convenience functions for accessing individual system settings.

android.provider.Settings.System.getInt - Convenience function for retrieving a single system settings value as an integer. Note that internally setting values are always stored as strings; this function converts the string to an integer for you. The default value will be returned if the setting is not defined or not an integer.

android.provider.Settings.System.SCREEN_BRIGHTNESS - String that holds Screen Brightness value ranging from 0 to 255.

Get Current Screen_Brightness Value
                The above functions and constants can be used to get screen brightness as shown in the code snippet.This example  has a seekbar defined with maximum value as 255, since the screen brightness constant varies from 0 to 255.
float curBrightnessValue = 0;
curBrightnessValue=android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);

The obtained value should be set to setProgress method of seekbar as shown below ,

 seekBar.setProgress(screen_brightness);

Set/Put Screen_Brightness Value 
               In the same way as get, we can also put value to set Screen_Brightness as shown in the below code snippet
android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, progress);

progress is the current SeekBar progress, in this way SeekBar and Screen_Brightness will be synced.

Android Permission to Change Screen Brightness
                Since we are modifying system settings we need to add permissions in AndroidManifest.xml to modify Screen_Brightness.
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
          
activity_main.xml

<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"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:max="10" />
</RelativeLayout>


MainActivity.java

package com.example.android_adjustbrightness;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

  private SeekBar seekBar;

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  seekBar = (SeekBar) findViewById(R.id.seekBar1);
  seekBar.setMax(255);

   float curBrightnessValue = 0;
  try {
   curBrightnessValue = android.provider.Settings.System.getInt(
     getContentResolver(),
     android.provider.Settings.System.SCREEN_BRIGHTNESS);
  } catch (SettingNotFoundException e) {
   e.printStackTrace();
  }

   int screen_brightness = (int) curBrightnessValue;
  seekBar.setProgress(screen_brightness);
  seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   int progress = 0;

    @Override
   public void onProgressChanged(SeekBar seekBar, int progresValue,
     boolean fromUser) {
    progress = progresValue;
   }

    @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
    // Do something here,
    // if you want to do anything at the start of
    // touching the seekbar
   }

    @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
    android.provider.Settings.System.putInt(getContentResolver(),
      android.provider.Settings.System.SCREEN_BRIGHTNESS,
      progress);
   }
  });
 }

  @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android_adjustbrightness"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.WRITE_SETTINGS" >
    </uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android_adjustbrightness.MainActivity"
            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>

</manifest>


Source code of this Application :
Androd_AdjustScreenBrightness.zip
Share This
Blogger
Facebook
Disqus

comments powered by Disqus

No comments :

Post a Comment

Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids