A Widget Is A Small Gadget or Control of Your Android Application Placed On The Home Screen
A Widget Is A Small Gadget or Control of Your Android Application Placed On The Home Screen
on the home screen. Widgets can be very handy as they allow you to put your
favourite applications on your home screen in order to quickly access them.
You have probably seen some common widgets, such as music widget, weather
widget, clock widget e.t.c
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:updatePeriodMillis="0"
android:minHeight="146dp"
android:initialLayout="@layout/activity_main">
</appwidget-provider>
appWidgetManager.updateAppWidget(currentWidgetId,views);
A part from the updateAppWidget method, there are other methods defined in
this class to manipulate widgets. They are as follows −
2 onDisabled(Context context)
This is called when the last instance of AppWidgetProvider is
deleted
3 onEnabled(Context context)
This is called when an instance of AppWidgetProvider is created.
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/example_appwidget_info" />
</receiver>
Example
Here is an example demonstrating the use of application Widget. It creates a
basic widget applications that will open this current website in the browser.
To experiment with this example, you need to run this on an actual device on
which internet is running.
Step Description
s
6 Run the application and choose a running android device and install
the application on it and verify the results.
package com.example.sairamkrishna.myapplication;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;
import android.widget.Toast;
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
views.setOnClickPendingIntent(R.id.button, pending);
appWidgetManager.updateAppWidget(currentWidgetId,views);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:transitionGroup="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_centerHorizontal="true"
android:textColor="#ff3412ff"
android:textSize="35dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Widget"
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:layout_below="@+id/textView" />
&lr;/RelativeLayout>
Following is the content of the res/xml/mywidget.xml.
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:updatePeriodMillis="0"
android:minHeight="146dp"
android:initialLayout="@layout/activity_main">
</appwidget-provider>
<resources>
<string name="action_settings">Settings</string>
</resources>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/mywidget"></meta-data>
</receive>
</application>
</manifest>
Let's try to run your application. I assume you have connected your actual
Android Mobile device with your computer. To run the app from Android studio,
open one of your project's activity files and click Run icon from the tool bar.
Before starting your application, Android studio will display following window to
select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device
which will display your default screen −
Go to your widget section and add your created widget to the desktop or home
screen. It would look something like this −
Now just tap on the widget button that appears, to launch the browser. But
before that please make sure that you are connected to the internet. After
pressing the button , the following screen would appear −
Note. By just changing the url in the java file, your widget will open your
desired website in the browser.