I am trying to make a home screen widget which will toggle on and off the gps (Location in settings). I still don't understand how to handle a button click event.
I took this code from a tutorial online.
public class LocationWidget extends AppWidgetProvider {
private static String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
String status;
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
status = "OFF";
} else {
status = "ON";
}
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
remoteViews.setTextViewText(R.id.textView, status);
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
remoteViews.setTextViewText(R.id.textView, "Button clicked");
Log.v("ACTION", intent.getAction());
}
}
Inside onReceive, Log.v returns "ACTION﹕android.appwidget.action.APPWIDGET_UPDATE". So I need help to make the button behave the way i want when clicked. Can someone explain how and why?
i will open my heplactivity you can change and add your setting intent
appwidget.java
public class MyWidget extends AppWidgetProvider {
Button btnhelp;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent configIntent = new Intent(context, HelpActivity.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.btnhelp, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
manifest
<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/widget_info" />
</receiver>
resource xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="#layout/widget_layout"
android:minHeight="70dp"
android:minWidth="70dp"
android:updatePeriodMillis="300000"></appwidget-provider>
layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="#drawable/ic_dashboard_help"
android:clickable="true">
<Button
android:id="#+id/btnhelp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="4dip"
android:background="#android:color/transparent" />
</LinearLayout>
I got it.
public class LocationWidget extends AppWidgetProvider {
public static String WIDGET_BUTTON = "MY_PACKAGE_NAME.WIDGET_BUTTON";
private static final String ACTION_CLICK = "ACTION_CLICK";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
String status;
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
status = "OFF";
} else {
status = "ON";
}
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
if (status == "ON") {
remoteViews.setInt(R.id.actionButton, "setBackgroundColor", Color.GREEN);
remoteViews.setTextViewText(R.id.actionButton, "Location Service On");
}
if (status == "OFF") {
remoteViews.setInt(R.id.actionButton, "setBackgroundColor", Color.RED);
remoteViews.setTextViewText(R.id.actionButton, "Location Service Off");
}
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
String buttonclick = "buttonclick";
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.actionButton, getPendingSelfIntent(context, buttonclick));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
if (intent.getAction() == "buttonclick") {
Log.v("ACTION", "buttonclick");
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
callGPSSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callGPSSettingIntent);
}
}
}
You should be able to do
if( intent.getAction() == <action_type> )
{
/*Handling code here*/
}
Related
I've seen a lot of answer here, none of them worked..! I just want a textview to get updated instantly once I click a button next to it.! I've tried to print a toast message and it shows normally, but updating views is the problem.
My Code
public class FollowersWidget extends AppWidgetProvider {
public static String REFRESH_ACTION = "android.appwidget.action.APPWIDGET_UPDATE";
int x = 0;
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(REFRESH_ACTION)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
x++;
views.setTextViewText(R.id.textv_count, x + "");
int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, FollowersWidget.class));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
try {
String count = MyConfigureActivity.getNumCount(context, appWidgetId);
views.setTextViewText(R.id.textv_count, count );
views.setOnClickPendingIntent(R.id.refreshButton,getRefreshPendingIntent(context, appWidgetId));
} catch (Exception e) {
e.printStackTrace();
}
appWidgetManager.updateAppWidget(appWidgetId, views);
}
public static PendingIntent getRefreshPendingIntent(Context context, int appWidgetId) {
Intent intent = new Intent(REFRESH_ACTION);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
// other widget methods..
..
.
}
My Manifest
<receiver android:name=".FollowersWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/followers_info"/>
</receiver>
I figured it out finally, I've seen a lot of ways to handle the onClick on widgets, but this way that worked out for me and it updates views intantally.
Code
public class TestWidget extends AppWidgetProvider {
public static String REFRESH_ACTION = "android.appwidget.action.APPWIDGET_UPDATE";
static private RemoteViews views;
private int x = 0;
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equalsIgnoreCase(REFRESH_ACTION)) {
views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TestWidget.class));
x++;
views.setTextViewText(R.id.textv_count, x + "");
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
views = new RemoteViews(context.getPackageName(), R.layout.test_widget);
views.setTextViewText(R.id.textv_count, x + "");
views.setOnClickPendingIntent(R.id.button, getPenIntent(context));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
static private PendingIntent getPenIntent(Context context) {
Intent intent = new Intent(context, TestWidget.class);
intent.setAction(REFRESH_ACTION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
Just check out following code:
public class WidgetExample extends AppWidgetProvider {
public static final String REFRESH_ACTION = "YOUR_REFRESH_ACTION";
public static final String VALUE_KEY = "VALUE_KEY";
public static final String PREFERENCE_FILE_KEY = "PREFERENCE_FILE_KEY";
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent != null) {
if (intent.getAction() != null) {
if (intent.getAction().equals(REFRESH_ACTION)) {
int value = readValue(context) + 1;
saveValue(context , value);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetExample.class));
for (int id : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
views.setTextViewText(R.id.textv_count, String.valueOf(value));
views.setOnClickPendingIntent(R.id.button_refresh, getRefreshPendingIntent(context));
appWidgetManager.updateAppWidget(id, views);
}
}
}
}
}
#Override
public void onEnabled(Context context) {
int value = readValue(context);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, WidgetExample.class));
for (int id : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.followers_widget);
views.setTextViewText(R.id.textv_count, String.valueOf(value));
views.setOnClickPendingIntent(R.id.button_refresh, getRefreshPendingIntent(context));
appWidgetManager.updateAppWidget(id, views);
}
}
private void saveValue(Context context, int value) {
SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCE_FILE_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(VALUE_KEY, value);
editor.apply();
}
private int readValue(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCE_FILE_KEY, Context.MODE_PRIVATE);
return sharedPref.getInt(VALUE_KEY, 0);
}
public static PendingIntent getRefreshPendingIntent(Context context) {
Intent intent = new Intent(REFRESH_ACTION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
public class VWid extends AppWidgetProvider {
public static String WIDGET_UPDATE = "WIDGET_UPDATE";
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (WIDGET_UPDATE.equals(intent.getAction())) {
Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
}
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
int k, p;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int k = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
Toast.makeText(getActivity(), k+"", Toast.LENGTH_LONG).show();
Intent imageview2Intent= new Intent(context, VWid.class);
PendingIntent pendingIntent2= PendingIntent.getBroadcast(context,0, imageview2Intent,0);
views.setOnClickPendingIntent(R.id.imagebuttonrefresh, pendingIntent2); //
views.setTextViewText(R.id.textview1, "Last Refresh: " + dateFormat.format(new Date()).toString());
pushWidgetUpdate(context, views);
}
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, VWid.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
Log.d("UPDATED", "testing");
}
}
When I first add the widget to my homescreen, textview1 displays: Last Refresh: {current date and time}. It also displays the system volume in a Toast.
Let's say I increase/decrease the system volume, when I hit refresh the onUpdate() function should fire showing the updated Toast and also updating the textview1 text to the new data and time but that's not happening.
How can I modify the code so I can achieve a refresh, in essence calling the onUpdate() function.
Try this:
public class WidgetProvider extends AppWidgetProvider {
public static final String REFRESH_ACTION = "com.packagename.REFRESH_ACTION";
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(WidgetProvider.REFRESH_ACTION)) {
Bundle extras = intent.getExtras();
if (extras != null) {
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds != null && appWidgetIds.length > 0) {
Toast.makeText(context, "REFRESH", Toast.LENGTH_SHORT).show();
this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
}
}
}
}
#Override
public void onUpdate(final Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; ++i) {
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent intentServiceCall = new Intent(context, WidgetService.class);
intentServiceCall.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
//Refresh
Intent refreshIntent = new Intent(context, WidgetProvider.class);
refreshIntent.setAction(WidgetProvider.REFRESH_ACTION);
refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
intentServiceCall.setData(Uri.parse(intentServiceCall.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, refreshIntent, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.ivRefreshWidget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
Change this section :
Intent imageview2Intent= new Intent(context, VWid.class);
imageview2Intent.setAction(WIDGET_UPDATE);
PendingIntent pendingIntent2= PendingIntent.getBroadcast(context,0, imageview2Intent,0);
views.setOnClickPendingIntent(R.id.imagebuttonrefresh, pendingIntent2); //
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (WIDGET_UPDATE.equals(intent.getAction())) {
pushWidgetUpdate(context);
}
}
I'm having some trouble getting a very simple Android widget button that starts an activity working on the Lock Screen. I can confirm the widget works when on the home screen. What do I need to do get the button working?
WidgetProvider.Java
public class WidgetProvider extends AppWidgetProvider {
public static String WIDGET_BUTTON = "com.test.WIDGET_BUTTON";
public static String WIDGET_STOP_BUTTON = "com.test.WIDGET_STOP_BUTTON";
MediaPlayer WidgetMP;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName watchWidget = new ComponentName(context, WidgetProvider.class);
appWidgetManager.updateAppWidget(watchWidget, createWidget(context));
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
//Toast.makeText(context, "onRecieve", Toast.LENGTH_SHORT).show();
if (WIDGET_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "Start Button", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
watchWidget = new ComponentName(context, WidgetProvider.class);
Intent newIntent = new Intent(context, Alarm.class);
newIntent.putExtra("widget", true);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
if (WIDGET_STOP_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "Stop Button", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
watchWidget = new ComponentName(context, WidgetProvider.class);
Intent newIntent = new Intent(context, Test.class);
newIntent.putExtra("widget", false);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager manager,
int appWidgetId, Bundle widgetOptions) {
manager.updateAppWidget(appWidgetId, createWidget(context));
}
private RemoteViews createWidget(Context context) {
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.widget_alarm_button, getPendingSelfIntent(context, WIDGET_BUTTON));
remoteViews.setOnClickPendingIntent(R.id.widget_stop_button, getPendingSelfIntent(context, WIDGET_STOP_BUTTON));
return remoteViews;
}
}
widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:initialKeyguardLayout="#layout/widget"
android:initialLayout="#layout/widget"
android:minHeight="146dp"
android:minWidth="146dp"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen|keyguard"
>
</appwidget-provider>
When I add my widget from the widget page on my phone, it plays the toast message "Button Clicked." The toast is only suppose to be displayed once you push the button, and it works, but it also is displayed when the widget is added to the screen..
I am guessing it may have to do with the super, but I am not exactly sure.
Code:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, Provider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.onoffbutton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
System.out.println("Click!");
}
}
EDIT:
public static String BUTTON_CLICK = "android.appwidget.action.BUTTON_CLICK";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, Provider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
Intent clickIntent = new Intent(context, Provider.class);
clickIntent.setAction(BUTTON_CLICK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.onoffbutton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context context, Intent clickIntent) {
super.onReceive(context, clickIntent);
if (clickIntent.getAction().equals(BUTTON_CLICK)) {
Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
System.out.println("Click!");
}
}
That's because you have same action for widget update
Make a separate intent action for button(BTN_ACTION for example). In onReceive() check which i=action intent has and if intent.getAction().equals(BTN_ACTION) - handle button click.
I'm trying to make a widget containing a textview change its text to a random number on clicking the widget. It gives a problem displaying widget. I have this code:
public class WidgetConfig extends AppWidgetProvider{
public static String RandomClick = "RandomClick";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews updateViews;
ComponentName watchWidget;
updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, WidgetConfig.class);
updateViews.setOnClickPendingIntent(R.id.randomnumber,getPendingSelfIntent(context, RandomClick));
appWidgetManager.updateAppWidget(watchWidget,updateViews);
Random r = new Random();
int randomInt = r.nextInt(6);
String randomStringInt = String.valueOf(randomInt);
final int N = appWidgetIds.length;
for (int i=0;i<N;i++){
int appWidgetID = appWidgetIds[i];
RemoteViews v = new RemoteViews(context.getPackageName(),R.layout.widget_layout);
v.setTextViewText(R.id.randomnumber, randomStringInt);
appWidgetManager.updateAppWidget(appWidgetID, v);
}
}
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RandomClick)){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews updateViews;
ComponentName watchWidget;
updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, WidgetConfig.class);
Random r = new Random();
int randomInt = r.nextInt(6);
String randomStringInt = String.valueOf(randomInt);
updateViews.setTextViewText(R.id.randomnumber, randomStringInt);
appWidgetManager.updateAppWidget(watchWidget, updateViews);
}
super.onReceive(context, intent);
}
protected PendingIntent getPendingSelfIntent(Context context, String action){
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}
In which randomnumber is just the textview in widget_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundshape"
android:layout_margin="8dp"
android:onClick="ClickRandom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:minHeight="56dp"
android:minWidth="56dp"
android:gravity="center"
android:id="#+id/randomnumber"
android:textSize="24dp"/>
</LinearLayout>
Help much appreciated. Thank you!
I have uploaded the complete work.You can even download the code from this link.Well this is the code which i have used.When you click on the widget it will automatically generate a random no and update ui.I have also uploaded an image which shows the output also.
MainActivity.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "App widget ready to be added!",
Toast.LENGTH_LONG).show();
finish();
}
WidgetConfig.java
public class WidgetConfig extends AppWidgetProvider {
#Override
public void onUpdate(Context ctxt, AppWidgetManager mgr,
int[] appWidgetIds) {
ComponentName me=new ComponentName(ctxt, WidgetConfig.class);
mgr.updateAppWidget(me, buildUpdate(ctxt, appWidgetIds));
}
private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(),
R.layout.widget);
Intent i=new Intent(ctxt, WidgetConfig.class);
i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , i,
PendingIntent.FLAG_UPDATE_CURRENT);
int v = (int)(Math.random()*6);
String rs = Integer.toString(v);
CharSequence cs = rs.subSequence(0, 1);
updateViews.setTextViewText(R.id.textView1,cs);
updateViews.setOnClickPendingIntent(R.id.textView1, pi);
updateViews.setOnClickPendingIntent(R.id.background, pi);
return(updateViews);
}
}