⬆️ ⬇️

The highlighted widget in Android



Hello, dear habrazhiteli. This article describes the process of creating a “highlighted” widget, that is, a widget that can be distinguished using the D-Pad five-way handle that is present in many Android devices in the form of trackballs or buttons. This article is aimed at a prepared reader who has already written his first “Android hello, world”. I think you should not talk about the benefits of managing hardware keys on devices with a capacitive screen in the cold season, so get down to business right away.





Widget configuration file



Let's start with the widget.xml widget configuration file, create it in the res / xml / subdirectory of your project.

<? xml version ="1.0" encoding ="utf-8" ? > <br> < appwidget-provider <br> xmlns:android ="http://schemas.android.com/apk/res/android" <br> android:minWidth ="72dip" <br> android:minHeight ="72dip" <br> android:focusable ="true" <br> android:updatePeriodMillis ="0" <br> android:initialLayout ="@layout/widget" /> <br><br> * This source code was highlighted with Source Code Highlighter .


The android: minWidth and android: minHeight attributes are responsible for the widget sizes, android: updatePeriodMillis for how often the widget will be updated, in a specific case, we do not need an update, android: initialLayout - indicates which interface the Activity will be used for the widget. We are also interested in the android: focusable attribute, which, as the name implies, allows or denies focusing the cursor on widget elements.



Widget layout



The widget will consist of an icon and a caption under it, both elements will be pressed and highlighted.

')

res / layout / widget.xml

<? xml version ="1.0" encoding ="utf-8" ? > <br> < LinearLayout <br> xmlns:android ="http://schemas.android.com/apk/res/android" <br> android:layout_width ="72dip" <br> android:layout_height ="72dip" <br> android:orientation ="vertical" > <br> < ImageView <br> android:id ="@+id/icon" <br> android:layout_width ="48dip" <br> android:layout_height ="48dip" <br> android:clickable ="true" <br> android:focusable ="true" <br> android:src ="@drawable/icon" <br> android:background ="@drawable/icon_background" <br> android:layout_gravity ="center" /> <br> < LinearLayout <br> android:id ="@+id/label" <br> xmlns:android ="http://schemas.android.com/apk/res/android" <br> android:layout_width ="wrap_content" <br> android:layout_height ="22dip" <br> android:orientation ="vertical" <br> android:clickable ="true" <br> android:focusable ="true" <br> android:background ="@drawable/label_background" <br> android:gravity ="center" > <br> < TextView <br> android:layout_width ="wrap_content" <br> android:layout_height ="wrap_content" <br> android:text ="@string/label" <br> android:textSize ="13sp" <br> android:textColor ="#ffffff" /> <br> </ LinearLayout > <br> </ LinearLayout > <br><br> * This source code was highlighted with Source Code Highlighter .


Here you should pay attention to the android: background attribute of the icon and signature. This parameter, as you can guess, allows you to put a background image or a color in the background of the object, in this case it points to files that describe the rules for highlighting elements.



Highlight Rules



“Rules of highlighting” I called StateListDrawable , a tool used to change the background of an object depending on its state. The rules are contained in the item tags, android: state_focused attributes (object in focus) and android: state_pressed (object pressed) - these are the conditions under which the background image will be set, android: drawable indicates the resource to be used as the background.



res / drawable / icon_background.xml

<? xml version ="1.0" encoding ="utf-8" ? > <br> < selector xmlns:android ="http://schemas.android.com/apk/res/android" > <br> < item <br> android:state_focused ="true" <br> android:state_pressed ="false" <br> android:drawable ="@drawable/icon_shadow" /> <br> < item <br> android:state_focused ="true" <br> android:state_pressed ="true" <br> android:drawable ="@drawable/icon_shadow" /> <br> < item <br> android:state_focused ="false" <br> android:state_pressed ="true" <br> android:drawable ="@drawable/icon_shadow" /> <br> </ selector > <br><br> * This source code was highlighted with Source Code Highlighter .


In the rules for highlighting the icons, we see three rules that can be voiced like this:





res / drawable / label_background.xml

<? xml version ="1.0" encoding ="utf-8" ? > <br> < selector xmlns:android ="http://schemas.android.com/apk/res/android" > <br> < item <br> android:state_focused ="true" <br> android:state_pressed ="false" <br> android:drawable ="@drawable/label_shadow" /> <br> < item <br> android:state_focused ="true" <br> android:state_pressed ="true" <br> android:drawable ="@drawable/label_shadow" /> <br> < item <br> android:state_focused ="false" <br> android:state_pressed ="true" <br> android:drawable ="@drawable/label_shadow" /> <br> < item android:drawable ="@drawable/label" /> <br> </ selector > <br><br> * This source code was highlighted with Source Code Highlighter .


The rules for highlighting a signature are almost the same, except for the fourth rule without conditions, which sets the default background image.



Conclusion



Now we just need to put the resources into the res / drawable directory and compile the Manifest:

<? xml version ="1.0" encoding ="utf-8" ? > <br> < manifest <br> xmlns:android ="http://schemas.android.com/apk/res/android" <br> package ="org.selectdroid" <br> android:versionCode ="1" <br> android:versionName ="1.0" > <br> < application <br> android:icon ="@drawable/icon" <br> android:label ="@string/app_name" > <br> < receiver <br> android:name =".Widget" <br> android:label ="@string/app_name" > <br> < intent-filter > <br> < action android:name ="android.appwidget.action.APPWIDGET_UPDATE" /> <br> </ intent-filter > <br> < meta-data <br> android:name ="android.appwidget.provider" <br> android:resource ="@xml/widget" /> <br> </ receiver > <br> </ application > <br> </ manifest > <br><br> * This source code was highlighted with Source Code Highlighter .


After compiling and running, we will get this widget:





Thank you all for your attention. I hope that this article will be useful to someone.



The article was written using developer.android.com documentation.

The source code of the widget prototype can be downloaded here .

Compiled apk here .

Source: https://habr.com/ru/post/114089/



All Articles