Android Basics
Android - Home Android - Overview Android - Environment Setup Android - Architecture Android - Application Components Android - Hello World Example Android - Resources Android - Activities Android - Services Android - Broadcast Receivers Android - Content Providers
Android - Intents/Filters
Android - User Interface
Android - UI Layouts Android - UI Controls Android - Event Handling Android - Styles and Themes Android - Custom Components
Android Advanced Concepts
Android - Drag and Drop Android - Notifications Location Based Services Android - Sending Email Android - Sending SMS Android - Phone Calls
Android Useful Examples
Android - Alert Dialoges Android - Animations Android - Audio Capture Android - AudioManager Android - Auto Complete Android - Best Practices Android - Bluetooth Android - Camera Android - Clipboard Android - Custom Fonts Android - Data Backup Android - Developer Tools Android - Emulator Android - Facebook Integration
Android - Google Maps Android - Image Effects Android - ImageSwitcher Android - Internal Storage Android - JetPlayer Android - JSON Parser Android - Linkedin Integration Android - Loading Spinner Android - Localization Android - Login Screen Android - MediaPlayer Android - Multitouch Android - Navigation Android - Network Connection Android - NFC Guide
Android - ProgressBar Android - Push Notification Android - RenderScript Android - RSS Reader Android - Screen Cast Android - SDK Manager Android - Sensors Android - Session Management Android - Shared Preferences Android - SIP Protocol Android - Spelling Checker Android - SQLite Database Android - Support Library Android - Testing Android - Text to Speech
Android - Twitter Integration Android - UI Design Android - UI Patterns Android - UI Testing Android - WebView Layout Android - Wi-Fi Android - Widgets Android - XML Parsers
Android Useful Resources
Android - Questions and Answers Android - Useful Resources Android - Discussion
Android - Widgets Tutorial
P revious Pag e Pr
Next Page
A widget is a small gadget or control of your android application placed on the home screen. Widgets can be very handy as they allow you to put your favourite applications on your home screen in order to quickly access them. You have probably seen some common widgets, such as music widget, weather widget, clock widget e.t.c Widgets could be of many types such as information widgets, collection widgets, control widgets and hybrid widgets. Android provides us a complete framework to develop our own widgets.
Widget - XML file In order to create an application widget , first thing you need is AppWidgetProviderInfo object, which you will define in a separate widget XML file. In order to do that, right click on your project and create a new folder called
xml. Now right click on the newly created folder and create a new XML file. The resource type of the XML file should be set to AppWidgetProvider. In the xml file, define some properties which are as follows −
android:initialLayout ="@layout/activity_main"
Widget - Layout file Now you have to define the layout of your widget in your default XML file. You can drag components to generate auto xml.
Widget - Java file After defining layout, now create a new JAVA file or use existing one, and extend it with AppWidgetProvider class and override its update method as follows. In the update method, you have to define the object of two classes which are PendingIntent and RemoteViews. Its syntax is − pending = views =
.getActivity (context context, , 0, intent intent, , 0); (context context. .getPackageName (), (), R R. .layout layout. .activity_main );
In the end you have to call an update method updateAppWidget() updateAppWidget() of the AppWidgetManager class. Its syntax is − appWidgetManager .updateAppWidget (currentWidgetId ,views views); );
A part from the updateAppWidget method, there are other methods defined in this class to manipulate widgets. They are as follows − Sr.No Sr. No
Meth Me thod od & De Descr scrip ipti tion on
1
onDeleted(Context context, int[] appWidgetIds)
This is called when an instance of AppWidgetProvider is deleted.
3
onEnabled(Context context)
This is called when an instance of AppWidgetProvider is created. 4
onReceive(Context context, Intent intent)
It is used to dispatch calls to the various methods of the class
Widget - Manifest file You also have to declare the AppWidgetProvider AppWidgetProvider class in your manifest file as follows: android:name ="ExampleAppWidgetProvider"
android:name ="android.appwidget.action.APPWIDGET_UPDATE"
android:name ="android.appwidget.provider" android:resource ="@xml/example_appwidget_info"
Example Here is an example demonstrating the use of application Widget. It creates a basic widget applications that will open this current website in the browser. To experiment with this example, you need to run this on an actual device on which internet is running. Steps Ste ps
Descrip Des criptio tion n
1
You will You will use use And Andro roid id stu studi dio o to cre creat ate e an An Andr droi oid d appl applic icat atio ion n unde underr a pac packa kage ge com.example.sairamkrishna.myapplication. While creating this project, make sure you Target SDK and
3
Modi Mo dify fy the the res res/l /lay ayou out/ t/ac acti tivi vity ty_ma _main in to to add add res respe pect ctiv ive e XML XML comp compon onen ents ts
4
Create Cre ate a new new fol folder der and xml fil file e unde underr res/ res/xml xml/myw /mywidg idget. et.xml xml to add add res respec pectiv tive e XML XML compo componen nents ts
5
Modi Mo dify fy th the e And Andro roid idMa Mani nife fest. st.xml xml to ad add d the the ne nece cess ssar aryy per permi missi ssion ons s
6
Run the the appli applicat cation ion and and choos choose e a runnin running g androi android d device device and and insta install ll the the applic applicati ation on on itit and veri verify fy the the result results. s.
Following is the content of the modified MainActivity.java. com. com .example .sairamkrishna .myapplication ; android .app app. . android .appwidget . android .appwidget . android .content content. . android .content content. . android .net net. . ; android widget. .widget. android widget. .widget.
(
; ; ; ; ; ; ;
{ onUpdate ( onUpdate( context, context , i= i =0; i i<
intent = intent .addFlags intent. addFlags( ( intent. intent .setData setData( (
appWidgetManager ,
[] appWidgetIds [] appWidgetIds ) {
( .ACTION_VIEW ); .FLAG_ACTIVITY_NEW_TASK ); .parse (url url)); ));
pending = views =
.getActivity (context context, , 0,intent intent, , 0); (context context. .getPackageName (), (),R R.layout layout. .activity_main );
views .setOnClickPendingIntent (R.id views. id. .button button, , pending pending); ); appWidgetManager .updateAppWidget (currentWidgetId ,views views); ); makeText( makeText (context "widget added" LENGTH_SHORT ). ).show show(); ();
Following is the modified content of the xml res/layout/activity_main.xml. xmlns:android ="http://schemas.android.com/apk/res/android" xmlns:tools ="http://schemas.android.com/tools" android:layout_width ="match_parent" android:layout_height ="match_parent" android:paddingLeft ="@dimen/activity_horizontal_margin" android:paddingRight ="@dimen/activity_horizontal_margin" android:paddingTop ="@dimen/activity_vertical_margin" android:paddingBottom ="@dimen/activity_vertical_margin" tools:context =".MainActivity" android:transitionGroup ="true"
android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:text ="Tutorials point" android:id ="@+id/textView" android:layout_centerHorizontal ="true" android:textColor ="#ff3412ff" android:textSize ="35dp"
android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:text ="Widget" android:id ="@+id/button" android:layout_centerHorizontal ="true" android:layout_marginTop ="61dp" android:layout_below ="@+id/textView" &lr;/RelativeLayout>
Following is the content of the res/xml/mywidget.xml. ?> xmlns:android ="http://schemas.android.com/apk/res/android" android:minWidth ="146dp" android:updatePeriodMillis ="0" android:minHeight ="146dp"
Following is the content of the res/values/string.xml.
name="app_name" My Application name= name= name ="hello_world" Hello world! name= name ="action_settings" Settings
Following is the content of AndroidManifest.xml file. ?> xmlns:android ="http://schemas.android.com/apk/res/android" package= package ="com.example.sairamkrishna.myapplication"
android:allowBackup ="true" android:icon ="@mipmap/ic_launcher" android:label ="@string/app_name" android:theme ="@style/AppTheme" android:name =".MainActivity"
android:name ="android.appwidget.action.APPWIDGET_UPDATE"
android:name ="android.appwidget.provider" android:resource ="@xml/mywidget"
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app app from Android studio, studio, open open one of of your project's activity activity files and and click Run Run
icon from
the tool bar. Before starting your application, Android studio will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display your default screen −
Go to your widget section and add your created widget to the desktop or home screen. It would look something like this −
Now just tap on the widget button that appears, to launch the browser. But before that please make sure that you are connected to the internet. After pressing the button , the following screen would appear −
Note. By just changing the url in the java file, your widget will open your desired website in the browser. P revious Pag e Pr
Next Page Advertisements
Write for us
FAQ's
Helping
© Copyright 2016. All Rights Reserved. Enter email for newslet
go
Contact