Sunday, June 26, 2022
HomeAndroid Studio For Beginners 2020Working with EditText in Android Studio

Working with EditText in Android Studio

In this article we will learn the working with EditText in android studio. The EditText is the standard text entry widget in Android apps. Moreover, If the user needs to enter text into an app, this is the primary way for them to do that.

We can customize the behavior of an EditText by using several attributes as following.

<EditText
      android:id="@+id/plain_text_input"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:inputType="text"/>

In order to get the text value in EditText we can use the following method.

EditText simpleEditText = (EditText) findViewById(R.id.et_simple);
String strValue = simpleEditText.getText().toString()

Furtheromer, We might want to limit the entry to a single-line of text (avoid newlines):

EditText
android:singleLine="true"
android:lines="1"
/>

Also, We can limit the characters of a field using the digits attribute

<EditText
 android:inputType="number"
 android:digits="01"
/>

Note, This would restrict the digits entered to just “0” and “1”. We might want to limit the total number of characters with:

<EditText
 android:maxLength="5"
/>

Customizing the InputType

Text fields can have different input types. Such as number, date, password, or email address. The type determines what kind of characters are allowed inside the field. Moreover, it may prompt the virtual keyboard to optimize its layout for frequently used characters. By default, any text contents within an EditText control is displayed as plain text. By setting the inputType attribute, we can facilitate input of different types of information, like phone numbers and passwords:

<EditText
 ...
 android:inputType="phone">
</EditText

Furthermore, The android:inputType also allows you to specify certain keyboard behaviors. Such as whether to capitalize all new words or use features like auto-complete and spelling suggestions.

You can set multiple inputType attributes if needed (separated by ‘|’).
Example:

<EditText
 android:id="@+id/postal_address"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:hint="@string/postal_address_hint"
 android:inputType="textPostalAddress|
 textCapWords|
 textNoSuggestions" />

Icon or button inside Custom Edit Text and its action and click listeners

This example will help to have the Edit text with the icon at the right side.

Note: In this just I am using setCompoundDrawablesWithIntrinsicBounds, So if you want to change the
icon position you can achieve that using setCompoundDrawablesWithIntrinsicBounds in setIcon.

public class MKEditText extends AppCompatEditText {
 public interface IconClickListener {
 public void onClick();
 }
 private IconClickListener mIconClickListener;
 private static final String TAG = MKEditText.class.getSimpleName();
 private final int EXTRA_TOUCH_AREA = 50;
 private Drawable mDrawable;
 private boolean touchDown;
 public MKEditText(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 }
 public MKEditText(Context context) {
 super(context);
 }
 public MKEditText(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 public void showRightIcon() {
 mDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_android_black_24dp);
 setIcon();
 }
 public void setIconClickListener(IconClickListener iconClickListener) {
 mIconClickListener = iconClickListener;
 }
 private void setIcon() {
 Drawable[] drawables = getCompoundDrawables();
 setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], mDrawable,
drawables[3]);
 setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
 setSelection(getText().length());
 }
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 final int right = getRight();
 final int drawableSize = getCompoundPaddingRight();
 final int x = (int) event.getX();
 switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
 if (x + EXTRA_TOUCH_AREA >= right - drawableSize && x <= right + EXTRA_TOUCH_AREA)
{
 touchDown = true;
 return true;
 }
 break;
 case MotionEvent.ACTION_UP:
 if (x + EXTRA_TOUCH_AREA >= right - drawableSize && x <= right + EXTRA_TOUCH_AREA
&& touchDown) {
 touchDown = false;
 if (mIconClickListener != null) {
 mIconClickListener.onClick();
 }
return true;
 }
 touchDown = false;
 break;
 }
 return super.onTouchEvent(event);
 }
}

If you want to change the touch area you can change the EXTRA_TOUCH_AREA values default I gave as 50. And for Enable the button and click listener you can call from your Activity or Fragment like this,

MKEditText mkEditText = (MKEditText) findViewById(R.id.password);
mkEditText.showRightIcon();
mkEditText.setIconClickListener(new MKEditText.IconClickListener() {
 @Override
 public void onClick() {
 // You can do action here for the icon.
 
 }
 });

Hiding SoftKeyboard

Hiding Softkeyboard is a basic requirement usually when working with EditText. The softkeyboard by default can only be closed by pressing back button and so most developers use InputMethodManager to force Android to hide the virtual keyboard calling hideSoftInputFromWindow and passing in the token of the window containing your focused view. The code to do the following:

public void hideSoftKeyboard() 
{
 InputMethodManager inputMethodManager = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
 inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
}

Thank You

Learn More about EditText in android studio

Read about Buttons and Button ClickListener in android studio.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

KU NUR RASYIDAH BINTI KU BIDE on Custom Calendar View Android | Project Java Files
SHIVANT KUMAR PANDEY on Login And Registration by Firebase
Adriane Troy U. Alariao on Custom Calendar View With Events Android