Tuesday, May 22, 2012

Android Sample Code: Battery Widget

*If you are new to Android, you should read this post first: Sample Code Battery Status

For a widget to work, below are some important items:
  • AndroidManifest.xml must be altered
  • Must create a layout file for widget provider
  • Must create a layout for widget
  • Main class source code
First, let see the AndroidManifest.xml:
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.sample.batterystatuswidget"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk android:minSdkVersion="7" />  
   <application  
     android:icon="@drawable/widget_battery01"  
     android:label="@string/app_name" >  
     <receiver android:name=".BatteryStatusWidgetActivity" android:label="@string/app_name">  
       <intent-filter>  
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
       </intent-filter>  
       <meta-data android:name="android.appwidget.provider"  
                            android:resource="@xml/widget_main" />  
     </receiver>  
   </application>  
 </manifest>  
Take note that the different is on the android.appwidget.action.APPWIDGET_UPDATE and additional "<meta-data ..".

Next, since in the AndroidManifest.xml above it refers to the @xml/widget_main, then widget_main.xml must exist:
 <?xml version="1.0" encoding="utf-8"?>  
 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
   android:initialLayout="@layout/widget_layout"  
   android:minWidth="40dp"  
   android:minHeight="40dp"  
   android:updatePeriodMillis="30000">  
 </appwidget-provider>  
Take note that updatePeriodMillis is referring to how frequent the widget will trigger itself to be refreshed.

Next, since in widget_main.xml above it refers to @layout/widget_layout, then widget_layout.xml must exist:
 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/relativeLayout1"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:gravity="center" >  
     <ImageView  
       xmlns:android="http://schemas.android.com/apk/res/android"  
       android:id="@+id/imageView1"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_alignParentLeft="true"  
       android:layout_alignParentTop="true"  
       android:src="@drawable/widget_batt_frame01" />  
           <LinearLayout  
             xmlns:android="http://schemas.android.com/apk/res/android"  
             android:id="@+id/layout"  
             android:layout_width="wrap_content"  
             android:layout_height="wrap_content"  
             android:layout_centerHorizontal="true"  
             android:layout_centerVertical="true"  
             android:layout_margin="10dip"  
             android:gravity="center_vertical"  
             android:orientation="vertical"  
             android:paddingTop="0dip" >  
             <TextView  
               android:id="@+id/widget_text"  
               style="@android:style/TextAppearance.Medium"  
               android:layout_width="wrap_content"  
               android:layout_height="0dp"  
               android:layout_gravity="center_vertical"  
               android:layout_weight="1"  
               android:gravity="center_horizontal|center_vertical"  
               android:text="@string/battery_level"  
               android:textSize="12dp"  
               android:textColor="#000000"/>  
           </LinearLayout>  
 </RelativeLayout>  
This layout will create a drawable image and a text on top of it. In this case, the text will display the remaining battery in percentage.

Finally, the source code for the main class:
 public class BatteryStatusWidgetActivity extends AppWidgetProvider {  
   /** Called when the activity is first created. */  
      private String batteryLevel = "init";  
      private int widgetImageFrame = R.drawable.widget_batt_frame01;  
      @Override  
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
           context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));  
           updateView(context);  
      }  
      @Override  
      public void onReceive(Context context, Intent intent) {  
           int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);  
           int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  
           int level = -1;  
           level = (rawlevel * 100) / scale;  
           batteryLevel = Integer.toString(level);  
           widgetImageFrame = R.drawable.widget_batt_frame01;  
           updateView(context);  
           super.onReceive(context, intent);  
      }  
      public void updateView(Context context) {  
           RemoteViews thisViews = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.widget_layout);  
           thisViews.setTextViewText(R.id.widget_text, batteryLevel);  
           thisViews.setImageViewResource(R.id.imageView1, widgetImageFrame);  
           ComponentName thisWidget = new ComponentName(context, BatteryStatusWidgetActivity.class);  
           AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, thisViews);  
      }  
Take note the following:
  • updateView(context) function is needed to refresh the widget
  • R.drawable.widget_batt_frame01 is referring to the image that registered to layout widgetImageFrame

Happy Coding Android!

That's all for now!
*I use Code Formatter to place codes in this post.

*UPDATE*
You may download full source code here: Android Apps

:: aerobrainTech ::

4 comments:

  1. Thanks For Sharing..........<a href= "http://pcsoftwarez.com/earthview-5-5-33-crack-patch/ >EarthView 5.5.33 Crack Patch </a>

    ReplyDelete
  2. Very informative and It was an awesome post. I love reading your fantastic content. Thanks for sharing it with us. We are so greatful to your sharing. EarthView 5.5.33

    ReplyDelete