I don't know what i am supposed to use to build a filter like this.
Since your question is tagged with Material Design, i guess you're looking for the appropriate ui-element:
It's called Chip, see https://material.io/components/chips
Please note it's looking different depending on Material Design version.
Try this, You can adapt with your own logic
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupCategories()
}
fun setupCategories() {
val layoutInflater = LayoutInflater.from(this)
val chipGroup = findViewById<ChipGroup>(R.id.chip_group)
chipGroup.removeAllViews()
val categories = listOf("All", " World", "️ Game", " Technologie")
categories.forEachIndexed { index, category ->
val chip = layoutInflater.inflate(R.layout.chip_item, chipGroup, false) as Chip
chip.id = View.generateViewId()
chip.text = category
chip.tag = index
chipGroup.addView(chip)
}
chipGroup.setOnCheckedChangeListener { group, checkedId ->
val chip = group.findViewById<Chip>(checkedId)
val checkedIndex = chip.tag as Int
val category = categories[checkedIndex]
}
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:singleLine="true"
app:singleSelection="true" />
</LinearLayout>
chip_item.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<com.google.android.material.chip.Chip
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:ensureMinTouchTargetSize="false"
tools:text="Category" />
</layout>