I'm relatively new to app development and working with Android Studio and layouts. I'm in the process of designing my app and have encountered an issue. I have the design for a ListItem for a ListView where the player's name and 2 icons (Edit and Delete) should be displayed. So, when the user adds a player, the entries should be displayed like that.
My problem now is: How can I best implement the design in XML to achieve the desired functionality?
ListItem Design, created with Figma
I thought one could design the whole thing as a button in XML, which then receives this design as a background. But then the question is, how can I make the icon clickable?
Or is there any better way to do this, that i might miss here?
First of all - don't use ListView
- it has lots of problems. Use RecyclerView
.
There are multiple ways to do this. You can use this one, for example:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/text_end"/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/text_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"/>
<ImageView
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your drawable path goes here"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/text_end"/>
<ImageView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="your drawable path goes here"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/edit" />
</androidx.constraintlayout.widget.ConstraintLayout>
Don't forget to add path to your image in ImageView
src
attribute.
Then in your ViewHolder you can find and set onClickListener
on each ImageView