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

- 1,866
- 16
- 31

- 1
-
1Goto google .How to show Toast in Android – IntelliJ Amiya Jan 04 '17 at 13:02
-
http://stackoverflow.com/questions/8202006/android-have-toast-appear-on-button-click – IntelliJ Amiya Jan 04 '17 at 13:03
-
not sure if it's a good ux. – meh Jan 04 '17 at 13:03
-
1Do you specifically only want to show the toast if both buttons are pressed at the same time? Or if any of the buttons are pressed? – Danieboy Jan 04 '17 at 13:05
-
Yes, I want to show toast if pressed two buttons at the same time – rokrol Jan 04 '17 at 13:12
3 Answers
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.
-
Thank you so much.I need an example of a full pressed two buttons at the same time As in the picture I would understand that a good example of – rokrol Jan 04 '17 at 18:49
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.

- 3,109
- 23
- 38
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>

- 1,331
- 2
- 14
- 34
-
-
-
-
-
-
-
-
It has been used Sdk 7.1 and 4.0.3 and 4.0 But there is the same problem comes out of the application – rokrol Jan 04 '17 at 19:52
-
-
-