How to change the image in app widget programmatically in android
Asked Answered
C

2

5

I design an android app widget in which two ImageView i want change the image both of the the ImageView when click on the image as well as call a method. Now i use RemoteView like this

RemoteViews views1 = new RemoteViews(context.getPackageName(),
                R.layout.activity_main);
        views1.setOnClickPendingIntent(R.id.smsImageView, pendIntent1);
        appWidgetManager.updateAppWidget(appWidgetIds, views1);

but i have no idea how i change the image in imageView Thanks.

Cybernetics answered 30/1, 2013 at 6:23 Comment(2)
just go through this.. #14133482Demanding
how i call a method when click on image like imageView..setOnClickListener(new OnClickListener() { public void onClick(View v) {Cybernetics
D
8

Widget_Layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/imageview1_image1"/>

    <ImageView
        android:id="@+id/imageview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:src="@drawable/imageview2_image1" />

</LinearLayout>

In your AppWidgetProvider :

public class SwitchWidget extends AppWidgetProvider {
public static String SWITCH_WIDGET_UPDATE = "MainActivity.Switch";
public static String IMAGEVIEW1 = "imageview1";
public static String IMAGEVIEW2 = "imageview2";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // TODO Auto-generated method stub
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    updateSwitch1(context, appWidgetManager, appWidgetIds[0]);
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);
    Log.d("SWitch Widget", "On Receive");




    RemoteViews remoteViews;

    AppWidgetManager appWidgetManager = AppWidgetManager
            .getInstance(context);

    if (SWITCH_WIDGET_UPDATE.equals(intent.getAction())) {
        Log.d("SWitch Widget", "Widget Choose");
        ComponentName thisAppWidget = new ComponentName(
                context.getPackageName(), getClass().getName());

        int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
        for (int appWidgetID : ids) {

                updateSwitch1(context, appWidgetManager, appWidgetID);
            } 

        }

    }
    if (intent.getAction().equals(IMAGEVIEW1)) {




            remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget1);
            if(flag){
            flag = flase           
                remoteViews.setImageViewResource(R.id.imageview1,
                        R.drawable.imageview1_image2);
                 startService(new Intent(this, YOUR_SERVICE_CLASS_NAME.class)); 
            } else {
            flag = true;
                remoteViews.setImageViewResource(R.id.imageview1,
                        R.drawable.imageview1_image1);
                startService(new Intent(this, YOUR_SERVICE_CLASS_NAME.class));
            }
            appWidgetManager.updateAppWidget(new ComponentName(context,
                    SwitchWidget.class), remoteViews);


        }
        else if (intent.getAction().equals(IMAGEVIEW2)) {

            remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget1);
            if(flag){
         flag = flase          
                remoteViews.setImageViewResource(R.id.imageview2,
                        R.drawable.imageview2_image2);
            } else {
        flag = true;
                remoteViews.setImageViewResource(R.id.imageview2,
                        R.drawable.imageview2_image1);
            }
            appWidgetManager.updateAppWidget(new ComponentName(context,
                    SwitchWidget.class), remoteViews);

      }



private void updateSwitch1(Context context,
        AppWidgetManager appWidgetManager, int appWidgetId) {
    // TODO Auto-generated method stub
    Log.d("SWitch Widget", "Switch1");
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget1);



    Intent imageview1Intent= new Intent(context, SwitchWidget.class);
    wifiIntent.putExtra("ID", appWidgetId);
    wifiIntent.setAction(IMAGEVIEW1);
    PendingIntent imageview1PendingIntent = PendingIntent.getBroadcast(context,
            0, imageview1Intent,0);
    remoteViews.setOnClickPendingIntent(R.id.imageview1, imageview1PendingIntent);

    Intent imageview2Intent= new Intent(context, SwitchWidget.class);
    wifiIntent.putExtra("ID", appWidgetId);
    wifiIntent.setAction(IMAGEVIEW2);
    PendingIntent imageview2PendingIntent = PendingIntent.getBroadcast(context,
            0, imageview2Intent,0);
    remoteViews.setOnClickPendingIntent(R.id.imageview2, imageview2PendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }

  }

And also in your widget receiver add this two actions :

 <action android:name="imageview1" />
  <action android:name="imageview2" />
Demanding answered 30/1, 2013 at 7:11 Comment(4)
@NaveenKumar Check my answer and notify me whether this helps you or not.Demanding
i also want to start a service when click on ImageView how i implement thisCybernetics
yes i made but i don't know how to start a service when click on imageView in app widgetsCybernetics
thanks @Rahil2952 after some modifications it work me fine..thanks againCybernetics
K
0

just call function to set images resources for it.

view1.setImageViewResources(R.id.img, IMGS[1])

then you got it

Katherinakatherine answered 30/1, 2013 at 6:40 Comment(1)
how i call a method when click on image like imageView..setOnClickListener(new OnClickListener() { public void onClick(View v) { });Cybernetics

© 2022 - 2024 — McMap. All rights reserved.