In this article I would like to share the experience of creating a simple vidzhetika for Android, and it is a small reminder. If you put this in Google: "Create a widget for Android", then the search will produce a lot of information. But you did not find the exact information with examples of how to make this widget. That's why I decided to open this section "Programming" and write the first article.
If you do it right, then your creation will look like this:
First, download the program Eclipse!
Start writing the first widget. Create a new project in Eclipse: File => New => Progect => Android Project => SimpleActivity
Open the file AndroidManifest.xml, Keyboard press CTRL + a and DELETE, and insert there the code below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<!--package проекта-->
package="com.api.app"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<!--Определяем наш файл класса что бы можно было с ним работать-->
<receiver android:name="SimpleActivity" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/manifest" />
</receiver>
<!--конец-->
</application>
<uses-sdk android:minSdkVersion="8"/>
</manifest>
By this action, we explained the program's exactly what we create a widget, not a simple program. Now fill in the body of the widget, namely file main.xml. Open this file, remove what was there and add my code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="@drawable/frame">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="пыщ пыщ!!!!111" />
</RelativeLayout>
In this file, we have crushed a general view of our widget.
And now we have to describe the widget, whatever tablet or phone could not understand what you are talking business. Create a folder in the directory xml res (you should get like this / res / xml /). And now in the xml folder create a file manifest.xml (tobish / res / xml / manifest.xml) and write there:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/main">
</appwidget-provider>
Go ahead ... File SimpleActivity.java - must be empty, because our widget by and large will not do anything, how to display the information in a single entry:
public class widget extends AppWidgetProvider
{}
Now all compile the project. Press MENU in our emulator and on a device look what happened:
In the next article I will discuss how to put the events in this widget, but the truth while and did not really understand.
Thank you all for your attention, wait for the next article.