0

create a custom view for color picker that contains image and I'm going to take a color from this image on touch bot neither onTouch(View v, MotionEvent ev) nor onClick(View v) is called.

So here is my view class:

public class ChangeColor extends RelativeLayout {

    public ChangeColor(Context context) { super(context); init(); }
    public ChangeColor(Context context, AttributeSet attrs) { super(context, attrs); init(); }

    private void init(){
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.change_color, this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        ImageView iv = (ImageView)findViewById(R.id.colorScaleImageView);
        final Bitmap bitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();

        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("ImageView", "onClick();");
            }
        });
        iv.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent ev) {
                Log.d("ChangeColor", "onTouch();");
                switch (ev.getAction()){
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    int c = bitmap.getPixel((int)ev.getX(), (int)ev.getY());
                    SettingsManager.setColor(c);
                    Log.d("ChangeColor", "" + c);
                    return false;
                default:
                    return true;
                }
            }
        });
    }
}

and layout xml:

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

    <ImageView
        android:id="@+id/colorScaleImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/seekBar1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/background_color" />

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:maxHeight="8dp"
    android:minHeight="8dp"
    android:paddingLeft="7dp"
    android:paddingRight="7dp"
    android:progressDrawable="@drawable/seekbar_progress"
    android:thumb="@drawable/seekbar_thumb_"
    android:thumbOffset="7dp" />

</RelativeLayout>

So no one log message appear in LogCat.

P.S. I saw few similar questions on SO but they did not helped me. P.P.S. This view is used in fragment and fragment is used in Activity. I added a fullscreen view in that activity with OnClickListener in order to see if clicks go to the parent view and I can see Log message from that background view only I tap outside the image and no message if I tap on image.

Thanks.

oleg.semen
  • 2,901
  • 2
  • 28
  • 56
  • you can not combine both OnClick and OnTouch listeners. use one of them only – Gopal Gopi Dec 26 '13 at 17:11
  • @GopalRao I think you can have both, just if you don't use the implements method. – Blo Dec 26 '13 at 17:18
  • @Fllo what do you mean by "if you don't use the implements method"? can you go more detailed... – Gopal Gopi Dec 26 '13 at 17:21
  • @GopalRao It means when you call these methods like OlegSemen did, it should work. But when you use "public class ChangeColor extends RelativeLayout implements OnClickListener, OnTouchListener ...{" this induces a conflit between these methods. Maybe it's wrong but I saw that several times so, now, I prefer OlegSemen 's way. – Blo Dec 26 '13 at 17:29
  • @GopalRao I belive that at list of them will be called. But anyway if I coment out one of them nothing is changed. I still can't see any log message. – oleg.semen Dec 26 '13 at 17:30
  • I wonder how you are able to add **android:orientation="vertical"** attribute for RelativeLayout – Gopal Gopi Dec 26 '13 at 17:35
  • and if you use Eclipse IDE, some time eclipse will fail to print logs in LogCat. better try showing Toasts in OnClick and OnTouch for testing purpose or restart yor Eclipse and make sure it is printing logs – Gopal Gopi Dec 26 '13 at 17:37

1 Answers1

0

You have extended the relative layout but are not calling it in this example.

In the XML you should implement the class you developed instead of RelativeLayout

try

<?xml version="1.0" encoding="utf-8"?>
<**namespace**.ChangeColor xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/colorScaleImageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBar1"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:clickable="true"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/background_color" />

</**namespace**.ChangeColor>

**EDIT It was correct the first time. To implement it you'll have to attach ChangeColor to another layout, it cannot be called in the layout that it is attempting to inflate when it's called. Ex. ChangeColor inflates change_color.xml, you cannot have the tag NAMESPACE.ChangeColor called in the layout that ChangeColor.init() is inflating.

MrPancake
  • 84
  • 4
  • If I do like in your example inflater can't parse xml in line 2 and if I comment out `init()` method there is no layout at all... Did I missed anythind else ? – oleg.semen Dec 26 '13 at 17:27