Built-in widgets
This page covers the basic built-in widgets. For the full list of components and APIs, see the com.facebook.litho.widget package in javadocs.
Textβ
Shows simple text (the equivalent of an Android TextView
within Litho).
Required Propβ
text: CharSequence
- text to display.
Usageβ
Text
has numerous optional props that can be used style text, just as with TextView
since both use android.text.Layout
under the hood.
A full list of the optional props is available in the javadocs.
- KComponent
- Spec API
Text(
text = "This is my example text",
textSize = 12.sp,
textColor = Color.RED,
alignment = TextAlignment.CENTER)
final Component component =
Text.create(c)
.text("This is my example text")
.textSizeRes(R.dimen.my_text_size)
.textColorRes(R.color.my_text_color)
.textAlignment(Layout.Alignment.ALIGN_CENTER)
.build();
TextInputβ
Renders an editable text input using an Android EditText
.
Required Propβ
None.
Usageβ
As this component is backed by Android's EditText
, many native capabilities are applicable:
- Use an
android.text.InputFilter
to set a text length limit or modify text input. - Change the input representation by passing an
android.text.InputType
constant. - For performance reasons, avoid recreating the Component with different props to change its configuration. Instead, use Event triggers
OnTrigger
to update text, request view focus or set selection. For example,TextInput.setText(c, "myTextInputKey", "myText")
.
- KComponent
- Spec API
TextInput(
initialText = "Initial text",
multiline = true,
inputType = InputType.TYPE_CLASS_TEXT,
inputFilter = InputFilter.LengthFilter(/* maxLength= */ 10))
private static final InputFilter lenFilter = new InputFilter.LengthFilter(maxLength);
Component component =
TextInput.create(c)
.initialText(text)
.textColorStateList(ColorStateList.valueOf(color))
.multiline(true)
.inputFilter(lenFilter)
.backgroundColor(Color.TRANSPARENT)
.inputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES)
.build();
Imageβ
Displays a drawable.
Required Propβ
drawable: Drawable
- drawable to display.
Usageβ
- KComponent
- Spec API
Image(
drawable = drawableRes(R.drawable.ic_launcher),
scaleType = ImageView.ScaleType.CENTER_CROP)
Component component =
Image.create(c)
.drawableRes(R.drawable.my_drawable)
.scaleType(ImageView.ScaleType.CENTER_CROP)
.build();
Cardβ
The Litho equivalent of an Android CardView
. Card
applies borders with shadows/elevation to a given component.
If your card is rendered on top of a dynamically coloured background that cannot be 'faked' using the Card
component, use the less performant prop transparencyEnabled(true)
.
Required Propβ
child: Component
- the component to decorate.
Usageβ
- KComponent
- Spec API
Card(cornerRadius = 5.dp, clippingColor = Color.RED, child = { Text("my content") })
Component component =
Card.create(c)
.content(myContentComponent)
.clippingColorRes(R.color.my_clipping_color)
.cornerRadiusDip(8)
.build();
SolidColorβ
Renders solid color.
Required Propβ
color: Int
- color to display.
Usageβ
- KComponent
- Spec API
SolidColor(color = Color.RED)
Component component =
SolidColor.create(c)
.color(Color.RED)
.alpha(0.5)
.build();
Progressβ
Renders an infinitely spinning progress bar backed by the Android's ProgressBar
.
Required Propβ
None.
Usageβ
- KComponent
- Spec API
Progress(indeterminateDrawable = drawableRes(R.drawable.ic_launcher))
Component component =
Progress.create(c)
.indeterminateDrawableRes(R.drawable.my_loading_spinner)
.build();
Spinnerβ
A simple spinner (dropdown) component. Derived from the standard Android Spinner
.
Required Propsβ
options: List<String>
- list of selection options.selectedOption: String
- the currently selected option.
Usageβ
- KComponent
- Spec API
Spinner(options = myOptions, selectedOption = myOptions.get(0), onItemSelected = {})
List<String> myOptions = ...
Component component =
Spinner.create(c)
.options(myOptions)
.selectedOption(myOptions.get(0))
.build();
VerticalScrollβ
Wraps another component, allowing it to be vertically scrollable. It's analogous to Android's ScrollView
.
Required Propβ
child: Component
- a component to vertically scroll.
Usageβ
- KComponent
- Spec API
VerticalScroll(style = Style.height(100.dp)) { Column { getComponents(this) } }
Component component =
VerticalScroll.create(c)
.childComponent(myComponentToScroll)
.verticalFadingEdgeEnabled(true)
.fadingEdgeLengthDip(FADING_EDGE_LENGTH_DP)
.build();
HorizontalScrollβ
Wraps another component, allowing it to be horizontally scrollable. It's analogous to Android's HorizontalScrollView
.
Required Propβ
child: Component
- a component to horizontally scroll.
Usageβ
- KComponent
- Spec API
HorizontalScroll { Row { getComponents(this) } }
Component component =
HorizontalScroll.create(c)
.contentProps(myComponentToScroll)
.build();
Recyclerβ
Recycler
is the equivalent of Android's RecyclerView
. It's recommended to use Sections for efficient list rendering, which uses Recycler
under the hood.
To use the Recycler
directly, see the RecyclerCollectionComponent page.