Processing More Than One Button Click at Android Widget

Processing more than one button click at Android Widget

That works, our Widget class that extends AppWidgetProvider:

public class ExampleProvider extends AppWidgetProvider {

// our actions for our buttons
public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
public static String ACTION_WIDGET_SETTINGS = "ActionReceiverSettings";
public static String ACTION_WIDGET_ABOUT = "ActionReceiverAbout";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main_layout);

Intent active = new Intent(context, ExampleProvider.class);
active.setAction(ACTION_WIDGET_REFRESH);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button_refresh, actionPendingIntent);

active = new Intent(context, ExampleProvider.class);
active.setAction(ACTION_WIDGET_SETTINGS);
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button_settings, actionPendingIntent);

active = new Intent(context, ExampleProvider.class);
active.setAction(ACTION_WIDGET_ABOUT);
actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.button_about, actionPendingIntent);

appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
Log.i("onReceive", ACTION_WIDGET_REFRESH);
} else if (intent.getAction().equals(ACTION_WIDGET_SETTINGS)) {
Log.i("onReceive", ACTION_WIDGET_SETTINGS);
} else if (intent.getAction().equals(ACTION_WIDGET_ABOUT)) {
Log.i("onReceive", ACTION_WIDGET_ABOUT);
} else {
super.onReceive(context, intent);
}
}
...

And AndroidManifest.xml where we must register our actions:

    <receiver android:name="ExampleProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_REFRESH"/>
<action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_SETTINGS"/>
<action android:name="org.divenvrsk.widgets.ExampleProvider.ACTION_WIDGET_ABOUT"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info"/>
</receiver>
</application>

Handling multiple buttons inside a widget - Android

Just make pending intents on buttons with action. So in onReceive() just check action of intent and make some logic on that. If you make some long tasks, better to make all logic in IntentService.

Set pending intents on your buttons that are in RemoteViews like this:

public static final String ACTION_BUTTON_SHARE = "ACTION_BUTTON_SHARE";
public static final String ACTION_BUTTON_REFRESH = "ACTION_BUTTON_REFRESH";

Intent refreshIntent = new Intent(context, ExampleAppWidget.class)
.setAction(ACTION_BUTTON_REFRESH);
PendingIntent refreshPI = PendingIntent.getBroadcast(context, 0, refreshIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.refresh_button, refreshPI);

Intent shareIntent = new Intent(context, ExampleAppWidget.class)
.setAction(ACTION_BUTTON_SHARE);
PendingIntent sharePI = PendingIntent.getBroadcast(context, 0, shareIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.share_button, sharePI);

In ExampleAppWidget.class (that extends from AppWidgetProvider) override OnReceive() method

@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
if (intent.getAction().equals(ACTION_BUTTON_REFRESH)) {
//make some logic here on button refresh
} else if (intent.getAction().equals(ACTION_BUTTON_SHARE)) {
//make some logic here on button share
} else {
super.onReceive(context, intent);
}
}
}

How to avoid multiple button click at same time in android?

The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span).
Example:

// Make your activity class to implement View.OnClickListener
public class MenuPricipalScreen extends Activity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
// setup listeners.
findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);
findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);
...
}

.
.
.

// variable to track event time
private long mLastClickTime = 0;

// View.OnClickListener.onClick method defination

@Override
public void onClick(View v) {
// Preventing multiple clicks, using threshold of 1 second
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();

// Handle button clicks
if (v == R.id.imageView2) {
// Do your stuff.
} else if (v == R.id.imageView3) {
// Do your stuff.
}
...
}

.
.
.

}

Multiple button handling in android

Solution: Out of a group of buttons, at a time only one button will be translated to given x, y rest of the buttons will be reset to original position if already translated.

activity_buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ButtonsActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="149dp"
android:layout_marginTop="87dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="2dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintStart_toStartOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="2dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toEndOf="@+id/button5"
app:layout_constraintTop_toBottomOf="@+id/button3" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="17dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button4" />

</androidx.constraintlayout.widget.ConstraintLayout>

ButtonsActivity.kt

class ButtonsActivity : AppCompatActivity() {
private val buttonList = mutableListOf<Button>()
var activatedButtonIndex = -1

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_buttons)

val button1 = findViewById<Button>(R.id.button)
val button2 = findViewById<Button>(R.id.button2)
val button3 = findViewById<Button>(R.id.button3)
val button4 = findViewById<Button>(R.id.button4)
val button5 = findViewById<Button>(R.id.button5)

button1.setOnClickListener(listener)
button2.setOnClickListener(listener)
button3.setOnClickListener(listener)
button4.setOnClickListener(listener)
button5.setOnClickListener(listener)

//set tags for dynamically identify the object in foreach instead of resource id
button1.tag = 0
button2.tag = 1
button3.tag = 2
button4.tag = 3
button5.tag = 4
buttonList.add(button1)
buttonList.add(button2)
buttonList.add(button3)
buttonList.add(button4)
buttonList.add(button5)
}

private val listener = View.OnClickListener { view ->
val tag: Int = view.tag as Int
view?.let {
if (activatedButtonIndex == tag) {
return@let
}
startTranslateAnim(it)
if (activatedButtonIndex != tag) {
resetTranslationAnim(activatedButtonIndex)
}
activatedButtonIndex = tag
}
}

private fun startTranslateAnim(it: View) {
it.animate()
.scaleXBy(0.1f)
.scaleYBy(0.1f)
.duration = 200
}

private fun resetTranslationAnim(previousSelectedIndex: Int) {
buttonList?.let {
it.forEachIndexed { index, button ->
if (index == previousSelectedIndex)
button.animate().scaleXBy(-0.1f)
.scaleYBy(-0.1f)
.duration = 200
}
}
}
}

Handling android onclick for multiple buttons (81 total)

For all elements set onClick with a same name:

<LinearLayout
//blah blah
android:onClick="myClickFunction"
/>

and then in your Java file you will need only this one click Listener:

   public void myClickFunction(View v) {
String mytag=(String) v.getTag();
// And do something with tag or id
}

android - Widget button click count

You can set the onclickpendingintent like this

views.setImageViewResource(R.id.imagebutton, R.drawable.buttonw);
Intent i = new Intent(context, top.class);
i.putExtra("widget", "1");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
views.setOnClickPendingIntent(R.id.imagebutton, pendingIntent);

it's an example to set onclick to an imageview that will launch the application when the imageview clicked

if you have any question feel free to ask in the comment

Handling android onclick for multiple buttons (81 total)

For all elements set onClick with a same name:

<LinearLayout
//blah blah
android:onClick="myClickFunction"
/>

and then in your Java file you will need only this one click Listener:

   public void myClickFunction(View v) {
String mytag=(String) v.getTag();
// And do something with tag or id
}


Related Topics



Leave a reply



Submit