Android: how to do background clickable effect by programmatically?

273 views Asked by At

I have a background for button.

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@android:color/transparent" />
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/whiteTransparent50" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/whiteTransparent50" />
    <item android:drawable="@android:color/transparent" />
</selector>

But this is not my want. Because i have a one main image. And image has a two section which i call A and B. When i press A section main image is changing to C image. and When i press B section main image is changing to D image. So my solution is:

A.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
//                    action pressed
                    case MotionEvent.ACTION_DOWN:{
                        Glide.with(getActivity())
                                .load(R.drawable.C_image)
                                .into(iv);
                        return true;
                    }
                    case MotionEvent.ACTION_UP:{
                        Glide.with(getActivity())
                                .load(R.drawable.main_image)
                                .into(iv);
                        return true;
                    }
                }
                return false;
            }
        });

        B.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
//                    action pressed
                    case MotionEvent.ACTION_DOWN:{
                        Glide.with(getActivity())
                                .load(R.drawable.D_image)
                                .into(iv);
                        return true;
                    }
                    case MotionEvent.ACTION_UP:{
                        Glide.with(getActivity())
                                .load(R.drawable.main_image)
                                .into(iv);
                        return true;
                    }
                }
                return false;
            }
        });

But when i do it setOnclickListener's not working? So my thought is: (android:state_focused="false" android:state_pressed="true) it's working.(android:state_focused="true" android:state_pressed="true") it's not working. How to do it?

0

There are 0 answers