Home > other >  how to set onclick Imageview is on other Imageview
how to set onclick Imageview is on other Imageview

Time:11-29

I am have a xml layout, when I set onClick in java code to imageview with the id iv_contact and imageview with id iv_user, the imageview on top doesn't work, it caught the onClick event of the bottom ImageView, how to solve this problem?

This is my xml layout:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@ id/cl_contains_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@ id/iv_contact"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:scaleType="centerCrop" />

    <androidx.cardview.widget.CardView
        android:id="@ id/cv_video_send"
        android:layout_width="@dimen/dp0"
        android:layout_height="@dimen/dp0"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="@dimen/dp12"
        android:layout_marginRight="@dimen/dp12"
        app:cardCornerRadius="@dimen/dp16"
        app:layout_constraintHeight_percent="0.25"
        app:layout_constraintWidth_percent="0.3"
        app:layout_constraintWidth_min="@dimen/dp110"
        app:layout_constraintHeight_min="@dimen/dp175">

        <ImageView
            android:id="@ id/iv_user"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

This is the setOnClick code:

ivImageUser.setOnClickListener(v -> {
    Intent getIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(getIntent, REQUEST_USER_IMAGE);
});
ivContact.setOnClickListener(v -> {
    Intent getIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(getIntent, REQUEST_CONTACT_IMAGE);
});

I tried different ways like setting android:clickable="true" however the results are not very positive, any help is greatly appreciated. Thanks everyone

CodePudding user response:

Hello Please Use iv_contact Imageview Bottom of CardView it will work.

<androidx.cardview.widget.CardView
    android:id="@ id/cv_video_send"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="12dp"
    android:layout_marginRight="@dimen/margin_12dp"
    app:cardCornerRadius="@dimen/text_size_16"
    app:layout_constraintHeight_min="175dp"
    app:layout_constraintHeight_percent="0.25"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_min="@dimen/margin_110dp"
    app:layout_constraintWidth_percent="0.3">

    <ImageView
        android:id="@ id/iv_user"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        

</androidx.cardview.widget.CardView>

<ImageView
    android:id="@ id/iv_contact"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

CodePudding user response:

Make sure the request codes REQUEST_USER_IMAGE & REQUEST_CONTACT_IMAGE have different values e.g. REQUEST_USER_IMAGE = 100 & REQUEST_CONTACT_IMAGE = 200 And handle it accordingly in onActivityResult()

if(requestCode == 100)
    doSomeOperations();
else if(requestCode == 200)
    doSomeMoreOperations();

NOTE:

Don't use deprecated method `startActivityForResult()`. Not Recommended.
Try this way:
public void openSomeActivityForResult() {
    Intent intent = new Intent(this, SomeActivity.class);
    someActivityResultLauncher.launch(intent);
}

// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();
                    doSomeOperations();
                }
            }
        });
  • Related