Android widgets – Advanced ⭐

Reading Time: 13 minutes

In this tutorial we are going to build a more complex widget, where you can add and remove 1 from the number using the plus and minus buttons. In this advanced tutorial about the Android widgets we are going to learn how we can handle click events and update the widget without opening the Activity. We gonna learn also how we can add a configuration screen to the Android widgets as well and how we can update the widget from the app.

Basics tutorial

This tutorial assumes you’re that familiar with the basics of Android widgets. If you aren’t then please review our free Android widgets – Basics tutorial.

The sample app

As you can see, our sample application going to have a simple user interface, which contains a TextView with a number and a plus and a minus Buttons. The widget will have the same Views, and at the bottom of the widget a TextView, When we click on it then the MainActivity will open.

This app uses SharedPreferences to store the number and the selected limit for the instances of the Widget. When you click on the plus-minus buttons, then the number gonna be saved all the time.

To have communication between the app and the widget instances we gonna create a service. Using it we can update the widget from the MainActivity.

To update the MainActivity we won’t use any service, instead we gonna override the onCreate() and the onResume() methods to load the stored number using again SharedPreferences.

Let’s start coding. 😎

Step 1 – Create new project

First thing first, we gonna create a whole new project. For this, launch Android Studio. If you see the “Welcome page”, then click on the “Start a new Android Studio project”. If you have an open project, then from the “File” menu select “New”, then “New Project”. After thet, this window will popup.

Here select the “Empty Activity” option. In the next window, we should provide some information about the newly created app.

This tutorial will contain very few code, but still we have to select the programming language. As the other tutorials on this website, this will be written also in Kotlin. So, select from the dropdown list the Kotlin language.

From the next list of the “Minimum SDK” select API 21. In our case API 21 gonna be enough.

If you are done, click on the “Finish” button. The build can take few minutes. Be patient! 😉

When the build has been finished, then you should see the open MainActivity::class and next to this the activity_main.xml files.

Step 2 – Add the Widget

You can create the widget manually, or you can add it using Android Studio. In the advanced tutorial we are going to select the second option again. In this case Android Studio is going to prepare for us the needed files.

So, with the right mouse button click on the main source set (where you can find the MainAcitvity.kt file as well). Then, from the quick menu select New, then the Widget option. From the submenu choose the App Widget option.

Our widget’s width gonna be 3 cells, and the heigth 2 cells. We need space for the 2 buttons. 😎

For this sample app we gonna create a Configuration screen as well, so check in its CheckBox.

Now, click on the Finish button.

Note that Android Studio has created a new Activity as well with the name of “AppWidgetConfigureActivity”. This Activity will be repsonsible for the configuration settings. About this we gonna talk later.

Step 3 – Widget layout

We won’t go into the details of the layout for the Widget, because as we have talked about earlier, it will be very simple. So just copy and paste the below xml lines into the app_widget.xml file, what you can find in the res->layout folder.


app_widget.xml

The missing blue and red colors go into the colors. xml file (res->values).

<color name=“blue”>#3669FF</string>
<color name=“red”>#FF4545</string>

And the missing strings go to the strings.xml file. (res->values)

<string name=“no_saved_number”>No saved number</string>
<string name=“number”>Number</string>
<string name=“open_app”>Open app</string>

Step 4 – Layout of the MainAcitvity

We won’t go into the details of the MainAcitivity‘s layout neither. It will be also very simple. So again, copy and paste the below xml code into the activity_main.xml file, what you can find also in the res->layout folders.


activity_main.xml

Step 5 –The Enum class for operations

To identify which button (plus or minus) we have clicked, we gonna create an Enum class, which gonna hold these 2 options. We are going to create it in the main source set, where you can find the already created Kotlin files.

So, click on the package name of the main source set with the right mouse button, select New and the Kotlin file/class options. In the popup window name the file as “Operation” and selct the Enum class option. Then press enter.

Extend the code with the below one.


enum class Operation

Step 6 – SharedPreferences

To store the number and the limit, we gonna use SharedPreferences.

Using SharedPreferences we can store key-value pairs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them.

For the SharedPreferences operations we gonna create a new Kotlin file also in the main source set. So, create one with the name of “SharedPreferencesDAO”.

Thenafter paste the below Kotlin code into the new file.


SharedPreferencesDAO

Notes

      • Member variables
        Change the value of the  PREFS_NAME to the package name of your app.
        The rest variables will be the keys for the save and get methods.
      • Save methods
        These methods gonna save the number and the limits for the instances of the widget.
        In case of the limit we need the id of the instance, because we can add as many instances from a widget, as many we want.
      • Getter methods
        Using the keys we can just get back the values.
        In case of the number, the getNumberPref() returns a String in case if there is no stored value yet. If no number was saved before, then the method returns the “No saved number” text.

