I want to achieve a design as shown above in the screenshot. The image is fetched from a URL and I want to add a click listener in the image only. Tried using Image span but with no success. Could somebody help me through this?
Thank you
I want to achieve a design as shown above in the screenshot. The image is fetched from a URL and I want to add a click listener in the image only. Tried using Image span but with no success. Could somebody help me through this?
Thank you
On
You can set another span call ClickableSpan at the same place of the ImageSpan. Here an example code:
builder.setSpan(new ClickableSpan() {
@Override
public void onClick(@NonNull View widget) {
Toast.makeText(context, "icon clicked", Toast.LENGTH_SHORT).show();
}
},
builder.length() - 1,
builder.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.tvTitle.setText(builder);
holder.tvTitle.setMovementMethod(LinkMovementMethod.getInstance());
It is important to setMovementMethod for the click to work.
On
I don't know your limitation, but you can using a layout like bellow:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="5dp">
<TextView
android:id="@+id/top_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Papaya (Around)"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/bottom_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=",(2kg)"
app:layout_constraintTop_toBottomOf="@id/top_tv"
app:layout_constraintStart_toStartOf="@id/top_tv"/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_help"
app:layout_constraintTop_toTopOf="@id/bottom_tv"
app:layout_constraintBottom_toBottomOf="@id/bottom_tv"
app:layout_constraintStart_toEndOf="@id/bottom_tv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and using setOnClickListener for ImageView. The result:
Here is a more complete answer to your question. I use a drawable resource, but you can create a drawable from your bitmap. See the following code for a description. This code can be placed into the
onCreate()of your main activity (or elsewhere.)