change icon when textview is clicked in widget

戏子无情 提交于 2019-12-24 18:26:20

问题


I have create a widget in android , whenever i click on Widget TextView one service get starts but at the same time i want to change the icon which is shown on the right side.

I can set a onclick over Widget TextView using

remoteViews.setOnClickPendingIntent(R.id.widgetTxt,pendingIntent);

but i don't know where i can change the icon when the TextView get clicks

image

@Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {

                        // Here i am trying to set a onclick over TextView
    Intent intent = new Intent(context,MyService.class);
            PendingIntent pendingIntent = PendingIntent.getService(context,0 , 
                    intent, 0);

            remoteViews.setOnClickPendingIntent(R.id.MyWidgetTxt,pendingIntent);

    }

When an icon itself gets clicked , it changes too and i have set its code in onReceive


回答1:


try this :

In manifest.xml

 <receiver android:name="TestwidgetProvider"
            android:label="@string/widget_title" android:exported="false" android:icon="@drawable/test">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.imranandroid.demo.ACTION_ON_ImG_CLICK"/>
                <action android:name="android.appwidget.action.APPWIDGET_DELETED"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>

In AppWidgetProvider.java :

  public class TestwidgetProvider extends AppWidgetProvider {
        public static final String TAG_IMAGE_CLICK = "com.imranandroid.demo.ACTION_ON_ImG_CLICK";
        public static RemoteViews rview;
public static int ii=0;
        @Override
        public void onUpdate(Context paramContext, AppWidgetManager appWidgetManager, 
                int[] appWidgetIds){
            //REMOVE YOUR CODE FROM HERE
            updateWidgetState(paramContext, "");    
        }
        @Override
        public void onReceive(Context paramContext, Intent paramIntent)
          {
             String str = paramIntent.getAction();
            if (paramIntent.getAction().equals(TAG_IMAGE_CLICK)) {
                updateWidgetState(paramContext, str);   
            }
            else
            {
              super.onReceive(paramContext, paramIntent);
            }
          }
         static void updateWidgetState(Context paramContext, String paramString)
          {
            RemoteViews localRemoteViews = buildUpdate(paramContext, paramString); //CALL HERE
            ComponentName localComponentName = new ComponentName(paramContext, TestwidgetProvider.class);
            AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
          }
     private static RemoteViews buildUpdate(Context paramContext, String paramString)
      {
         rview = new RemoteViews(paramContext.getPackageName(), R.layout.widget_layoutmain);
         Intent active = new Intent(paramContext, TestwidgetProvider.class); //YOUR WIDGET NAME
         active.setAction(TAG_IMAGE_CLICK);
         PendingIntent actionPendingIntent = PendingIntent.getBroadcast(paramContext, 0, active, 0);
         rview.setOnClickPendingIntent(R.id.MyWidgetTxt, actionPendingIntent);
         if (paramString.equals(TAG_IMAGE_CLICK)) {
         if(ii==0)
            {
            rview.setImageViewResource(R.id.ImageView01a, R.drawable.off);
                    ii=1;
            }
            else
            {
            rview.setImageViewResource(R.id.ImageView01a, R.drawable.on);
                    ii=0;
            }
            }
         return rview;
      }
     }


来源:https://stackoverflow.com/questions/9851000/change-icon-when-textview-is-clicked-in-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!