Step 7 – The widget's counter

In this step finally, we are going to implement the counter for the widget. It means, when we click on the plus-minus buttons, then the counter on the widget’s instance going to be changed without opening the app.

getPendingIntentWidget()

We are going to work in the CounterWidget.kt file, so open it. Then paste at the end of the file file the below getPendingIntentWidget() method.


getPendingIntentWidget()

To add a click listener to the widget’s views, we need to create a PendingIntent. Because our Widget is running in an application that is different from ours and runs in another Android process, we have to use PendingIntent. This is the way to ask another app to launch an Intent for us.

The other parameter of this method is the Operation::enum. It contains which button we have clicked, because we are going to catch it in the onReceive() method. That’s why we have to save it to the intent’s action.

Member variables

In the AppWidget::class we are going to define 2 Int variables to store the current number and the limit. In this case we don’t have to load it every time from SharePreferences.

So, paste the below companion object to the beginning of the AppWidget::class.


companion object

Why do we need the companion object? Because we have to reach always the same instance of the limit and number values, and we need them outside of the class as well. Let’s have a clear picture in the next method. 😊

The companion object is a singleton, and its members can be accessed directly via the name of the containing class.
Source: Kotlinlang

updateAppWidget()

Next, go to the updateAppWidget() method and modfiy it to the below lines.


updateAppWidget()

      1. Construct the RemoteViews object
      2. Add the click listeners to the buttons
      3. Get the limit from SharedPreferences
      4. Assign it to the limit variable
      5. Based on the limit value, set the background of the widget’s instance
      6. Instruct the widget manager to update the widget

updateNumber()

To update the widgets when we click on the buttons, and to save the number using SharedPreferences, we need to declare 2 more methods.

The first one will check the clicked button and based on it, it will update the number.

Paste the below method to the end of the AppWidget.kt file.


updateNumber()

saveNumber()

This method helps us to save the current number. Paste it also to the end of the AppWidget.kt file.


saveNumber()

onReceive()

The onReceive() method is called for every broadcast and before all callback methods, like onUpdate(), onEnabled(). It dispatch calls to the various other methods on AppWidgetProvider.

It means in our case, that we are going to update and save the number then refresh the instances’ of the widget in this method.

Insert the below method into the AppWidget::class.


onReceive()

      1. Get the saved number from the store
      2. Handle the click event and update the number
      3. Save the new number into the store
      4. Update the TextView of all instances of the widget
      5. Need is to update all instances of the widget one-by-one, otherwise the configurations of the instances will be ignored
      6. Instruct the widget manager to update the widget

onDeleted()

Before the first run delete the onDelete() method from the AppWidget::class.

Run the app

Finally, it is time to run the app. 

Add the widget to the Home screen. It has the default preview image. Then, click on the plus-minus buttons. The background going to be changed when you are above or below 0.

If you add one more instance to the Home screen, then it is going to be changed also when you tap on the buttons.

Next, we are going to prepare the configuration screen to have different limits for the different instances of the widget.

Step 8 – The AppWidgetConfigureActivity

In this step we are going to implement the configuration screen for the widget. This screen will be shown when we add a new instance of the widget… as you could see during the first run.

So, open up the AppWidgetConfigureActivity.kt file from the main source set.

First of all, delete the unnecessary variables and methods, which are outside of the AppWidgetConfigureActivity::class.

Next, remove the instance of appWidgetText, which is an EditText. We won’t set its value anymore. Thenafter remove all of its calles, which are now in red.

Thenafter add the below instance of the SharedPreferencesDAO::class to the beginning of the Activity.

private val numberLoggerPersistence = SharedPreferencesDAO(this)

Then, we have to save the limit number from the EditText. So paste the below line into the beginning of the onClickListener variable.


Save the limit value

Run the app

First remove all the instances of the widget.

Then add at least 2 instances with different limits. Tap on the plus-minus buttons, and the background should be changed for the instances if the number reaches their limits.

Step 9 – Open the app

So, let’s continue this tutorial with some new features. In this step we are going to implement how we can open the app from the widget.

As you saw, the layout of the widget contains a TextView with the text: “Open app”. When you tap on it, then this should open the MainActivity::class.

We have already added 2 click listeners for the plus-minus buttons. In the same place we gonna add the a click listener for the TextView as well. So, open up the AppWidget::class and navigate to the updateAppWidget() method and add the below line after the click listeners of the plus-minus buttons.


