Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android home screen widgets tutorial - part I

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2011, 09:57 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android home screen widgets tutorial - part I

Basic4android v1.6 adds support for home screen widgets. This tutorial will explain how to implement your own home screen widgets (also named App Widgets).

It is important to understand that the widgets are created and managed in another process, different than the process that your application is running in. The home screen application is hosting your widgets.
This means that it is not possible to directly access the widgets views. Instead we are using a special object named RemoteViews which gives us indirect access to the widget views.

Widgets do not support all views types. The following views are supported:
- Button (default drawable)
- Label (ColorDrawable or GradientDrawable)
- Panel (ColorDrawable or GradientDrawable)
- ImageView
- ProgressBar (both modes)

All views support the Click event and no other event.

The widget layout and configuration must be defined with XML files. During compilation Basic4android reads the layout file created with the designer and generates the required XML files.

Each widget is tied to a Service module. Through this module the widget is created and updated.

Creating a widget - step by step guide

- Add a Service module. Note that the service module handling the widget is a standard service.
- Design the widget layout with the designer. First add a Panel and then add the other views to this Panel.
The widget layout will be made from this panel.
- Add code similar to the following code the service module:
Code:
Sub Process_Globals
    
Dim rv As RemoteViews
End Sub

Sub Service_Create
    rv = 
ConfigureHomeWidget("LayoutFile""rv"0"Widget Name")
End Sub

Sub Service_Start (StartingIntent As Intent)
    
If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Sub rv_RequestUpdate
    rv.UpdateWidget
End Sub

Sub rv_Disabled
    
StopService("")
End Sub

Sub Service_Destroy

End Sub
- Compile and run your application. Go to the home screen, long press on the screen and you will see your widget listed on the widgets list.

ConfigureHomeWidget is a special keyword. At runtime it creates the RemoteViews object from the layout and set the events. At compile time the compiler generates the required files based on the arguments of this keyword.
The four parameters are: layout file, event name, update interval and the widget name.
Event name sets the subs that will handle the RequestUpdate and Disabled events.
The widget can be configured to update itself automatically. The interval, measured in minutes, defines how often will the widget request to update itself. Set to 0 to disable automatic updates. Updating the widget too often will have a bad impact on the battery. The minimum value is 30 minutes.
Widget name - the name that will appear in the widgets list.

As these arguments are read by the compiler, only strings or numbers are accepted.

Events:
Code:
Sub Service_Start (StartingIntent As Intent)
    
If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub
The above code checks the Intent message that caused this service to start and is responsible for raising the events related to the widget. It returns true if an event was raised.
The widget raises two events. RequestUpdate is raised when the widget needs to update itself. It will fire after adding the widget to the screen, after the device has booted, based on the scheduled updating interval (if set) or after the application was updated.
The Disabled event is raised when the last instance of our widget is removed from the screen.

As mentioned above all views support the Click event. All that needs to be done in order to handle the click event of a button named Button1 is to add a sub named Button1_Click (the sub name should actually match the EventName property which is by default the same as the name).
For example if you want to show the main Activity when the user presses on Button1 you can use this code:
Code:
Sub Button1_Click
 
StartActivity(Main)
End Sub
Modifying the widget:
It is not possible to directly access the widget views. Instead we need to use one of the RemoteView.Set methods.
If we want to change the text of a label named Label1 then we need to write the following code:
Code:
rv.SetText("Label1""This is the new text.")
'do more changes if needed
rv.UpdateWidget
After writing all the changes we call rv.UpdateWidget to send the updates to the widget.

A simple example is attached.
The example adds a simple widget. The widget doesn't do anything useful.


Second part of this tutorial: http://www.basic4ppc.com/forum/basic...l-part-ii.html
Attached Files
File Type: zip HomeWidgets.zip (13.2 KB, 689 views)
Reply With Quote
  #2 (permalink)  
Old 07-11-2011, 10:13 AM
dealsmonkey's Avatar
Senior Member
 
Join Date: Feb 2011
Posts: 133
Default

Very nice and just in time for my next app !!

i assume v 1.6 will be available soon ?

Neil
__________________
http://www.nejola.com
Reply With Quote
  #3 (permalink)  
Old 07-11-2011, 10:29 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Beta version will be released today to a group of beta testers. The stable version should be released in about one week.
Reply With Quote
  #4 (permalink)  
Old 07-11-2011, 10:35 AM
Basic4ppc Expert
 
Join Date: Jul 2008
Location: Borchen, Germany
Posts: 571
Send a message via Skype™ to corwin42
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Looks nice.

Time for new beta.

How is it possible to set the widget dimension like 1x1, 2x1, 4x1, 4x2 etc. Is this derived from the layout size?
__________________
You like my work and it is useful for you? If you want you can donate me a beer here:
Need a DropBox account and want 500MB additional free space? Use this link to create your account: DropBox
Reply With Quote
  #5 (permalink)  
Old 07-11-2011, 10:41 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

The size of the base Panel determines the widget minimum width and minimum height properties.
Reply With Quote
  #6 (permalink)  
Old 07-11-2011, 11:03 AM
Basic4ppc Veteran
 
Join Date: May 2011
Posts: 412
Default

Awesome!

Any ETA on preferences activities? Once you get that you pretty much have everything along these lines
Reply With Quote
  #7 (permalink)  
Old 07-11-2011, 11:29 AM
Basic4ppc Veteran
 
Join Date: Feb 2011
Posts: 238
Default

Is it compulsory to create layout files with visual designer? I mean can we create the views dynamically at run time, like for example when I check the RSS feed and there are only 2 items, I only need 2 labels, whereas if there are 5 items I need 5 labels.
Reply With Quote
  #8 (permalink)  
Old 07-11-2011, 11:30 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
Any ETA on preferences activities? Once you get that you pretty much have everything along these lines
This is a tutorial about home screen widgets. Questions regarding other features should be asked in the Questions or Wishlist sub forums.
Reply With Quote
  #9 (permalink)  
Old 07-11-2011, 11:31 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
Is it compulsory to create layout files with visual designer? I mean can we create the views dynamically at run time, like for example when I check the RSS feed and there are only 2 items, I only need 2 labels, whereas if there are 5 items I need 5 labels.
Yes. It is not possible to add views at runtime. Widgets do not support it. You can however add extra views and play with their visibility.
Reply With Quote
  #10 (permalink)  
Old 07-11-2011, 11:37 AM
Basic4ppc Veteran
 
Join Date: Feb 2011
Posts: 238
Default

Thats ok. Visibility should be fine.

One more question. There are a certain kind of widgets available called scrollable widgets. There aren't many. In fact there is only a handful available. Here is one such widget.

https://market.android.com/details?i...koxx.pure_news

You also need a different launcher like ADW.Launcher or Launcher Pro for scrollable widgets to work. Since widgets don't support ScrollViews, any idea how these widgets are implemented?
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android Serial tutorial Erel Basic4android Getting started & Tutorials 78 05-20-2012 05:39 AM
Android JSON tutorial Erel Basic4android Getting started & Tutorials 5 05-07-2012 03:33 AM
Android Network Tutorial Erel Basic4android Getting started & Tutorials 56 03-22-2012 07:26 AM
Android JSON Tutorial klaus German Tutorials 0 02-11-2011 10:31 PM
Android Widgets and Versions Cableguy Basic4android Updates and Questions 1 10-13-2010 12:50 PM


All times are GMT. The time now is 09:54 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0