Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android text input layout

I have a text input layout and I want to use that to show an error message if the data entered in an edit 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 data entered is not correct I do 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 of this is done I get the error:

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

Where am I going wrong?

EDIT 1: As soon as I remove the TextLabelWhite theme, it starts working. But the theme is necessary.

like image 233
praxmon Avatar asked Jan 18 '16 11:01

praxmon


People also ask

How do you edit a text box on Android?

Step 1: Create a new project in Android Studio and name it EditTextExample. Step 2: Now Open res -> layout -> xml (or) activity_main. xml and add following code. In this code we have added multiple edittext and a button with onclick functionality.

What is TextInputLayout?

Android TexInputLayout extends LinearLayout. The primary use of a TextInputLayout is to act as a wrapper for EditText(or its descendant) and enable floating hint animations. Rule of Thumb : TextInputLayout should wrap TextInputEditText instead of the normal EditText.

What is text fields in Android?

A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard.


1 Answers

Change

android:theme="@style/TextLabelWhite"

to

style="@style/TextLabelWhite"

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

UPDATE: To customize colors you can try this. Add

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

directly to your app theme style. It'll affect on all EditText, but if it's not a problem for your app it may helps.

UPDATE 2:

The best solution i've found. Use

android:theme="@style/TextLabelWhite"

just like in your xml. Change TextLabelWhite style parent to your AppTheme style, like:

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

But android:textColorHint will not work on TextInputLayout (have no such attribute). You should use design:hintTextAppearance, but place it in different style and apply directly to TextInputLayout via

design:hintTextAppearance="@style/CustomTextAppearance"
like image 192
j2esu Avatar answered Nov 23 '22 13:11

j2esu