Home Screen Widgets In Android



For Widget Creation Follow the Bellow Steps:

1.Create New Project.
2.Create One Folder in res as   res/xml .
3.Create One .xml file as AppWidgetProvider  in xml folder  like res/xml/widgmail.xml
4.Create  one .java file in src/   with Extention of  AppWidgetProvider.
5.Open manifest file Add some code as follows.

Step 1:

Now , We Create One project as follows.

Project Name          : Widget For Gmail.
ApplicationName    :WidgetForGmail.
Package Name         :com.widget.gmail
Acticity                      : WidgetForGmailActivity.
miniSDK Vertion      : 8

Step 2: Create one Folder named as xml in res/

Step 3: Create one xml file in xml folder as AppWidgetProvider named as widgmail.xml as follows


Then Click On thi Finish Button, and the write the following  code in widgmail.xml file 

res/xml/widgmail.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="58px"
  android:minHeight="38px"
  android:initialLayout="@layout/main">
</appwidget-provider>

Then Open The main.xml file write the following code:

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="58px"
    android:layout_height="38px"
    android:id="@+id/icon1"
    android:background="@drawable/gmail"
    >
</LinearLayout>

Step 4:Create  one .java file in src/   with Extention of  AppWidgetProvider, then write the following code in widgmail.java file as follows.

src/widgmail.java

package com.widget.gmail;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class WidGmail extends AppWidgetProvider {

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                  int[] appWidgetIds) {

            final int N = appWidgetIds.length;
            // Log.i("ExampleWidget","Updating widgets " +
            // Arrays.asList(appWidgetIds));

            for (int i = 0; i < N; i++) {
                  int appWidgetId = appWidgetIds[i];
                  // Create an Intent to launch ExampleActivity

                  Intent intent = new Intent(context, WidgetForGmailActivity.class);
                  PendingIntent p = PendingIntent.getActivity(context, 0, intent, 0);
                  RemoteViews views = new RemoteViews(context.getPackageName(),
                              R.layout.main);
                  views.setOnClickPendingIntent(R.id.icon1, p);

                  // TODO Auto-generated method stub
                  appWidgetManager.updateAppWidget(appWidgetId, views);
            }
      }
}





Step 5:

The Open the  WidgetForGmailActivity .java file  write the following code

package com.widget.gmail;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class WidgetForGmailActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
        Uri uri = Uri.parse("http://www.google.com");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
       
    }
}

Step 6:  Now , Open the androidmanifest.xml file write the following code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.widget.gmail" android:versionCode="1" android:versionName="1.0">
      <uses-sdk android:minSdkVersion="8" />

      <application android:icon="@drawable/gmai" android:label="@string/app_name">
            <activity android:name=".WidgetForGmailActivity"
                  android:label="@string/app_name">
                  <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
            </activity>
            <receiver android:name=".WidGmail" android:label="Gmail">
                  <intent-filter>
                        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                  </intent-filter>
                  <meta-data android:name="android.appwidget.provider"
                        android:resource="@xml/widgmail" />

            </receiver>
      </application>
</manifest>


Then Run the Application ,and perform long press on Home screen ,It appear one window, In This Window you Select Widgets and then Select Gmail Widget.
finally One Gmail icon is appear in Home Screen.

if you will click on that icon it will open Gamil login page. 



Comments

Popular posts from this blog

How To Run Compatibility Test Suite (CTS) ?

NFC : Reading a MiFare Classic 1K from Android Devices

Dalvik Virtual machine VS Java Virtual Machine: