Started by Debarghya DasSep 14, 2021

Open
Android Studio: How to create a counter by Button click in a widget?

0 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWES SHARE

How can i save the value in the Counter? If i start my Code, then the button is working, but it will reset to zero, so if i click the "+" Button, so it is always by 1. And when i click the "-" Button, then the count is by "-1". It does not go upper or lower. And every 24h i want a reset to zero. Can someone help me?

Code:

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import java.lang.reflect.Type;

public class Water extends AppWidgetProvider {

private static final String MyOnClickPlus = "Plus";
private static final String MyOnClickMinus = "Minus";
int sum = 0;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.water);
views.setOnClickPendingIntent(R.id.btnplus, getPendingSelfIntent(context, MyOnClickPlus));
views.setOnClickPendingIntent(R.id.btnminus, getPendingSelfIntent(context, MyOnClickMinus));


appWidgetManager.updateAppWidget(appWidgetIds, views);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.water);

if(intent.getAction().toString() == MyOnClickPlus){
sum += 1;
views.setTextViewText(R.id.mengeheute, Integer.toString(sum));
}
else if(intent.getAction().toString() == MyOnClickMinus) {
sum -= 1;
views.setTextViewText(R.id.mengeheute, Integer.toString(sum));
}
updateWidget(context, views);
super.onReceive(context, intent);
}

protected static PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, Water.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}

void updateWidget(Context context, RemoteViews remoteViews){
ComponentName widgetComponent = new ComponentName(context, Water.class);
AppWidgetManager.getInstance(context).updateAppWidget(widgetComponent, remoteViews);
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.water">

<application
android:allowBackup="true"
android:icon="@drawable/drinkwater"
android:label="@string/app_name"
android:roundIcon="@drawable/drinkwater"
android:supportsRtl="true"
android:theme="@style/Theme.Water">

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Water"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/water_info" />
</receiver>
</application>

</manifest>

0 Replies

You must be Logged in to reply
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan