Android Home Screen Widget (icon, shortcut style)

I am trying to create an icon / widget (cell 1 cell x 1) that can be placed on the Android main screen. The widget will look and act just like other standard shortcuts in android. It will have an icon and under this label, it will be selected using the trackball (backlighting is possible), it will be highlighted when selected / pressed.

How do I create this home screen widget?

Do I have to create the widget myself using the / xml code or is there some standard xml, style, theme, code that I can use to make sure the widget will have the same style / theme as other widgets on the main screen ?

I currently have

Res / draw / corners.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Corners"> <stroke android:width="4dp" android:color="#CC222222" /> <padding android:left="4dp" android:top="1dp" android:right="4dp" android:bottom="1dp" /> <corners android:radius="4dp" /> </shape> 

Res / layout / widget.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Widget" android:layout_width="72dip" android:layout_height="72dip" android:orientation="vertical" android:focusable="true" android:gravity="center_horizontal" style="@android:style/Widget" > <ImageView android:id="@+id/WidgetIcon" android:src="@drawable/icon" android:layout_width="fill_parent" android:layout_height="50dip" android:paddingTop="3dip" android:gravity="center" /> <TextView android:id="@+id/WidgetLabel" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/app_name" android:textSize="15dip" android:background="@drawable/corners" /> </LinearLayout> 

The resulting widget looks something like this, but cannot be selected, it does not stand out when pressed, and the label is not in the right place or in the right style.

Any ideas if there is a right way to do this, or should I just keep working on it until I get closer?

+4
android android widget
Apr 16 '10 at 8:39
source share
2 answers

The “right way to do this” is to make a shortcut, rather than trying to mimic it using an application widget. This has been repeatedly stated by the core Android team (in particular, Romain Guy) on the [android-developers] discussion list, for example :

Widgets should look like widgets, not shortcuts. The main reason is that there is absolutely no guarantee on how the shortcut will look. Other devices (especially with user user interfaces such as MOTOBLUR or HTC Sense) may have a different look and feel. Or in the next Android update, we can change the way labels are presented.

+9
Apr 16 '10 at 12:40
source share

To make your element compatible with the system appearance, it is easy to make a link to the android:attr/selectableItemBackground system attribute android:attr/selectableItemBackground . For example, if you want to make ImageView in your widget “selective” with the correct highlighting, etc .: Just do

 <ImageView ... android:background="?android:attr/selectableItemBackground" ... /> 

There are android_sdk_path/platforms/android-x in android_sdk_path/platforms/android-x . Happy to dig!

EDIT: I found out later that this simple attribute does not live in SDK <v11. So the best way that I know now is to download the appwidget template package and use it.

+1
Jul 16 '12 at 22:24
source share



All Articles