简体   繁体   中英

Android Ontouchlistener not showing correct id of the imageview when it's clicked

I have a fragment containing 6 imageviews in its layout of my android application. When I click on the images, It should toast me the id of the imageview. But instead it is toasting as false, when I click on any of the imageviews. My images are of triangular in shape and to check whether I have clicked inside or not of the image, I have used on touch listener in my project. But it is not showing me the id of the imageview when I click on it. I'm using a navigation drawer in my project and so I'm using here fragments instead of activity. Here is my code:

HomeFragment.java (Edited)

public class HomeFragment extends Fragment {

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        final OnTouchListener changeColorListener = new OnTouchListener() {

            @Override
            public boolean onTouch(final View v, MotionEvent event) {

                Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
                int color = 0;
                try {
                    color = bmp.getPixel((int) event.getX(), (int) event.getY());
                } catch (Exception e) {

                }
                if (color == Color.TRANSPARENT)
                    return false;
                else {
 //edited part
 if(v.getId() == R.id.loginbutton){
                  Toast.makeText(getActivity(), "Login button", Toast.LENGTH_SHORT).show();
              }
 //edited part
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            Toast.makeText(getActivity(), v.getId(), Toast.LENGTH_SHORT).show();
                            break;
                        case MotionEvent.ACTION_OUTSIDE:
                            break;
                        case MotionEvent.ACTION_CANCEL:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            break;
                        case MotionEvent.ACTION_SCROLL:
                            break;
                        case MotionEvent.ACTION_UP:
                            break;
                        default:
                            break;
                    }
                    return true;
                }
            }


        };
        ImageView loginbutton=(ImageView)rootView.findViewById(R.id.loginbutton);
        loginbutton.setDrawingCacheEnabled(true);
        loginbutton.setOnTouchListener(changeColorListener); 

        ImageView flightbutton=(ImageView)rootView.findViewById(R.id.flightbutton);
        flightbutton.setDrawingCacheEnabled(true);
        flightbutton.setOnTouchListener(changeColorListener); 

        ImageView registerbutton=(ImageView)rootView.findViewById(R.id.hotelbutton);
        registerbutton.setDrawingCacheEnabled(true);
        registerbutton.setOnTouchListener(changeColorListener); 

        ImageView supportbutton=(ImageView)rootView.findViewById(R.id.supportbutton);
        supportbutton.setDrawingCacheEnabled(true);
        supportbutton.setOnTouchListener(changeColorListener);                   

        ImageView aboutusbutton=(ImageView)rootView.findViewById(R.id.aboutusbutton);
        aboutusbutton.setDrawingCacheEnabled(true);
        aboutusbutton.setOnTouchListener(changeColorListener);                   

        ImageView contactbutton=(ImageView)rootView.findViewById(R.id.contactbutton);
        contactbutton.setDrawingCacheEnabled(true);
        contactbutton.setOnTouchListener(changeColorListener);                   

        return rootView;
    }

}

fragment_home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/supportbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="31dp"
        android:background="@drawable/support" />

    <ImageView
        android:id="@+id/flightbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@+id/supportbutton"
        android:layout_alignLeft="@+id/supportbutton"
        android:layout_marginBottom="17dp"
        android:background="@drawable/flight3" />

    <ImageView
        android:id="@+id/hotelbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/flightbutton"
        android:layout_alignBottom="@+id/flightbutton"
        android:layout_centerHorizontal="true"
        android:background="@drawable/hotel2" />

    <ImageView
        android:id="@+id/aboutusbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/hotelbutton"
        android:layout_below="@+id/flightbutton"
        android:background="@drawable/aboutus" />

    <ImageView
        android:id="@+id/loginbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignBaseline="@+id/hotelbutton"
        android:layout_alignBottom="@+id/hotelbutton"
        android:layout_alignParentRight="true"
        android:layout_marginRight="37dp"
        android:background="@drawable/login" />

    <ImageView
        android:id="@+id/contactbutton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/loginbutton"
        android:layout_centerVertical="true"
        android:background="@drawable/contact"
        android:fitsSystemWindows="false" />

</RelativeLayout>

Please correct me if I'm wrong.

Edit: Actually my intention was to check which button was clicked actually. The problem is when I added the edited part and run the app, It's intersting that when I click on the contact image, actually the toast is showing as Login button. But I have given in the else condition with the id of the login imageview, so that when I click on the login image, it should trigger the toast. Instead on clicking the contact image, the toast for login is actually called. How is that possible? Is there any solution for it...

From the documentation of Toast.makeText() there are two makeText() methods:

  • public static Toast makeText (Context context, int resId, int duration)
  • public static Toast makeText (Context context, CharSequence text, int duration)

I assume you want to display the ID as a string, but in this case you are passing v.getId() which returns an int, so actually the first method is called, which looks for a resource. So in the first place you could make it
Toast.makeText(getActivity(), v.getId() + "", Toast.LENGTH_SHORT).show() which will turn your second argument into a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM