Widgets

Realdash can open apps through launch app button on android using package name. Is it possible to open launch a widget aswell

Not sure if widgets have a package name

Thanks

I have never tried to launch a widget, will have to investigate how its done.

Great thanks!

It would open up a lot of possibilities for me!

I can’t find any way to reliable launch a widget from another app. It could be done if Android device is rooted and you know the widget internal class name, but this is too specific to implement for RealDash.

Creating a Widget - making a widget on an Android device
Home »Creating a Widget - making a widget on an Android device
Today we will figure out how to create your own Widget on an Android device. A widget is a familiar element of the desktop, with which you can access some functions of an application: view news in the widget window, weather forecast, update news on various services, control various functions of the device (lock the screen, turn on the radio, Internet and many many others). This time we will not create something grandiose and very useful, such as a flashlight :slight_smile:, but we will make a simple widget, which will be implemented as a button, when we click on which we, using a standard browser, get to everyone’s favorite http site: //learn-android.ru. Of course, you can customize any site you want.

Create a new project, select Blank Activity, minimum version of Android 2.2+. When creating a widget, the first thing is to create an AppWidgetProviderInfo object, in which we will indicate an xml file from which the view of the widget itself will be filled. To do this, create a res / xml folder in the project and create a new xml file named widget.xml in it with the following content:

<? xml version = "1.0" encoding = "utf-8"?>



Now let’s go to the activity_main.xml file and create the interface of our widget, it will consist of a Button:




As you can see, we have created a regular button, so it will be our widget:

Interface view

That is, you can then do whatever you want instead of this button.

Let’s move on to working with the code in the MainActivity.java file. It must inherit from the AppWidgetProvider class, for which its main onUpdate () method exists. In this method, we need to define two objects: PendingIntent and RemoteViews. At the end of their use, you need to call the updateAppWidget () method. MainActivity.java file code:

import android.net.Uri;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.example.widget.R;

public class MainActivity extends AppWidgetProvider {

@Override
public void onUpdate (Context context, AppWidgetManager appWidgetManager,
int [] appWidgetIds) {
for (int i = 0; i <appWidgetIds.length; i ++) {
int currentWidgetId = appWidgetIds ;

// Make a simple http request for the specified link and follow it:
String url = “http://learn-android.ru”;
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData (Uri.parse (url));

// Define two required objects of the PendingIntent and RemoteViews class:
PendingIntent pending = PendingIntent.getActivity (context, 0, intent, 0);
RemoteViews views = new RemoteViews (context.getPackageName (), R.layout.activity_main);

// Set up handling of a click on adding a widget:
views.setOnClickPendingIntent (R.id.button, pending);
appWidgetManager.updateAppWidget (currentWidgetId, views);
Toast.makeText (context, “Widget added”, Toast.LENGTH_SHORT) .show ();
}
}
}

For our widget to work successfully, we need a bit of magic in the AnroidManifest.xml manifest file. It should look like this:

<? xml version = "1.0" encoding = "utf-8"?>



As you might have guessed, the widget is defined in the tag.

The only thing left to tweak is to edit the strings.xml file, adding the lines we use there:

<? xml version = "1.0" encoding = "utf-8"?> LEARN.ANDROID Settings Hello world! Attention! Android Studio may swear at you when you start the program, requiring you to specify the default activity. Select the line "Do not launch Activity":

Making your own widget and launching other widgets from app are different things.