Android Tutorial Android home screen widgets tutorial - part I

Status
Not open for further replies.

Jim Brown

Active Member
Licensed User
Longtime User
Great work Erel. I have updated my WordLearn App with widget support. It works a treat:

 

enrique1

Member
Licensed User
Longtime User
Quick question,

when the widget is updated, run again Service_Start or Service_Create methods??


EDIT: Ok solved, Sub wid_RequestUpdate ^^'
 
Last edited:

anaylor01

Well-Known Member
Licensed User
Longtime User
Sorry. I am using the wrong terms. I mean I want to write a widget I can put icons on that will open other applications. I have been able to do it with apps I have written by manually putting the icons in the designer. I know I can use pm.GetApplicationIcon("com.google.android.youtube") in an app but I don't think I can do this with B4A widget.
 

corwin42

Expert
Licensed User
Longtime User

This should be possible. Use GetApplicationIcon() to get the drawable and set its bitmap with RemoteViews.SetImage() to an ImageView on the Widget.
 

Cableguy

Expert
Licensed User
Longtime User
Actually I think not. If I have understood correctly the entire widget has only one click event... for the whole widget!
 

corwin42

Expert
Licensed User
Longtime User
Actually I think not. If I have understood correctly the entire widget has only one click event... for the whole widget!

No. The only event supported is the click event but every view on the widget can have its own.
 

anaylor01

Well-Known Member
Licensed User
Longtime User
Each imageview has its own click event. Can you give me the syntax that you were talking about earlier
 

anaylor01

Well-Known Member
Licensed User
Longtime User
Another question about the widget size. I have the panel set for a height of 100 but it seems that it is taking up double that size. Any ideas?
 

corwin42

Expert
Licensed User
Longtime User
Another question about the widget size. I have the panel set for a height of 100 but it seems that it is taking up double that size. Any ideas?

I discussed this with Erel in another thread. B4A sets the Widget minWidth and minHeight values the same as your base panel. The problem here is that the cell size in portrait and landscape is different and you have to set the minimum of both variants for minWidth and minHeight. B4A currently does not support this very good.

You should calculate the values for minWidth and minHeight yourself with the formula (cellcount * 74) - 2 and then put these values manually in Objects/res/xml/<widget>_info.xml. Then make this file read only and be happy.
 

MuuSer

Member
Licensed User
Longtime User
You should calculate the values for minWidth and minHeight yourself with the formula (cellcount * 74) - 2 and then put these values manually in Objects/res/xml/<widget>_info.xml. Then make this file read only and be happy.
Thanks, Markus. Your advice is very helpful. First time it seems that no changes in behavior of the widget, but after removing widget from home screen and putting it back - size is just as it must be. Thanks again.
 

corwin42

Expert
Licensed User
Longtime User
It is better to just set the panel size to this value instead.

The problem is that you then loose much space. If you want to be compatible with all device resolutions I must agree. On a device with 240x320 it is really a problem if you size your widget bigger but on devices with high resolution you get very small widgets with large free borders. I'm just playing around with different widget sizes in portrait and landscape mode. There is really no perfect solution for widget sizing.
 

corwin42

Expert
Licensed User
Longtime User
Maybe I'm missing something. There should be no difference between setting the panel size to the calculated value or setting it manually in the XML file.

Attached is a screenshot of my current homescreen. The lower weather widget was created with B4A. The base panel has a size of 310x90 and I think it has the correct size for my homescreen (though it is not centered correctly now).

If I size the widget 294x72 (which is the maximum "official" size) it looks really small and has big borders.

If your widget is 4x4 the height is much worse. Then I could size my widget 310x390 without problems. The "official" size would be 294x294 with is nearly 100 dp smaller.
 

Attachments

  • AHWeather_Screenshot.jpg
    55.3 KB · Views: 1,088
Last edited:

dealsmonkey

Active Member
Licensed User
Longtime User
Here is a screen grab of one of my widgets on an HTC Desire.

What I did was lay down a transparent panel of 314 x 78, then put a visible panel on top which is 300 x 78 with all the controls on that visible panel. I then adjusted the second panel to center it

Neil
 

Attachments

  • device.jpg
    14 KB · Views: 982
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…