TubiRadioButton.java
Overview
`TubiRadioButton.java` defines a custom Android UI component, `TubiRadioButton`, extending `LinearLayout`. It encapsulates a radio button with an associated text label, providing a reusable and data-binding-friendly radio button widget for the Tubi TV application.
This custom view manages its own layout inflation, data binding, and checked state, making it easier to integrate consistent radio button functionality into the app’s user interface. It exposes methods to set the label text and to query or change the checked state programmatically.
Detailed Documentation
Class: TubiRadioButton
**Package:** `com.tubitv.media.views`
**Superclass:** `LinearLayout`
A compound view wrapping a single radio button and its label. Handles inflation of the radio button layout from XML, manages checked state toggling on clicks, and allows setting/getting the label text via resource IDs or strings.
Fields
Field Name | Type | Description |
|---|---|---|
`private boolean isAttachedToWindow` | `boolean` | Tracks whether this view is currently attached to the window. Useful for lifecycle awareness. |
`private ViewTubiRadioButtonBinding mBinding` | `ViewTubiRadioButtonBinding` | Data-binding generated binding class instance, linking UI elements in the inflated layout. |
Constructors
TubiRadioButton(Context context)
Description: Constructor used when creating the view programmatically.
Parameters:
context- The Context in which the view is running.
Usage example:
TubiRadioButton radioButton = new TubiRadioButton(context);Notes: Delegates to the three-argument constructor with default params.
TubiRadioButton(Context context, AttributeSet attrs)
Description: Constructor called when inflating from XML with attributes.
Parameters:
context- Context the view is running in.attrs- The attributes from XML.
Usage example:
Automatically called during XML inflation.
TubiRadioButton(Context context, AttributeSet attrs, int defStyleAttr)
Description: Full constructor with default style attribute.
Parameters:
context- The Context.attrs- XML attributes.defStyleAttr- Default style attribute.
Implementation details:
Calls
initLayout()to inflate and bind the layout unless running in edit mode (e.g., layout editor).Sets up an internal click listener to toggle checked state and propagate clicks.
Lifecycle Methods
void onAttachedToWindow()
Description: Called when the view is attached to a window.
Behavior: Sets
isAttachedToWindowflag totrue.Overrides:
View.onAttachedToWindow()
void onDetachedFromWindow()
Description: Called when the view is detached from the window.
Behavior: Sets
isAttachedToWindowflag tofalse.Overrides:
View.onDetachedFromWindow()
Private Methods
void initLayout()
Description: Inflates the custom radio button layout and configures the internal click listener.
Implementation details:
Uses
LayoutInflaterand Android Data Binding (DataBindingUtil.inflate) to inflateR.layout.view_tubi_radio_button.The root layout is attached to
this(LinearLayout).Sets an
OnClickListeneron the internal radio button (mBinding.tubiRadioButton) that toggles its checked state and callscallOnClick()on theTubiRadioButtonitself, enabling external click listeners to react.
Note: This method is called from constructors except when in edit mode.
Public Methods
void setText(@StringRes int textResId)
Description: Sets the label text using a string resource ID.
Parameters:
textResId- Resource ID of the string to display.
Usage example:
radioButton.setText(R.string.radio_button_label);Implementation: Retrieves the string from resources, then delegates to
setText(String).
void setText(@NonNull String text)
Description: Sets the label text directly with a string.
Parameters:
text- The text to display.
Usage example:
radioButton.setText("Option 1");
boolean isChecked()
Description: Returns the checked state of the internal radio button.
Returns:
trueif checked; otherwisefalse.Usage example:
boolean selected = radioButton.isChecked();
void setChecked(boolean checked)
Description: Sets the checked state of the internal radio button.
Parameters:
checked-trueto check,falseto uncheck.
Usage example:
radioButton.setChecked(true);
Important Implementation Details
Data Binding: The class uses Android's
DataBindingUtilto inflateview_tubi_radio_button.xmllayout, generating a binding objectmBindingwhich references the UI components (radio button and text view). This approach simplifies UI updates and decouples layout from code.Click Handling: The internal radio button has a click listener that toggles its own checked state and then calls
callOnClick()on theTubiRadioButtonview to propagate the click event. This means external listeners attached to theTubiRadioButtonwill be notified after the internal toggle.Lifecycle Awareness: The boolean
isAttachedToWindowtracks the view's attachment to the window, which can be useful if extended later for resource management or event subscription tied to visibility.Edit Mode Handling: Skips initialization in edit mode, preventing errors in layout editor tools.
Interaction with Other Parts of the System
Layout Resource: Depends on
R.layout.view_tubi_radio_buttonXML layout file which defines the UI elements — a radio button and a label text view.Data Binding Class: Uses
ViewTubiRadioButtonBindinggenerated from the above layout resource.Android Framework: Relies on Android UI classes (
LinearLayout,LayoutInflater,View, etc.) and annotations (@NonNull,@StringRes).Event Propagation: The class supports external click listeners via
callOnClick(), allowing integration with higher-level UI logic or selection groups.Usage Context: Can be used anywhere in the app UI where a labeled radio button is needed, providing a consistent look and behavior.
Usage Example
// Creating programmatically
TubiRadioButton radioButton = new TubiRadioButton(context);
radioButton.setText("Option A");
radioButton.setChecked(true);
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle radio button clicked
}
});
Visual Diagram
classDiagram
class TubiRadioButton {
-boolean isAttachedToWindow
-ViewTubiRadioButtonBinding mBinding
+TubiRadioButton(Context)
+TubiRadioButton(Context, AttributeSet)
+TubiRadioButton(Context, AttributeSet, int)
+void onAttachedToWindow()
+void onDetachedFromWindow()
-void initLayout()
+void setText(int)
+void setText(String)
+boolean isChecked()
+void setChecked(boolean)
}
TubiRadioButton --|> LinearLayout
TubiRadioButton ..> ViewTubiRadioButtonBinding : uses
Summary
`TubiRadioButton` is a lightweight, reusable Android UI component representing a radio button with an associated text label. It leverages Android Data Binding for layout inflation and UI management, handles its own checked state toggling, and exposes clear APIs for setting text and checked state. This class integrates seamlessly into the Tubi app’s UI layer, promoting code reuse and consistent styling of radio buttons throughout the application.