StateImageButton.java


Overview

`StateImageButton` is a custom Android UI component extending the standard `ImageButton`. It provides a toggleable button that visually reflects two distinct states — *checked* and *not checked* — by switching its background drawable accordingly. This component is primarily designed for use cases where a binary state toggle is needed with an intuitive visual cue, such as toggling subtitles, mute/unmute, or similar features.

The class supports XML attribute customization for specifying the drawable resources representing the checked and unchecked states. It also integrates with Android Data Binding, enabling seamless state updates directly from layout XML files.


Class Details

public class StateImageButton extends ImageButton

A toggleable image button that switches its background resource based on an internal boolean checked state.


Inner Interface

public interface OnAction


Fields

Field Name

Type

Description

`private boolean isChecked`

boolean

Stores the current checked state of the button.

private int mStateCheckedDrawableId

int

Drawable resource ID used when button is checked.

private int mStateNotCheckedDrawableId

int

Drawable resource ID used when button is unchecked.


Constructors

public StateImageButton(Context context)
public StateImageButton(Context context, AttributeSet attrs)
public StateImageButton(Context context, AttributeSet attrs, int defStyleAttr)

Methods

private void init(@Nullable AttributeSet attrs)

private void toggleCheckState()

private void setDrawableSelector()

public boolean isChecked()

public void setChecked(boolean checked)

public static void onStateChanged(StateImageButton imageButton, boolean checked)

<com.tubitv.media.views.StateImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    bind:setCheckedState="@{viewModel.isSubtitleEnabled}" />

Implementation Details


Interaction with Other System Components


Usage Example

// In an Activity or Fragment
StateImageButton subtitleToggle = findViewById(R.id.subtitleToggle);
subtitleToggle.setChecked(true);

// Or toggle state manually
subtitleToggle.setChecked(!subtitleToggle.isChecked());

**With XML attributes:**

<com.tubitv.media.views.StateImageButton
    android:id="@+id/subtitleToggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:state_checked="@drawable/custom_checked_icon"
    app:state_not_checked="@drawable/custom_unchecked_icon" />

Visual Diagram

classDiagram
    class StateImageButton {
        -boolean isChecked
        -int mStateCheckedDrawableId
        -int mStateNotCheckedDrawableId
        +StateImageButton(Context)
        +StateImageButton(Context, AttributeSet)
        +StateImageButton(Context, AttributeSet, int)
        +boolean isChecked()
        +void setChecked(boolean)
        -void init(AttributeSet)
        -void toggleCheckState()
        -void setDrawableSelector()
        +static void onStateChanged(StateImageButton, boolean)
    }

    class OnAction {
        <<interface>>
        +void onAction()
    }

    StateImageButton ..> OnAction : "defines"

Summary

`StateImageButton` is a lightweight, customizable toggle button tailored for Android applications requiring visual state changes between two modes. It is flexible due to its support for custom drawables, integration with data binding, and simple API for state control. This component encapsulates state logic and drawable management cleanly while leaving interaction handling to the parent view or controller.


End of documentation for StateImageButton.java