Home > Blockchain >  Android button first and second click
Android button first and second click

Time:08-17

I need help with button first and second click. first click should send an sms and changing button background color and button text color. Second click should send an other sms and set back button backcolor and text color. thanks.

public boolean onLongClick(View v) {
    switch (v.getId()){
        case R.id.button1:
            btn1.setBackgroundColor(Color.GREEN);
            btn1.setTextColor(Color.RED);
            sendSMSMessage1();

            // On second click do other event 

            sendSMSMessage11();
            btn1.setBackgroundColor(Color.BLACK);
            btn1.setTextColor(Color.WHITE);

            break;
        case R.id.button2:
            btn2.setBackgroundColor(Color.GREEN);
            btn2.setTextColor(Color.RED);
            sendSMSMessage2();
            // On second click do other event

            sendSMSMessage22();

            btn2.setBackgroundColor(Color.BLACK);
            btn2.setTextColor(Color.WHITE);

            break;
    }
    return false;
}

xml

<Button
    android:id="@ id/button1"
    style="@style/Widget.Material3.Button.OutlinedButton"
    android:layout_width="214dp"
    android:layout_height="70dp"
    android:layout_centerHorizontal="true"
    android:layout_marginStart="123dp"
    android:layout_marginEnd="124dp"
    android:layout_marginBottom="372dp"
    android:text="START"
    android:textAlignment="center"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:textColor="#ffffff"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.521"
    app:layout_constraintStart_toStartOf="parent"
    app:strokeColor="#ffffff"
    app:strokeWidth="5dp" />

CodePudding user response:

You can do it via additional flag. For example

boolean wasButtonClicked = false;

button.setOnLongClickListener {
  if (wasButtonClicked) {
    //action 2
  } else {
    //action1
  }
}

CodePudding user response:

this way did not work.

        case R.id.button1:
            boolean wasButtonClicked = false;
            if (wasButtonClicked) {
                btn1.setBackgroundColor(Color.GREEN);
                btn1.setTextColor(Color.RED);
                sendSMSMessage1();
            }
            else {
                btn1.setBackgroundColor(Color.BLACK);
                btn1.setTextColor(Color.WHITE);
            }
            break;
  • Related