setOnClickPendingIntent

Now you should have the getPendingIntentActivity() in red color. It is because we haven’t created this method yet.

This method will create a PendingIntent for the start of the Activity. So, copy and paste the below method after the AppWidget::class.


getPendingIntentActivity()

Run the app

Run again the app an test that the clicking on the TextView works as expected.

There is now a problem. The widget have to pass the current number ot the MainActivity::class. Don’t worry, we are going to fix this in the next step. 😊

Step 10 – Get and set the number in the activity

So, our task now is to update the user interface of the MainActivity to see there the current number as well and handle there the click on the plus-minus buttons as well.

We won’t take it from the widget. Remember, that we save the number every time, when we tap on the plus-minus buttons?

In the MainActivity::class we are going to get the data from SharedPreferences in the onCreate() and onResume() methods. This gonna solve our problem. 😎

Member variables

First add the below 2 member variables to the beginning of the MainActivity::class.


Member variables

setNumber()

Thenafter add the below method after the onCreate() method. which will get the number from SharedPreferences and set the TextView of the number.

It will set the number integer only, when there is a saved number.


setNumber()

Then call this method from the onCreate() and onResume() methods.


call setNumber()

saveNumber()

The next step is to save the number. So, paste the below method after the onCreate() method.


saveNumber()

updateNumber()

In this method we will handle the clicks on the button and set the text of the TextView. Also in this method we will call the saveNumber() method. This method goes also after the onCreate() method.


updateNumber()

Click listener

Finally in this step add the below click listeners to the onCreate() method.


setOnClickListener ()

Run the app

Run again the app an test the TextView on widget. Tap on it and the app should open and the TextView for the number on the MainActivity will be also updated.

Next test the plus-minus buttons on the MainActivity as well. It should work also.

Now, if you send the app to the background or just close it, then you can check that the instances of the widget aren’t refreshed. There you can see still the previous value. But, if you you tap on the buttons, then it will change the real number and update with the correct data the number’s TextView.

We gonna solve this issue in the next step using service.

Step 10 – Update widget with service

So, the last step of this tutorial is to update the widget’s instances from the app. We can do it with a service.

A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC).

Source: developer.android.com

When we click on the buttons on the MainActivity, then it will send a broadcast. The service will catch this broadcast and it will call the updateAppWidget() method in the AppWidget.kt file. This service will be start in the onUpdate() of the AppWidget::class…. So, let’s finish this tutorial. 😎

Create the service

First, we are going to create a service. So, click with the right mouse button on the main source set, then select New and go to Service, then select again Service. In the popup window name it as “WidgetUpdaterService”.

After the creation, the WidgetUpdaterService::class overrides the onBind() method. We won’t use it, so change it to return null. It means, it has to return a nullable type: IBinder?


onBind

Override onStartCommand()

Thenafter in the WidgetUpdaterService::class we have to override the onStartCommand() method.

The system invokes this method by calling startService() when another component (such as an activity) requests that the service be started. When this method executes, the service is started and can run in the background indefinitely.

Source: developer.android.com

 

In this method first we have to find the ids of the widget’s instances, because we have to update them one-by-one using a for-cycle. So, the onStartCommand() method look like below.


onStartCommand()

Start the service

The next step is to start the WidgetUpdaterService, what we are going to do in the onUpdate() method of the AppWidget::class.

Currently there we have to same for-cycle, what we have added to the service’s onStartCommand() method. We will replace it with the below lines.


Start the service

To start a service first we need an Intent, which will take over the ids of the widget’s instances. Then by calling the startService() method on the context we can start the service.

Send Broadcast

So, the last step is to send a broadcast which will tell for the Android system to update the widget. The broadcast message itself is wrapped in an Intent object whose action string identifies the event that occurred (for example ACTION_APPWIDGET_UPDATE).

So, open up the MainActivity.kt file from the main source set, and paste the below method to the end of the MainActivity::class.


refreshTodayLabel()

      1. Creates an instance for the AppWidgetManager.
      2. Then, we will get the ids of the widget.
      3. The next Intent will update the app widget.
      4. Using the putExtra() method we can take over the ids of the widget’s instances.
      5. Finally we can send the broadcast.

Call this method from the updateNumber() method, after saving the number to SharedPreferences.

Run the app

For the last time, run again the app and test it.

Source Code

The source code is available under the below link, check it out and download it.

Download

Questions

I hope the description was understandable and clear. But, if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂

Follow and like us:

Click to rate this post!
[Total: 0 Average: 0]

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications