code Android Click an event two buttons

122 views Asked by At

I am new in the Android I want a full example as in the picture Please help my thank you.

enter image description here

3

There are 3 answers

1
Danieboy On

The multitouch part

So I've been digging around in this subject and come across some things to keep in mind.

android:splitMotionEvents="true"

The above setting needs to be set.

Also what you'll probably want to read about is Android Multitouch.

I won't be able to give you an example from scratch for such a broad question but I'll send you some useful links to get you started.


1. Android work multitouch button [StackOverflow]

2. How to code for multitouch [StackOverflow]

3. Handling Multi-Touch Gestures [developer.android.com]

4. How to use Multi-touch in Android 2 [zdnet.com]

5. How to use Multi-touch in Android 2: Part 2, Building the Touch example [zdnet.com]


I would assume you are looking for something like the 5th link. Again, I won't post that code here for the sake of it.

The toast part

So after you've implemented the first above part you simply need to add the following code to that event (where you've checked that both buttons are clicked at the same time).

// All of this inside your OnClick-event 

    // If (both buttons are clicked) 
    {
        Toast.makeText(getApplicationContext(), "Show this message", Toast.LENGTH_SHORT);
    }

    if(toast != null) 
        toast.show();

// End your OnClick-event.
1
Anjal Saneen On
    boolean button1_clicked=false, button2_clicked=false;

    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            button1_clicked =true;

            if(button1_clicked && button2_clicked){
                Toast.makeText(context, "Toast", Toast.LENGTH_SHORT).show();
                button1_clicked =false;
            }

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    button1_clicked =false;
                }
            }, 500);
        }
    });
    button2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            button2_clicked =true;

            if(button1_clicked && button2_clicked){
                Toast.makeText(context, "Toast", Toast.LENGTH_SHORT).show();
                button2_clicked =false;
            }

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    button2_clicked =false;
                }
            }, 500);
        }
    });

I Think this handler helps to reset the variable after 500 milli second.

11
Curio On

I used this way:

public class MainActivity extends AppCompatActivity
{
private long start;
private long finish;
private boolean ciclo=true;
private Activity activity;
private boolean show;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    activity = this;
}

public void onClick(View arg0)
{
    new MultiplyTask().execute();

    switch(arg0.getId())
    {
        case R.id.button:
            start = System.currentTimeMillis();
            break;
        case R.id.button2:
            finish = System.currentTimeMillis();
    }
}

public class MultiplyTask extends AsyncTask<String,String,String>
{
    @Override
    public void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    public void onPostExecute(String s)
    {
        super.onPostExecute(s);
        if(show)
        {
            Toast.makeText(activity, "Yes", Toast.LENGTH_LONG).show();
            ciclo = true;
            show=false;
        }
    }

    @Override
    public String doInBackground(String... params)
    {
        while(ciclo)
        {
            if(Math.abs(finish-start)<50)
            {
                show=true;
                ciclo=false;
            }
        }

        return null;
    }
}

}

And the layout has to be like this:

    <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="69dp"
    android:layout_marginStart="69dp"
    android:layout_marginTop="215dp"
    android:id="@+id/button"
    android:onClick="onClick" />

    <Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:onClick="onClick"
    android:layout_alignBaseline="@+id/button"
    android:layout_alignBottom="@+id/button"
    android:layout_toRightOf="@+id/button"
    android:layout_toEndOf="@+id/button"
    android:layout_marginLeft="69dp"
    android:layout_marginStart="69dp" />
    </RelativeLayout>