Working 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.


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

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

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


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


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


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:


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 ‘|’).

 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) {
 public MKEditText(Context context, AttributeSet attrs) {
 super(context, attrs);
 public void showRightIcon() {
 mDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_android_black_24dp);
 public void setIconClickListener(IconClickListener iconClickListener) {
 mIconClickListener = iconClickListener;
 private void setIcon() {
 Drawable[] drawables = getCompoundDrawables();
 setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], mDrawable,
 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;
 case MotionEvent.ACTION_UP:
 if (x + EXTRA_TOUCH_AREA >= right - drawableSize && x <= right + EXTRA_TOUCH_AREA
&& touchDown) {
 touchDown = false;
 if (mIconClickListener != null) {
return true;
 touchDown = false;
 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(;
mkEditText.setIconClickListener(new MKEditText.IconClickListener() {
 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)
 inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 

Thank You

Learn More about EditText in android studio

Read about Buttons and Button ClickListener in android studio.

Leave a Comment

Your email address will not be published. Required fields are marked *