-2

I have a layout with images and I want that when I tap on each of them they show a specific message on the screen. my problem is that when I am tapping on any imageview in the layout I am getting only the message I set for the first one. Here is my code can you tell me what the problem is: earlir I tried this code in the method on singletap: int id=0 case 0:id=R.id.imageView1 but this doesnot work as well please help

gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

          public boolean onSingleTapConfirmed(MotionEvent e){
              Toast.makeText(Creaacount_5.this, "single tap", Toast.LENGTH_SHORT).show();
              EditText b=(EditText)findViewById(R.id.editText1);
                 b.append("*");  

                 int id=0;
                 ImageView i=(ImageView) findViewById(R.id.imageView1);
                 id=i.getId();

                 int id1;
                 ImageView b1=(ImageView) findViewById(R.id.imageView2);
                 id1=b1.getId();
                 int id2;
                 ImageView c=(ImageView) findViewById(R.id.imageView2);
                 id2=c.getId();

                 int v=i.getId();

                     if(id==R.id.imageView1){

                      EditText t1 = (EditText)findViewById(R.id.editText1);
                       id=R.drawable.playing;
                      SpannableString ss = new SpannableString("abc");
                      ImageView r=(ImageView) findViewById(R.id.imageView1);
                        Drawable d = r.getDrawable();
                        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                        ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
                        ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                        t1.append(ss);
                       id=R.drawable.playing;
                       Toast.makeText(Creaacount_5.this,"play",Toast.LENGTH_SHORT).show();

                       SharedPreferences s1=getSharedPreferences(MyPreferences,MODE_PRIVATE);
                       SharedPreferences.Editor editor1=s1.edit();
                       editor1.putInt("play", R.drawable.playing);
                     }
Ray
  • 31
  • 6

2 Answers2

0

Instead of doing much, you can use this simple Listener for each of your images:

ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
       // your code here
    }
});

Also, you can use android:onClick = "MyMethod" in each of your image views in XML, and then create a method in Java:

public void MyMethod(View view){

    if(view.getId()==image1)
    // do something with that image
    else if(view.getId()==image2)
    // do something with that image
    .
    .
    . 
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Ajeet
  • 1,540
  • 1
  • 17
  • 30
  • Actually I have to use a double tap as well – Ray Mar 27 '15 at 07:50
  • I have set the touch listener for each image as well – Ray Mar 27 '15 at 07:54
  • Is it necessary to use a double tap ? A better gesture would be a long hold which could be implemented easily . – Ajeet Mar 27 '15 at 07:57
  • And if you still want to use double tap i could help tell me :) – Ajeet Mar 27 '15 at 07:58
  • yes it is necessary for my project to use a double tap because it is the project's requirements – Ray Mar 27 '15 at 07:58
  • on each single tap or double tap a message must appear to the user for eg if you tap on father the message father must appear or double then fathers must appear. but in my case whenever I am tapping on any image in my layout I am getting the message I tried to set for the first one according to the code above – Ray Mar 27 '15 at 08:00
  • I found a simple solution here have a look http://stackoverflow.com/questions/13530937/how-to-listen-to-doubletap-on-a-view-in-android ..they have created a custom view which you can use in your xml – Ajeet Mar 27 '15 at 08:01
  • but I have to set the touch listener for each image – Ray Mar 27 '15 at 08:06
  • I am getting problem just for the message. earlier I tried to use this code int id=0 switch(id){ case 1: id=R.id.imageView1 Toast.makeText(); – Ray Mar 27 '15 at 08:09
0

Here's how you can try the above,just an overview though:

boolean firstTouch = false;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == event.ACTION_DOWN){
            if(firstTouch && (Helper.getCurrentTimeInMilliSeconds() - time) <= 300) {
                //do stuff here for double tap
                Log.e("** DOUBLE TAP**"," second tap ");
                firstTouch = false;

            } else {
                firstTouch = true;
                time = Helper.getCurrentTimeInMilliSeconds();
                Log.e("** SINGLE  TAP**"," First Tap time  "+time);
                return false;
            }
        }
        return true;
}

Please let me know if it helps.

Steve Kamau
  • 2,755
  • 10
  • 42
  • 73