-2

enter image description here Okay, so this is my situation. On the picture that you see here, you have an ImageView which is all green, except for a transparent circle with a black stroke around it. Behind it is a RelativeLayout which has an all yellow background. What needs to happen is, when I click on the circle, the onClickListener for the RelativeLayout should start, but when I click somewhere else on the picture, the onClickListener for the ImageView should start. Is this even possible? If it is, than what should I do?

Some extra clarification:

  • When the circle is clicked, the onClickListener of the ImageView is started, instead of RelativeLayout.
  • The circle is a part of the ImageView
  • Adding an extra View over the circle can't be the answer, because I'm trying to avoid that

Java:

package com.nicksoft.myapplication;

import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.imageView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "IMG", Toast.LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.rl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "BG", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFF00"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_test" />
</RelativeLayout>
Nick
  • 45
  • 8
  • 1
    Which picture are you referring to? – Amongalen Aug 05 '20 at 14:16
  • Was writing on my phone, so I accidently haven't added one. Sorry about that... – Nick Aug 05 '20 at 14:17
  • Please read [mcve] ... and include your efforts. Questions that only ask others to design/solve your requirements do most often not receive much positive attention. – GhostCat Aug 05 '20 at 14:18
  • And note: you dont need that large of a screenshot. – GhostCat Aug 05 '20 at 14:18
  • Just `return true` in the circle `onClickListener` which should signal that you handled the click. – Bruno Bieri Aug 05 '20 at 14:35
  • @BrunoBieri I didn't quite understood you. Could you be able to maybe write it in code? – Nick Aug 05 '20 at 15:11
  • I see you want basically a "hole" in your view. When the hole is clicked your `RelativeLayout` shall get the click and if you miss the hole your `ImageView` should get it. Right? This can help you: https://stackoverflow.com/q/19947835 but I for a click listener you probably need to a custom view which handles the hole in the `onTouch` event. – Bruno Bieri Aug 05 '20 at 15:37

1 Answers1

0

Maybe add another image over that big green one(making the transparent circle effect), and listen the onCLickListener to that image instead of RelativeLayout

Rui Alves
  • 161
  • 1
  • 7