Text Area (lv_textarea)
An editable text input with a cursor; supports one-line and password modes.
Overview
The Text Area is an editable text input with a cursor. It wraps long lines, can scroll, and supports one-line and password modes plus character and accepted-list filtering.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_textarea.c
*/
#include "../lv_examples.h"
/**
* @title Text area
* @brief A one-line field, a password field, and a multi-line area.
*
* The first uses `one_line="true"` with `placeholder_text` shown while
* empty. The second adds `password_mode="true"` so characters are masked
* (briefly revealed for `password_show_time` ms). The third is a normal
* multi-line area with starting `text` that wraps and scrolls.
*/
void lv_example_textarea(void)
{
static lv_style_t style_ta;
static lv_style_t style_cursor;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_ta);
lv_style_init(&style_cursor);
lv_style_set_bg_color(&style_ta, lv_color_hex(0x111827));
lv_style_set_bg_opa(&style_ta, (255 * 100 / 100));
lv_style_set_text_color(&style_ta, lv_color_hex(0xf3f4f6));
lv_style_set_radius(&style_ta, 8);
lv_style_set_pad_all(&style_ta, 10);
lv_style_set_border_color(&style_ta, lv_color_hex(0x3b82f6));
lv_style_set_border_width(&style_ta, 1);
lv_style_set_border_color(&style_cursor, lv_color_hex(0xf3f4f6));
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_set_flex_flow(screen, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_row(screen, 12, 0);
lv_obj_set_style_flex_cross_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_main_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_track_place(screen, LV_FLEX_ALIGN_CENTER, 0);
/* 💡 The middle field masks input; the others show plain and wrapped text. */
lv_obj_t * lv_textarea_0 = lv_textarea_create(screen);
lv_obj_set_width(lv_textarea_0, 240);
lv_textarea_set_one_line(lv_textarea_0, true);
lv_textarea_set_placeholder_text(lv_textarea_0, "Type your name ...");
lv_obj_add_style(lv_textarea_0, &style_ta, 0);
lv_obj_add_style(lv_textarea_0, &style_cursor, LV_PART_CURSOR | LV_STATE_FOCUSED);
lv_obj_t * lv_textarea_1 = lv_textarea_create(screen);
lv_obj_set_width(lv_textarea_1, 240);
lv_textarea_set_one_line(lv_textarea_1, true);
lv_textarea_set_password_mode(lv_textarea_1, true);
lv_textarea_set_password_show_time(lv_textarea_1, 500);
lv_textarea_set_text(lv_textarea_1, "secret");
lv_obj_add_style(lv_textarea_1, &style_ta, 0);
lv_obj_add_style(lv_textarea_1, &style_cursor, LV_PART_CURSOR | LV_STATE_FOCUSED);
lv_obj_t * lv_textarea_2 = lv_textarea_create(screen);
lv_obj_set_size(lv_textarea_2, 240, 64);
lv_textarea_set_text(lv_textarea_2, "A multi-line text area wraps long lines and scrolls when the content grows.");
lv_obj_add_style(lv_textarea_2, &style_ta, 0);
lv_obj_add_style(lv_textarea_2, &style_cursor, LV_PART_CURSOR | LV_STATE_FOCUSED);
}
<!--
@title Text area
@brief A one-line field, a password field, and a multi-line area.
The first uses `one_line="true"` with `placeholder_text` shown while
empty. The second adds `password_mode="true"` so characters are masked
(briefly revealed for `password_show_time` ms). The third is a normal
multi-line area with starting `text` that wraps and scrolls.
-->
<screen>
<styles>
<style
name="style_ta"
bg_color="0x111827"
bg_opa="100%"
text_color="0xf3f4f6"
radius="8"
pad_all="10"
border_color="0x3b82f6"
border_width="1"
/>
<style name="style_cursor" border_color="0xf3f4f6" />
</styles>
<view
flex_flow="column"
style_pad_row="12"
style_flex_cross_place="center"
style_flex_main_place="center"
style_flex_track_place="center"
>
<!-- 💡 The middle field masks input; the others show plain and wrapped text. -->
<lv_textarea width="240" one_line="true" placeholder_text="Type your name ...">
<style name="style_ta" />
<style name="style_cursor" selector="cursor|focused" />
</lv_textarea>
<lv_textarea width="240" one_line="true" password_mode="true" password_show_time="500" text="secret">
<style name="style_ta" />
<style name="style_cursor" selector="cursor|focused" />
</lv_textarea>
<lv_textarea
width="240"
height="64"
text="A multi-line text area wraps long lines and scrolls when the content grows."
>
<style name="style_ta" />
<style name="style_cursor" selector="cursor|focused" />
</lv_textarea>
</view>
</screen>
Properties
Parts
Style parts of the widget with local style (e.g. style_bg_color-knob="0xff0000") or with style sheets (e.g. <style name="style_knob" selector="knob">). See Styles to learn more.
| Part | Description |
|---|---|
main | The background and the typed text: background, padding and text properties. |
scrollbar | The scrollbar shown when the text is taller than the area: width (thickness), background properties and padding on the respective side. |
selected | The selected text. Only text_color and bg_color are used. |
cursor | The insertion cursor: set bg_color/bg_opa for a block cursor or a left border (border_side='left') for a bar cursor. anim_duration controls blinking. |
textarea_placeholder | The placeholder text shown when empty: text properties (e.g. text_color, text_font). |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
text | string | Set textarea text |
placeholder_text | string | Set placeholder text |
one_line | bool | Enable single-line mode |
password_mode | bool | Enable password masking |
password_show_time | int | Set delay to show typed chars (ms) |
text_selection | bool | Enable text selection |
cursor_pos | int | Set cursor position (0-based) |
cursor_click_pos | bool | Move the cursor to where the textarea is clicked |
accepted_chars | string | Only these characters can be typed, e.g. 0123456789 |
max_length | int | Maximum number of characters that can be typed |
password_bullet | string | Character shown instead of each letter in password mode |
Last updated on