Android Widget Start
How to make widget?
What is difference between general Android application and Widget?
First step of Widget
- Create class which extends AppWidgetProvider
- Create layout of widget(same as Android layout xml)
- Create widget definition xml
- Edit manifest for Widget
What can we use?
Widget has some limitation to use layout and widget classes.
Layout
- FrameLayout
- LinearLayout
- RelativeLayout
- GridLayout
UI Components
- AnalogClock
- Button
- Chronometer
- ImageButton
- ImageView
- ProgressBar
- TextView
- ViewFlipper
- ListView
- GridView
- StackView
- AdapterViewFlipper
Sample
First simple widget.
AppWidgetProvider class(MainWidget.java)
public class MainWidget extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Set remote view RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main); rv.setTextViewText(R.id.word, "Hello"); // Set view to widget ComponentName cn = new ComponentName(context, MainWidget.class); appWidgetManager.updateAppWidget(cn, rv); } }
Layout res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/word" android:layout_width="fill_parent" android:layout_height="match_parent" android:textSize="30sp" android:textAlignment="center" android:gravity="center" android:text="Hello" /> </LinearLayout>
Widget definition(res/xml/mainwidget.xml)
Widget definition is widget configuration file.
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="72dip" android:minHeight="72dip" android:updatePeriodMillis="1800000" android:initialLayout="@layout/main" />
Parameters are very important.
Widget has special size for it. 1 tile is 72dip.
updatePeriodMillis is update time(The minimum is 30 minutes).
AndroidManifest.xml
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <receiver android:name="com.atmarkplant.widgetsample.MainWidget" android:label="MainWidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/mainwidget" /> </receiver> </application>
You need receiver tag for Widget.
Name is your class name(with package), label is display name.
Resource is our Widget definition xml file.
We don’t need any Activity in this widget application.
Run
To run this application, “Run” -> “Debug As” -> “Android Application” from eclipse
Install widget and you can see from application or widget list.
Select widget and drag it to your home, and this widget runs.
Good day! I could have sworn I’ve visited this blog before but after browsing through many of the articles I realized it’s new to me. Regardless, I’m definitely happy I found it and I’ll be bookmarking it and checking back frequently!
Thank you for another great article. Where else may just anybody get that kind of information in such an ideal method of writing? I’ve a presentation subsequent week, and I am at the look for such information.
Wonderful post but I was wondering if you could write a litte more on this topic? I’d be very grateful if you could elaborate a little bit further. Cheers!
WOW just what I was looking for. Came here by searching for meta_keyword
I have read so many articles or reviews concerning the blogger lovers however this article is actually a nice article, keep it up.