The EditText has to be wrapped by TextInputLayout in order to display the floating Also supports showing an error via setErrorEnabled(boolean) and setError(CharSequence).
Implementation
build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "inputlayout.test.com.textinputlayout" minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.0' }
MainActivity.java
package inputlayout.test.com.textinputlayout; import android.os.Bundle; import android.support.design.widget.TextInputLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.KeyEvent; import android.view.inputmethod.EditorInfo; import android.widget.EditText; import android.widget.TextView; import java.lang.ref.WeakReference; public class MainActivity extends AppCompatActivity { private static final int MIN_TEXT_LENGTH = 5; private static final String EMPTY_STRING = ""; private TextInputLayout mTextInputLayout; private EditText mEditText; private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); mTextInputLayout = (TextInputLayout) findViewById(R.id.text_input_layout); mEditText = (EditText) findViewById(R.id.editText); mTextInputLayout.setHint(getString(R.string.hint)); mEditText.setOnEditorActionListener(ActionListener.newInstance(this)); } private boolean shouldShowError() { int textLength = mEditText.getText().length(); return textLength > 0 && textLength < MIN_TEXT_LENGTH; } private void showError() { mTextInputLayout.setError(getString(R.string.error)); } private void hideError() { mTextInputLayout.setError(EMPTY_STRING); } private static final class ActionListener implements TextView.OnEditorActionListener { private final WeakReference<MainActivity> mainActivityWeakReference; public static ActionListener newInstance(MainActivity mainActivity) { WeakReference<MainActivity> mainActivityWeakReference = new WeakReference<>(mainActivity); return new ActionListener(mainActivityWeakReference); } private ActionListener(WeakReference<MainActivity> mainActivityWeakReference) { this.mainActivityWeakReference = mainActivityWeakReference; } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { MainActivity mainActivity = mainActivityWeakReference.get(); if (mainActivity != null) { if (actionId == EditorInfo.IME_ACTION_GO && mainActivity.shouldShowError()) { mainActivity.showError(); } else { mainActivity.hideError(); } } return true; } } }
activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> </android.support.design.widget.AppBarLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:focusable="true" android:focusableInTouchMode="true" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize" android:orientation="vertical" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="60dp"> <android.support.design.widget.TextInputLayout android:id="@+id/text_input_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" app:errorEnabled="true" app:errorTextAppearance="@style/ErrorText">> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeOptions="actionGo" android:inputType="text" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
strings.xml
<resources> <string name="app_name">TextInputLayout</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="error">Please enter at least 5 characters</string> <string name="hint">Please Enter Email Id</string> </resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="green">#1E9618</color> <color name="accent">#4EE147</color> <color name="green_dark">#156912</color> <color name="error_text_color">#FF0000</color> <color name="colorPrimary">#F50057</color> <color name="colorPrimaryDark">#C51162</color> <color name="textColorPrimary">#FFFFFF</color> <color name="windowBackground">#FFFFFF</color> <color name="colorAccent">#FF80AB</color> <color name="navigationBarColor">#000000</color> </resources>
No comments:
Post a Comment