Simple App Widget – Android Studio Tutorial



In this tutorial we will learn, how to create a simple home screen app widget that contains a button and opens the MainActivity when we click it.
For this we will create a widget layout, an AppWidgetProvider class and an AppWidgetProviderInfo xml file. In the AppWidgetProvider java file we will then override onUpdate and create a PendingIntent which brings us to our MainActivity. We set this intent on our widget button with help of the RemoteViews class and then update our widget with the appWidgetManager object. We repeat this process for all instances of our widget by looping through the appWidgetIds int array.
In the app widget info xml file, we define the update frequency with updatePeriodMillis, the widgetCategory, minHeight, minWidth, minResizeWidth, initialLayout and more.
Lastly we register our AppWidgetProvider in the AndroidManifest.xml file as a broadcast receiver with the APPWIDGET_UPDATE intent filter. We register our appwidget info object in the meta data tag with android.appwidget.provider.
Our widget will have a previewImage which we create with help of the Widget Preview app and it will be resizable.

Watch the whole playlist:

App Widget Guidelines:

Example code:

____________________
❗ Subscribe to the channel:

🎓 Find more tutorials and courses on my website:

💻 Hire me for your next project:

📨 Subscribe to the Coding in Flow newsletter:

💚 Donate to Coding in Flow with a message:

❓ Ask your programming questions in our chat:

📣 Follow Coding in Flow on other sites:
Facebook:
Instagram:
Twitter:
Github:

⌨️ My gear and tools:
Microphone:
Mouse:
Keyboard:
Desk:
Monitor:
Webcam:
Screen recorder:

(above links are affiliate links, which means that I earn a small commission if you buy through them with NO additional costs to you.)

27 Replies to “Simple App Widget – Android Studio Tutorial”

  1. Mike MIkee

    Hi, is that possible to add programmatically multiple widgets from the app I've been searching for a long time and cant find something like this. For example, let's say we have tasks app and one would like to pin specific task to the home screen is that even possible?

  2. Jacob David Cunningham

    Commenting that I used "Empty Activity" from Android Studio when you first start a new project, seems to be right, not saying only option.

    Edit: maybe not, couldn't extend the AppWidgetProvider had to use android.appWidget.AppWidgetProvider. Well his above automatically did the import/had it, so that's odd. I ended up with the same thing but had to import it on my side.

    I have a working demo from this tutorial so thanks. My AS editor just wasn't auto-updating imports but I have a pinable widget that launches the activity.

  3. Marshal Anderson

    Just to say that I really appreciate how you clarify the way variables and objects are linked and referenced in the code – just taking that moment to refer back a couple of lines to show us where something is coming from makes a huge difference.

  4. Alexander Viana

    How to make this kind of code with Widgets?
    Widgets uses the RemoteViews. Help me! All buttons respond together.
    for (int i = 0; i < 3; i++) {

    for (int j = 0; j < 3; j++) {

    String buttonID = "button_" + i + j;

    int resID = getResources().getIdentifier(buttonID, "id", getPackageName());

    buttons[i][j] = findViewById(resID);

    buttons[i][j].setOnClickListener(this);

    }

    }

  5. Techpanda89

    This is exactly what I needed to know to add an extra bit of flair/functionality to my app. Thank you for all of your time and excellent presentation skills.

Leave a Reply

Your email address will not be published. Required fields are marked *