1

am working in android widget and yes there is lot of example is there but unfortunately i could not able to give the exact result..

As i have two image button and need to change the picture while clicking on it

My class WidgetProvider

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

 int widgetId = appWidgetIds[i];


        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.simple_widget);

        Intent intent = new Intent(context, WidgetProvider.class);
  remoteViews.setOnClickPendingIntent(R.id.bluetooth, buildButtonPendingIntent(context));

        remoteViews.setOnClickPendingIntent(R.id.wifi, buildButtonPendingIntent1(context));
   pushWidgetUpdate(context, remoteViews);

public static PendingIntent buildButtonPendingIntent(Context context) {
    Intent intent = new Intent();
    intent.setAction("pl.looksok.intent.action.CHANGE_PICTURE");
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

  public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
    ComponentName myWidget = new ComponentName(context, SimpleWidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(myWidget, remoteViews);
}

My Receiver Class:

public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){
        updateWidgetPictureAndButtonListener(context);
    }
}

private void updateWidgetPictureAndButtonListener(Context context) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.simple_widget);

    remoteViews.setImageViewResource(R.id.bluetooth, getImageToSet());

    //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
    remoteViews.setOnClickPendingIntent(R.id.bluetooth, SimpleWidgetProvider.buildButtonPendingIntent(context));

    remoteViews.setImageViewResource(R.id.wifi, getWifi(context));
    remoteViews.setOnClickPendingIntent(R.id.wifi, SimpleWidgetProvider.buildButtonPendingIntent(context));
 SimpleWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}

     private int getWifi( Context context) {

    WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if(wifi.isWifiEnabled()){
        wifi.setWifiEnabled(false);
        return R.mipmap.ic_wifioff;
    } else{
        wifi.setWifiEnabled(true);
        return R.mipmap.ic_wifi;
    }

}

  private int getImageToSet() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    boolean isEnabled = bluetoothAdapter.isEnabled();
    if (isEnabled) {
        bluetoothAdapter.disable();
        return R.mipmap.ic_batoff;
    } else {
        bluetoothAdapter.enable();
        return R.mipmap.ic_bt;

    }

}

I got one example and this code if i click the image both the onclick method is calling can anyone help me to finish this

Edgar prabhu
  • 563
  • 3
  • 21

0 Answers0