Cdacians

Cdacians
Cdacians

Sunday, 26 February 2012

Brightness Example in Android


Description:
This example shows how you can change brightness settings on device through coding.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it BrightnessExample.
2.) Set write permission to devic’s settings in manifest file. Write following into your manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example"
   android:versionCode="1"
   android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="10" />
    <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name" >
        <activity
           android:label="@string/app_name"
           android:name=".BrightnessExample" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
        <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
</manifest>
3.) Write following into your main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
    <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:id="@+id/backlightsetting"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       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>
4.) Run for output.
Steps:
1.) Create a project named BrightnessExample and set the information as stated in the image.
Build Target: Android 2.3.3
Application Name: BrightnessExample
Package Name: com.example
Activity Name: BrightnessExample
Min SDK Version: 10
2.) Open BrightnessExample.java file and write following code there:
package com.example;
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 BrightnessExample 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(newButton.OnClickListener(){
          @Override
          public void onClick(View arg0) {
           int SysBackLightValue = (int)(BackLightValue * 255);
           android.provider.Settings.System.putInt(getContentResolver(),
             android.provider.Settings.System.SCREEN_BRIGHTNESS,
             SysBackLightValue);
         
          }});
      BackLightControl.setOnSeekBarChangeListener(newSeekBar.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) {
          }
          @Override
          public void onStopTrackingTouch(SeekBar arg0) {
          }});
          }
        }
3.) Compile and build the project.
Output

Thanks
akm
www.cdacians.com

No comments:

Post a Comment