Quantcast
Viewing latest article 2
Browse Latest Browse All 10

EditText in Android

You are developing game apps, business apps or any app you need EditText widget for taking input from users. Android EditText widget is functionally rich and customizable.I have worked on many platforms and i have noticed that on most of the platforms you have do lots of coding for UI customization. But in android you can easily customize UI elements as per your requirements. In this post i am trying to collect most of the information about android EditText. EditText is the subclass of TextView class.
Like other widgets you can add a simple EditText


using java code

EditText editText = new EditText(context);

or using XML


Single-line EditText:

Single-line EditText means if you press “enter key” on EditText,focus will go to the next widget. Default behavior of EditText is multi-line.It means if you press “enter key”, a new line will add to the EditText.
using android:singleLine=”true” property you can make single-line EditText. But this property is now deprecated.
So if you add any inputtype property, EditText will become single-line EditText.
e.g. android:inputType=”text”

Multi-line EditText:
There are two properties are available for multi-line Edittext.
a. android:lines:
Using “lines” property you can specify height of edittext in terms of lines.
e.g. android:lines = “3″ means only 3 lines will be visible in EditText and for 4rth line, you have to scroll vertically.

android:inputType
Using the inputType property we can restrict use to enter the correct data.
e.g.


b.android:maxLines
In the above example user can enter only numbers in the EditText widget. If you specify a inputType, EditText will become single line.
using maxLines property you can specify the maximum height of the EditText.
e.g.android:maxLines=”3″ means first only single line will be visible and when you press “enter key”, EditText’s height will expend till the maxLines.

Gravity:


In the above image we can see that default alignment of cursor in EditText is vertical center. We can control alignment of cursor using “gravity” proprty.
e.g. android:gravity=”top” will align cursor to the top left of the EditText.

Read-only EditText:
Sometimes we need to disable EditText. For that we can use “editable” and “enabled” properties of EditText.


Hint(Watermark) in EditText:
If you are using “hint” property of EditText, a small description of field will be visible inside field and if user clicks on EditText, it disappears and user is free to write something in it. So there is no need of label for EditText, if you are using “hint” property.


e.g.
 

or from java code

editText.setHint("Enter you name");

Error popup in edittext:
Sometimes user entered invalid data in the EditText and you want to notify user that you have entered an invalid data. In this type of case you can use setError() method of EditText. It will show small popup of the error message.

	
editText.setError("Invalid Input");

Hide soft keyboard:
Sometimes you want to hide soft keyboard on some action. You can do this using this code.

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);

If in an activity first focus is on EditText, soft keyboard will appear on activity launch. If you don’t want this, you can use this code.

Listen IME key events and enter key:

editText.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(event!=null && event.getAction()==KeyEvent.ACTION_DOWN){
					//Handle enter key
					return true;
				}
				if(actionId == EditorInfo.IME_ACTION_NEXT){
					//Handle IME NEXT key
					return true;
				}
				if(actionId == EditorInfo.IME_ACTION_DONE){
					//Handle IME DONE key
					return true;
				}
				return false;
			}
		});

android:layout_weight
using layout_weight we can specify the ratio of the size of the child items in the layout.
e.g.


    
    
   


Explanation of the above XML
android:layout_weight: Using android:layout_weight we are specifying that first EditText will take 2/3 of the layout width and second EditText will take 1/3 of the layout_width;
android:layout_width=”0dip”: If you give the layout_width = “wrap_content”,EditText will expend its width while you are typing in it.If you give the layout_width=”fill_parent”, first EditText will take 1/3 of the layout width and second EditText will take 2/3 of the layout width. Because of that i am giving layout_weight=”0dip”.

Drawable:
Best thing about android widgets are, you can easily customize them. And for customization you no need to rush codes in the layout xml. So create a drawable xml in the drawable folder
e.g.



    
    
    
    


and apply that drawable to the widgets.

Selector:
EditText can be in different states like focused, disabled, pressed, etc. You can see in the previous example EditText will look same in all states.So if you want to give different looks to EditText for different states. you need to create selector xml.



    
    
    

I will keep update this article because there are lots of things that we can do with EditText widget. If you want to suggest something to improve this post, your comments are always welcome here.

Incoming search terms:


Viewing latest article 2
Browse Latest Browse All 10

Trending Articles