We provide Android App Development Company Services for every kind of business as fashion, wellness, shopping, E-commerce, and more. For many different reputed industries, we have created a dynamic, compelling, and user-friendly application for the Android platform.
In Android, we can publish this type of view using the App Widget provider and component of the application that provide feature to hold other app widgets.
1User interface
Widgets are running in the different application so for security and performance reasons some restrictions are
In widgets. So you can only use standard component and this component interacts with RemoteViews.
You can use only:
-
AnalogClock
-
Button
-
Chronometer
-
ImageButton
-
ImageView
-
ProgressBar
-
TextView
-
ViewFlipper
-
ListView
-
GridView
-
StackView
-
AdapterViewFlipper
Along with ViewStub, which allows lazy inflation of a layout, you can only use the following containers:
-
FrameLayout
-
LinearLayout
-
RelativeLayout
-
GridLayout
Extensions of these classes are not allowed.
For some restrictions, widgets use only TextView, Button, ImageView.
We require one to receive the data so open AndroidManifest.xml and add the below the line.
2AndroidManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?><manifest xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:dist=”http://schemas.android.com/apk/distribution” package=”com.demo.widgets”> <application android:allowBackup=”true” android:icon=”@mipmap/ic_launcher” android:label=”@string/app_name” android:roundIcon=”@mipmap/ic_launcher_round” android:supportsRtl=”true” android:theme=”@style/AppTheme”> <receiver android:name=”.MyWidget”> <intent-filter> <action android:name=”android.appwidget.action.APPWIDGET_UPDATE” /> </intent-filter> <meta-data android:name=”android.appwidget.provider” android:resource=”@xml/my_widget_info” /> </receiver> <activity android:name=”.MainActivity”> <intent-filter> <action android:name=”android.intent.action.MAIN” /> <action android:name=”android.intent.action.VIEW” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> </application></manifest>
view rawAndroidManifest.xml hosted with ❤ by GitHub
3AppWidgetProviderInfo Metadata
In Android AppWidgetProviderInfo provides the best qualities of App Widgets. it provides the function like minimum layout dimensions, how to update app widgets and initial layout file.
So we define AppWidgetProviderInfo object in XML resource using <appwidget-provider>
And save it in res/xml folder
4my_widget_info.xml
<?xml version=”1.0″ encoding=”utf-8″?><appwidget-provider xmlns:android=”http://schemas.android.com/apk/res/android” android:initialKeyguardLayout=”@layout/my_widget” android:initialLayout=”@layout/my_widget” android:minWidth=”100dp” android:minHeight=”40dp” android:resizeMode=”horizontal|vertical” android:updatePeriodMillis=”86400000″ android:widgetCategory=”home_screen” />
view rawmy_widget_info.xml hosted with ❤ by GitHub
5Call AppWidgetProvider
In Java for handling app widgets broadcast we extend AppWidgetProvider class and then we override the update method.
6MyWidget.java
After that open the my_widget.xml file and write the code as below.
public class MyWidget extends AppWidgetProvider { public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; Log.e(“Updating widgets ” , Arrays.asList(appWidgetIds)+” ”); for (int i = 0; i < N; i++) { int appWidgetId = appWidgetIds[i]; String url = “https://www.vasundharavision.com/”; Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse(url)); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); // attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget); views.setOnClickPendingIntent(R.id.btn, pendingIntent); // To update a textview views.setTextViewText(R.id.txtWidget, “Vasundhara Vision”); appWidgetManager.updateAppWidget(appWidgetId, views); } }}
view rawMyWidget.java hosted with ❤ by GitHub
7my_widget.xml
After that open the MainActivity.java and write code as below.
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” android:layout_margin=”4dp” android:background=”#666F70″ android:gravity=”center” android:orientation=”vertical”> <TextView android:id=”@+id/txtWidget” android:textSize=”15sp” android:layout_width=”wrap_content” android:layout_height=”wrap_content”/> <Button android:id=”@+id/btn” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Click Me!” /></LinearLayout>
view rawmy_widget.xml hosted with ❤ by GitHub
8MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
view rawMainActivity.java hosted with ❤ by GitHub
9Output :
After that we run the project and we will get this type of output.