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

[TabLayout] Can't customize TabView click listener

Open
#4,105 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request Widget: Tab
Dominant language
Java
Stars
17.4k
Forks
3.2k
PR merge metrics
No merged PRs in 30d

Description

**Description:**

I have a business logic in the app to perform a certain validation when the TabView is clicked.
Due to the current TabLayout implementation, I can't prevent tab selection when the tab is clicked.

**Expected behavior:**

We should have an option to control this behavior.

**Source code:**
[TabLayout.java](https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/tabs/TabLayout.java#L2639-L2652)
```java
@Override
public boolean performClick() {
final boolean handled = super.performClick();

if (tab != null) {
if (!handled) {
playSoundEffect(SoundEffectConstants.CLICK);
}
tab.select();
return true;
} else {
return handled;
}
}
```

**Minimal sample app repro:**

1. Create Activity/Fragment with TabLayout and a few TabView items.
2. Click on the tab

**Material Library version:** 1.11.0

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.

Research direction

Start with TabLayout.java at lines 2639-2652 and review how TabView.performClick() selects its tab. Reproduce the behavior with the minimal sample app steps from the issue, then define and implement an option that lets callers prevent selection after a tab click. Done means the validation can block selection while normal tab clicks continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
mobile
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.