material-components / material-components/material-components-android

[Button] ButtonGroup wraps Buttons in RecyclerView, click button, button has a high chance of disappearing

Open
#4,797 0 comments 1 reaction 1 assignee View on GitHub

@pekingme is already working on this.

Since Jun 4, 2025.

bug Widget: Button
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

Description:

ButtonGroup wraps Buttons in RecyclerView, click button, button has a high chance of disappearing

https://github.com/user-attachments/assets/351d4593-f0f1-4358-89f0-7b7119f3c83e

Source code:

//item_grid.xml
<com.google.android.material.card.MaterialCardView 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"
	android:layout_margin="4dp">
	<androidx.constraintlayout.widget.ConstraintLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content">
		<androidx.appcompat.widget.AppCompatImageView
			android:id="@+id/image"
			android:layout_width="match_parent"
			android:layout_height="0dp"
			app:layout_constraintDimensionRatio="1"
			app:layout_constraintTop_toTopOf="parent"
			app:srcCompat="@mipmap/ic_launcher" />
		<com.google.android.material.button.MaterialButtonGroup
			android:id="@+id/btn_group"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:gravity="center_horizontal"
			android:padding="4dp"
			app:layout_constraintTop_toBottomOf="@id/image">
			<com.google.android.material.button.MaterialButton
				android:id="@+id/btn1"
				style="?materialIconButtonOutlinedStyle"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				app:icon="@drawable/baseline_bubble_chart_24" />
			<com.google.android.material.button.MaterialButton
				android:id="@+id/btn2"
				style="?materialIconButtonOutlinedStyle"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				app:icon="@drawable/baseline_filter_center_focus_24" />
		</com.google.android.material.button.MaterialButtonGroup>
	</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

// activity_main.xml
<FrameLayout 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/main"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">
	<androidx.recyclerview.widget.RecyclerView
		android:id="@+id/rv_list"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:orientation="vertical"
		app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
		app:spanCount="3" />
</FrameLayout>

class MainActivity : AppCompatActivity() {
	private val binding by lazy {
		ActivityMainBinding.inflate(layoutInflater)
	}
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		enableEdgeToEdge()
		setContentView(binding.root)
		ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
			val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
			v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
			insets
		}
		binding.rvList.adapter = GridAdapter()
	}
	private class GridAdapter : RecyclerView.Adapter<GridAdapter.GridViewHolder>() {
		override fun getItemCount(): Int {
			return 200
		}
		override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GridViewHolder {
			return GridViewHolder(
				ItemGridBinding.inflate(
					LayoutInflater.from(parent.context),
					parent,
					false
				)
			)
		}
		override fun onBindViewHolder(holder: GridViewHolder, position: Int) {
		}
		class GridViewHolder(private val binding: ItemGridBinding) : RecyclerView.ViewHolder(binding.root)
	}
}

Minimal sample app repro:
https://github.com/onlymash/ButtonGroupDemo

Android API version:

35

Material Library version:

1.14.0-alpha01

Device:

OnePlus 12/13 and OnePlus Pad 2 Pro

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.