Android Home And Lock Screen Widget - (Temperature Widget)

Create TemperatureWidget class and register with Temperature sensor intend
Download Source
package com.javaorigin.widget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.RemoteViews;

package com.javaorigin.widget;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.RemoteViews;

public class TemperatureWidget extends AppWidgetProvider {
 double temp=0;
 @Override
 
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
   int[] appWidgetIds) {
  context.getApplicationContext().registerReceiver(this,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  updateUI(context) ;
 }

 @Override
 public void onReceive(Context context, Intent intent) {  
  temp=intent.getIntExtra("temperature", 0)/10.0D;
  updateUI(context) ;
  super.onReceive(context, intent);
 }

 private void updateUI(Context context) {
  RemoteViews thisViews = new RemoteViews(context.getApplicationContext() .getPackageName(), R.layout.widget_layout);
  thisViews.setTextViewText(R.id.update, temp + "");

  ComponentName thisWidget = new ComponentName(context,TemperatureWidget.class);
  AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,thisViews);
 }
}

Create widget_layout.xml under res/layout folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dip"
    android:background="@drawable/myshape" >

    <TextView
        android:id="@+id/update"
        style="@android:style/TextAppearance.Medium"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center_horizontal|center_vertical"
        android:layout_margin="4dip"
        android:text="Static Text" >
    </TextView>

</LinearLayout> 
Create myshape.xml under res/drawable folder

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="#FFFFFFFF" />

    <gradient
        android:angle="225"
        android:endColor="#DD2ECCFA"
        android:startColor="#DD000000" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

</shape>
Create widget_info.xml under res/xml folder ,We can create this xml file using " File-->New --> Android xml file " menu with resource type is AppWidget Provider option ,then set attribute android:widgetCategory="keyguard|home_screen"
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    android:initialLayout="@layout/widget_layout"
    android:minHeight="20dp"
    android:minWidth="20dp"
    android:updatePeriodMillis="30000" android:widgetCategory="keyguard|home_screen">  
    

</appwidget-provider>

Configure android manifest xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javaorigin.widget"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
           <receiver android:name="TemperatureWidget" >
            <intent-filter >
                <action 
                    android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>
    </application>

</manifest>

10 comments :

  1. i have one suggestion.need explanation about the code and what u does

    ReplyDelete
  2. Good job man.
    http://www.hifive-studio.com/

    ReplyDelete
  3. i am facing the issue while running this code Like:-01 08:03:01 - TemperatureWidget] \TemperatureWidget\bin\TemperatureWidget.apk installed on device
    [2015-04-01 08:03:01 - TemperatureWidget] Done!

    ReplyDelete
  4. i have one project i want to put my app icon on the lock screen how it is possible plz help me my id is aashusonava@gmail.com

    ReplyDelete
    Replies
    1. For more details contact me at akhilgite@gmail.com

      Delete
  5. i have a suggestion that u also have to show the image of its output.

    ReplyDelete
  6. free android app source codes available in www.oortech.in

    ReplyDelete
  7. Good blog for newly developer..easy way to approach the Android Home And Lock Screen Widget.Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai | Android Training

    ReplyDelete