A concise tutorial on how to create and use Android widgets _android

Source: Internet
Author: User
Tags thread class

This example describes the creation and use of the Android programming widget. Share to everyone for your reference, specific as follows:

There is a detailed approach to how to create a widget in the Android reference, and here's a quick overview to see the reference in the Android SDK.

To create a widget, there are several steps:

(1) Create a class to inherit the class Appwidgetprovider, there are many methods in Appwidgetprovider, such as OnDelete (context,int[), onenable (context), etc., But generally we simply overwrite the OnUpdate (context,appwidgetmanager,int[]) method. In this method, we start the class of the background service, typically starting the thread class or the service class in Android. In this class we get the data from the server and process it and display it in the widget.

(2) Add a receiver tag to your androidmenifest.xml to point to your appwidgetprovider subclass. The contents are as follows:

<receiver android:name= "Jiwaiwidget"
android:label= "@string/app_name" android:icon= "
@drawable/jiwai" >
<intent-filter>
<action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name= "Android.appwidget.provider"
      android:resource= "@ Xml/info "/>
</receiver>

Explain the code above:

The first line specifies that the recipient of the widget is Jiwaiwidget, the Appwidgetprovider subclass you have established;

The second line specifies the label name of the widget, which is the app_name value in the String.xml in the value directory;

The third line specifies the widget's icon, which is the Jiwai picture under the drawable directory;

Line four-line sixth is provided in the Android documentation;

Line seventh specifies the descriptor information for the widget, which describes the information about the widget, such as the width, length, and interval of the automatic update, which is located in the Info.xml under the XML directory.

(3) Write the provider file information of your widget (in this case, Xml/info.xml)

<appwidget-provider xmlns:android= "http://schemas.android.com/apk/res/android"
  android:minwidth= "200DP"
  android:minheight= "90DP"
  android:updateperiodmillis= "43200000"
  android:initiallayout= "@layout Appwidget "
  android:configure=" Com.lawrenst.jiwai.JiwaiConfigure ">
</appwidget-provider>

Where Android:updateperiodmillis is the time interval for automatic Updates, Android:initiallayout is the widget's interface description file. Android:configure is optional, and if your widget needs to start an activity at startup, you need to set it up for you. In this case, you need to mumble your account number and password, so you should show an activity first, enter your account and password, and then display the information in your widget.

(4) write the Appwidget.xml file in the layout directory, configure your widget's interface information:

<?xml version= "1.0" encoding= "UTF-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/"
Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:orientation=" vertical " Android:id= "@+id/widget" android:background= "@drawable/title_a" > <linearlayout android:layout_width= "Fill_ Parent "android:orientation=" horizontal "android:layout_height=" Wrap_content "android:background=" @drawable/title
"> <textview android:id=" @+id/username_display "android:textstyle=" bold "android:layout_width=" Wrap_content " android:layout_height= "Fill_parent" android:textcolor= "#ffffff" android:textsize= "15px" android:gravity= "left|" Center_vertical "android:paddingleft=" 6px "/> </LinearLayout> <linearlayout android:orientation=" Vertical "android:layout_width=" fill_parent "android:layout_height=" fill_parent "> <textview android:id=" @+id /text1 "android:layout_width=" fill_parent "android:textcolor=" #ffffff "android:textsize=" 12px "Android: gravity= "Center_vertical|left" android:paddingleft= "6px" android:layout_height= "30px" > </TextView> < TextView android:id= "@+id/text2" android:textcolor= "#ffffff" android:layout_height= "30px" android:gravity= "Center_ Vertical|left "android:textsize=" 12px "android:paddingleft=" 6px "android:layout_width=" fill_parent "> </

 Textview> </LinearLayout> </LinearLayout>

The widget includes three TextView, two used to display the information, one to display the user name, the above code is relatively simple, so do not explain.

(5) As a Acvivity object is required to enter account information, a new login.xml is created under the layout directory as the activity profile:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" "Fill_parent"
;
<textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/hello" Android:textcolor= "#ff8c00" android:capitalize= "characters" android:textstyle= "bold"/> <linearlayout Android : orientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:gravity
= "Center_horizontal" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "@string/user" android:textcolor= "#ff8cff" android:capitalize= "characters"/> <edittext android:
Id= "@+id/username" android:layout_width= "200px" android:layout_height= "wrap_content"/> </LinearLayout> <linearlayout android:orientation= "Horizontal" android:layout_width= "Fill_parenT "android:layout_height=" wrap_content "android:gravity=" Center_horizontal "> <textview android:layout_width=
"Wrap_content" android:layout_height= "wrap_content" android:text= "@string/code" android:textcolor= "#ff8cff" android:capitalize= "Characters"/> <edittext android:id= "@+id/password" android:layout_width= "200px" Android: layout_height= "Wrap_content" android:password= "true"/> </LinearLayout> <linearlayout android:o rientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:gravity= " Center_horizontal "> <button android:id=" @+id/submit "android:layout_width=" Wrap_content "Android:layout_"

 height= "Wrap_content" android:text= "Submit"/> </LinearLayout> </LinearLayout>

There are two edittext to enter the username and password, plus a button object.
The preparations are almost ready, so you can write the code below.

Here to share a case for your reference: http://www.jb51.net/books/40184.html

More interested readers of Android-related content can view this site: "Summary of Android Basic components Usage", "Introduction to Android Development and advanced Tutorials", "Android resource Operation Tips", "Android View tips Summary" and " A summary of the usage of Android controls

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.