HTML text input scheme

I have a text input layout, and I want to use it to display an error message if the data entered into the editable text inside it is incorrect. The definitions are as follows:

<android.support.design.widget.TextInputLayout android:id="@+id/number_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/TextLabelWhite" > <EditText android:id="@+id/number_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:digits="01234 56789" android:hint="Number" android:inputType="number" android:textColor="@color/white" /> </android.support.design.widget.TextInputLayout> 

The style is defined as follows:

 <style name="TextLabelWhite" parent="TextAppearance.AppCompat"> <item name="android:textColorHint">@color/white</item> <item name="colorAccent">@color/white</item> <item name="colorControlNormal">@color/white</item> <item name="colorControlActivated">@color/white</item> </style> 

Now, if the entered data is incorrect, I perform the following operations:

 TextInputLayout numberInputLayout = (TextInputLayout) view.findViewById(R.id.number_input_layout); EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text); numberEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); numberInputLayout.setErrorEnabled(true); numberEditText.setError("Tis an error dear"); Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show(); 

When all this is done, I get an error message:

Can't convert to color: type=0x2 in the string numberInputLayout.setErrorEnabled(true);

Where am I going wrong?

EDIT 1: As soon as I delete the TextLabelWhite theme, it starts to work. But the topic is necessary.

+7
android android-edittext
Jan 18 '16 at 11:40
source share
3 answers

Edit

 android:theme="@style/TextLabelWhite" 

to

 style="@style/TextLabelWhite" 

in the attributes android.support.design.widget.TextInputLayout in your xml and you will not get an error.

UPDATE: To customize the colors, you can try this. Add

 <item name="colorControlNormal">@android:color/white</item> <item name="colorControlActivated">@android:color/white</item> 

directly to the theme style of the application . This will affect all of the EditText, but if that is not a problem for your application, this may help.

UPDATE 2:

The best solution I have found. Use

 android:theme="@style/TextLabelWhite" 

as in your xml. Change the parent style of TextLabelWhite to the AppTheme style, for example:

 <style name="TextLabelWhite" parent="AppTheme"> 

But android: textColorHint will not work with TextInputLayout (does not have such an attribute). You should use design: hintTextAppearance , but put it in a different style and apply directly to TextInputLayout via

 design:hintTextAppearance="@style/CustomTextAppearance" 
+15
Jan 18 '16 at 12:05
source share

First call view instead of getView()

Not

 EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text); 

Do

 EditText numberEditText = (EditText) view.findViewById(R.id.number_edit_text); 
+2
Jan 18 '16 at 11:44
source share

Just try it once and know the lemma: without TextInputLayout

 EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text); String strNumber = shipText.getText().toString(); if(TextUtils.isEmpty(strNumber )) { numberEditText .setError("Please enter a value"); } 

Or execute: with TextInputLayout

 TextInputLayout numberInputLayout = (TextInputLayout) view.findViewById(R.id.number_input_layout); EditText numberEditText = (EditText) getView().findViewById(R.id.number_edit_text); **numberInputLayout.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);** numberInputLayout.setErrorEnabled(true); ***numberInputLayout.setError("Tis an error dear");*** Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show(); 
0
Jan 18 '16 at 12:15
source share



All Articles