Hi .
Here we Discuss about how to create Simple Widget in android...
Here use AppWidgetProvider class .
1) res/layout/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:orientation="vertical"
android:layout_gravity="center"
android:background="@android:color/holo_orange_light"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome" />
<TextView android:id="@+id/widget_textview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="5dip"
android:padding="10dip"
android:text="Welcome Oak Tech."
android:textColor="@android:color/black"/>
</LinearLayout>
2) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="widget.co.in"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".WidgetActivity" 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/welcome" />
</receiver>
</application>
</manifest>
3) create xml folder in res . res/xml/welcome.xml
this .xml file create appwidget-provider tag for view widget.
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="@layout/main"
/>
4) Create Following Activity.
WidgetActivity.java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.RemoteViews;
public class WidgetActivity extends AppWidgetProvider {
}
5) Run Above Application and see in console
Uploading widget.apk onto device 'emulator-5554'
Installing widget.apk...
Success!
/widget/bin/widget.apk installed on device
Done!
6) Now Goto Emulator and Open widget (long press left click on home screen) . and See Widget window and longpress on widget icon for our application.
see Button and Textview in widget window.
output:
Here we Discuss about how to create Simple Widget in android...
Here use AppWidgetProvider class .
First Create Simple android project name is widget.
1) res/layout/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:orientation="vertical"
android:layout_gravity="center"
android:background="@android:color/holo_orange_light"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="welcome" />
<TextView android:id="@+id/widget_textview"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="5dip"
android:padding="10dip"
android:text="Welcome Oak Tech."
android:textColor="@android:color/black"/>
</LinearLayout>
2) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="widget.co.in"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".WidgetActivity" 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/welcome" />
</receiver>
</application>
</manifest>
3) create xml folder in res . res/xml/welcome.xml
this .xml file create appwidget-provider tag for view widget.
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="@layout/main"
/>
4) Create Following Activity.
WidgetActivity.java
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.RemoteViews;
public class WidgetActivity extends AppWidgetProvider {
}
5) Run Above Application and see in console
Uploading widget.apk onto device 'emulator-5554'
Installing widget.apk...
Success!
/widget/bin/widget.apk installed on device
Done!
6) Now Goto Emulator and Open widget (long press left click on home screen) . and See Widget window and longpress on widget icon for our application.
see Button and Textview in widget window.
output:
Comments
Post a Comment