Enable/Disable texarea

Dear all,
I am an electronic engineer and I created a container with some textarea. I want to enable textarea when the switch is off and to disable a textarea when the switch is ON. Is there some functions to disable/enable a textarea?

    tabview = lv_tabview_create(tabview_screen, NULL);
lan = lv_tabview_add_tab(tabview, "LAN");
wlan = lv_tabview_add_tab(tabview, "WLAN");
mqtt = lv_tabview_add_tab(tabview, "MQTT");
i_o_mapping_list = lv_tabview_add_tab(tabview, "IO MAPPING LIST");

cont = lv_cont_create(lan, NULL);
lv_obj_set_click(cont, false);
lv_obj_set_size(cont, SIZE_SCREEN_X, SIZE_SCREEN_Y);
static lv_style_t style;
lv_cont_set_fit(cont, LV_FIT_NONE);
lv_cont_set_layout(cont,  LV_LAYOUT_COLUMN_LEFT);
lv_style_set_pad_inner(&style, LV_STATE_DEFAULT, INTERLINE_1);
lv_style_set_text_font(&style, LV_STATE_DEFAULT, &arial_18_font);
lv_obj_add_style(cont, LV_OBJ_PART_MAIN, &style);

int i = 0;
label_switch = lv_label_create (cont, NULL);
lv_obj_set_size(label_switch, 75, 75);
lv_label_set_text(label_switch, "DHCP");
switch_ = lv_switch_create(cont, NULL);
lv_obj_set_size(switch_, 110, 40);
lv_obj_set_event_cb(switch_, switch_event_cb);
lv_obj_set_user_data(switch_, (int *)1);

static lv_style_t style___label_switch;
lv_style_set_text_font(&style___label_switch, LV_STATE_DEFAULT, &arial_18_font);
lv_obj_add_style(label_switch, LV_OBJ_PART_MAIN, &style___label_switch);

static lv_style_t style___label;
lv_style_set_text_font(&style___label, LV_STATE_DEFAULT, &arial_18_font);

static lv_style_t style___txt;
lv_style_set_text_font(&style___txt, LV_STATE_DEFAULT, &ali_font_16);

for(i=0;i<N_LABEL; i++){
	block_LAN[i].label = lv_label_create (cont, NULL);
	block_LAN[i].textarea = lv_textarea_create (cont, NULL);
	lv_obj_set_size(block_LAN[i].label, OBJ_W, OBJ_H);
	lv_obj_set_size(block_LAN[i].textarea, OBJ_W, OBJ_H);
	lv_label_set_text(block_LAN[i].label, vector_label_name[i]);
	lv_obj_set_user_data(block_LAN[i].textarea, (int *)i);
	lv_obj_set_event_cb(block_LAN[i].textarea, ta_event_cb);
	lv_textarea_set_text(block_LAN[i].textarea, "");
	lv_textarea_set_cursor_hidden(block_LAN[i].textarea, true);
	lv_obj_add_style(block_LAN[i].label , LV_OBJ_PART_MAIN, &style___label);
	lv_obj_add_style(block_LAN[i].textarea , LV_OBJ_PART_MAIN, &style___txt);


Another question, as you see implemeted the tab LAN: Is there a way to write in textarea in this format: ?
Let me know.
Thanks in advance
Francesco Pugliese


you can add an event callback to switch and call lv_obj_add/clear_state(textarea, LV_STATE_DISABLED). See Switch (lv_switch) — LVGL documentation

Yes, but it’s a little bit complicated now. See this example: Text area (lv_textarea) — LVGL documentation

dear kisvegabor,
thanks for fast reply.
I tried it but it doesn’ work. Why?

lv_obj_add_state(recipe.textarea, LV_STATE_DISABLED);
lv_obj_set_state(recipe.textarea, LV_STATE_DISABLED);

Thanks in advance
Let me know
Francesco Pugliese

Why, what happens?

Not that there is no lv_obj_set_state, only add and clear.

Thanks for fast reply. But it doesn t work. Why?
I am using lvgl 7.10.1
Let me know
Thanks in advance
Francesco Pugliese

Oh, I missed that it’s v7. But is should work quite same in v7 too.

But why is it now working? Compiler error? Does nothing? Freezes?

hello, thanks for fast reply. It doesn’t do anything. Why?
Let me know please.
Francesco Pugliese

Please post your current code.

Thanks for fast reply. Here you are my code.

static void event_handler_save_btn(lv_obj_t * obj, lv_event_t event){
	if(event == LV_EVENT_CLICKED ) {
		int *cmd = lv_obj_get_user_data(obj);
		if ((int)cmd == 1){
		    lv_obj_set_hidden(save_btn, false);
		    lv_obj_set_hidden(mod_btn, true);
		    lv_obj_add_state(recipe.textarea, LV_STATE_DISABLED);
		    lv_obj_set_click(add_btn, false);
		if ((int)cmd == 2){
		    lv_obj_set_hidden(mod_btn, false);
		    lv_obj_set_hidden(save_btn, true);
		    lv_obj_set_click(add_btn, true);
		    lv_slider_set_value(recipe.speed_cutter_slider, 0, LV_ANIM_ON);
			lv_slider_set_value(recipe.speed_press_slider, 0, LV_ANIM_ON);
			lv_textarea_set_text(recipe.textarea, "");
			lv_label_set_text(recipe.label_slider_value_cutter, "");
			lv_label_set_text(recipe.label_slider_value_press, "");
			lv_checkbox_set_checked(recipe.checkbox_air, false);

why doesn’t work ?

if ((int)cmd == 1){

Is it correct? Shouldn’t be if (*cmd == 1){?

Are you sure the text are has a style which makes it different when it’s in disabled state?