Android Screen Brightness Sample


Lets get start how to configure the Brightness settings in Android SmartPhones. In several native apps of Android it’s often required to peoperly set the brightness of the screen according to apps requirement.

So, to develop such an app , lets start in Eclipse by creating a new Android Project.

  •  Choose the Runtime environment of Android  & the package settings.

  • Lets modify the AndroidManifest.xml :

<? xmlversion=“1.0”encoding=“utf-8”?>

<manifest xmlns:android=http://schemas.android.com/apk/res/android&#8221;

package=“com.example.BrightnessSample”

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=“.BrightnessSampleActivity”>

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

  • Lets Modify the 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>

 

  • Next, add the following Code in BrightnessSampleAcitivity.java:

package  com.example.BrightnessSample;

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  BrightnessSampleActivity  extends  Activity {

/** Called when the activity is first created. */

floatBackLightValue = 0.5f;

@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()

{

        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(new     SeekBar.OnSeekBarChangeListener() {

          public void o nProgressChanged(SeekBar arg0,int arg1,boolean arg2)

{

        BackLightValue = (float)arg1/100;

BackLightSetting.setText(String.valueOf(BackLightValue));

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();

layoutParams. screenBrightness = BackLightValue;

  getWindow().setAttributes(layoutParams);

        }

public void onStopTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

}

public void  onStartTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

}});

}

}

  • Lets check the output of Screen Brightness Sample in Android  :

  • Increase the Progress Bar to check the View in Screen Brightness.

Advertisements

About Anindita
Anindita Basak is working as Big Data Cloud Consultant in Microsoft. Worked in multiple MNCs as Developer & Senior Developer on Microsoft Azure, Data Platform, IoT & BI , Data Visualization, Data warehousing & ETL & of course in Hadoop platform.She played both as FTE & v- employee in Azure platform teams of Microsoft.Passionate about .NET , Java, Python & Data Science. She is also an active Big Data & Cloud Trainer & would love share her experience in IT Training Industry. She is an author, forum contributor, blogger & technical reviewer of various books on Big Data Hadoop, HDInsight, IoT & Data Science, SQL Server PDW & PowerBI.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: