Studyon Minte9.com
Android




Text Fields



        
        // Keybord Type (inputType="textEmail")
	// Keybord Actions (imeOptions="actionSend")
	// Keybord Button Event (editText.setOnEditorActionListener)
	// Keybord Button Label (imeActionLabel="@string/launch")
	// Fullscreen input mode disabled (imeOptions="flagNoExtractUi")
	// AutoComplete


1) Keybord Type 
	
	android:inputType="text"
	
	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:paddingBottom="@dimen/activity_vertical_margin"
	    android:paddingLeft="@dimen/activity_horizontal_margin"
	    android:paddingRight="@dimen/activity_horizontal_margin"
	    android:paddingTop="@dimen/activity_vertical_margin"
	    android:orientation="vertical"
	    tools:context=".MainActivity" >
	    
	    <!-- Text keypad -->
		<EditText 
		android:id="@+id/text_field"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/text_hint"
		android:inputType="text"
		/>

	    <!-- Email keypad (@, .com) -->
		<EditText 
		android:id="@+id/email_field"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/email_hint"
		android:inputType="textEmailAddress"
		/>
	    
	    <!-- Uri keypad (/, .www) -->
		<EditText 
		android:id="@+id/uri_field"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/uri_hint"
		android:inputType="textUri"
		/>
		
	    <!-- Number keypad -->
		<EditText 
		android:id="@+id/number_field"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/number_hint"
		android:inputType="number"
		/>
		
	    <!-- Phone keypad -->
		<EditText 
		android:id="@+id/phone_field"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/phone_hint"
		android:inputType="phone"
		/>

	</LinearLayout>


2) Keybord Actions

	android:imeOptions="actionSend"

	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:paddingBottom="@dimen/activity_vertical_margin"
	    android:paddingLeft="@dimen/activity_horizontal_margin"
	    android:paddingRight="@dimen/activity_horizontal_margin"
	    android:paddingTop="@dimen/activity_vertical_margin"
	    android:orientation="vertical"
	    tools:context=".MainActivity" >

	    <!-- actionSend -->
		<EditText 
		android:id="@+id/text_field"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/text_hint"
		android:inputType="text"
		android:imeOptions="actionSend"
		/>
	    
	    <!-- actionDone - default (Done / Next) -->
		<EditText 
		android:id="@+id/text_field2"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/text_hint"
		android:inputType="text"
		/>
		
	    <!-- actionDone (Search) -->
		<EditText 
		android:id="@+id/text_field3"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/text_hint"
		android:inputType="text"
		android:imeOptions="actionSearch"
		/>
		
	     <!-- actionNone (suppress the default behavior) -->
		<EditText 
		android:id="@+id/text_field4"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/text_hint"
		android:inputType="text"
		android:imeOptions="actionNone"
		/>

	</LinearLayout>


3) Action button events

You can listen for the specific action event using an TextView.OnEditorActionListener

	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.os.Bundle;
	import android.view.KeyEvent;
	import android.view.inputmethod.EditorInfo;
	import android.widget.EditText;
	import android.widget.TextView;
	import android.widget.TextView.OnEditorActionListener;

	public class MainActivity extends Activity {
		
	    EditText editText;
	     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		editText = (EditText) findViewById(R.id.text_field);
		editText.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				boolean handled = false;
				
				if (actionId == EditorInfo.IME_ACTION_SEND) {
					sendMessage();
					handled = true;
				}
				
				return handled;
			}
		});
	    }
	    
	    public void sendMessage() {
		editText.setText("I've been sent!");
	    }
	}


4) Custom action button label

If the keyboard is too large to reasonably share space with the underlying application (such as when a 
handset device is in landscape orientation) then fullscreen ("extract mode") is triggered. In this mode, a 
labeled action button is displayed next to the input.

	%--- activity_main.xml

    	<EditText 
		...
		android:imeOptions="actionSend"
		android:imeActionLabel="@string/launch"
		/>

	%--- strings.xml
	<resources>
		...
		<string name="launch">Launch</string>
	</resources>

This will change the default Send with Launch.


5) Fullscreen input mode disabled

You can disable the fullscreen input mode with 	android:imeOptions="flagNoExtractUi" 
(without it the send button will apear next to editText)

	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    xmlns:tools="http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"' target='_blank'>http://schemas.android.com/tools"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:paddingBottom="@dimen/activity_vertical_margin"
	    android:paddingLeft="@dimen/activity_horizontal_margin"
	    android:paddingRight="@dimen/activity_horizontal_margin"
	    android:paddingTop="@dimen/activity_vertical_margin"
	    android:orientation="vertical"
	    tools:context=".MainActivity" >

	    <!-- actionSend -->
		<EditText 
		android:id="@+id/text_field"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:hint="@string/text_hint"
		android:inputType="text"
		android:imeOptions="flagNoExtractUi"
		/>
	    
	</LinearLayout>


6) Auto-complete Suggestions


	%--- activity_main.xml

		<LinearLayout 
		...
		<AutoCompleteTextView 
		android:id="@+id/autocomplete_country"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>

		</LinearLayout>


	%--- strings.xml

		<resources>
		...
		<string-array name="countries_array">
			<item>albania</item>
			<item>algeria</item>
			<item>romania</item>
			<item>ruanda</item>
		</string-array>

		</resources>


	%--- MainActivity.java

		package com.example.myapp;

		import android.app.Activity;
		import android.os.Bundle;
		import android.widget.ArrayAdapter;
		import android.widget.AutoCompleteTextView;

		public class MainActivity extends Activity {
			     
		    @Override
		    protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			
			// Autocomplete
			
			AutoCompleteTextView textView = 
					(AutoCompleteTextView) findViewById(R.id.autocomplete_country);
			
			String[] countries = 
					getResources().getStringArray(R.array.countries_array);
			
			ArrayAdapter<String> adapter = 
					new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
			
			textView.setAdapter(adapter);        
		    }
		}


http://developer.android.com/guide/topics/ui/controls/text.html