lawonga
lawonga

Reputation: 832

Widget on click to change text of widget

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!

Upvotes: 0

Views: 733

Answers (1)

Talha Q
Talha Q

Reputation: 4360

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);
 }
 }

enter image description here

Upvotes: 1

Related Questions