android autocompletetextview example

      Comments Off on android autocompletetextview example

android autocompletetextview example

android autocompletetextview example

android autocompletetextview example is best tutorial for the selection from array. If you have limited values and want to select any one from that array of values then in android autocompletetextview is best choice. AutoCompleteTextView can use array which can be created on onCreate method or onResume method else you can fetch array of string values from the resource also. lets take a look at the android tutorial on how to load values from array into autocompletetextview.

Questions related with android autocompletetextview

  • Show with example how to provide an AutoCompleteTextView for the user that can help them type some colors  from an array . How MultiAutoCompleteTextView differ from AutoCompleteTextView?
  • Explain Spinner and AutoCompleteTextView widgets taking suitable example.
  • Write the code example of an AutoCompleteTextView that can help user
    to type month names from an array.
  • What is the difference between AutoCompleteTextViewand
    MultiAutoCompleteTextView? Show how to provide an AutoCompleteTextView for the user that can help them type some of the basic colors from an array in the code. Also show the layout resource definition for this AutoCompleteTextView control.

Location : res/layout

File: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="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:background="#ad1457"
    tools:context="android.autocomplete.textview.MainActivity" >

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:ems="10"
        android:text="" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="29dp"
        android:text="By Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#fce4ec" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="AutoCompleteTextview Example"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#fce4ec" />

</RelativeLayout>

 

Location : src/package

File: ActivityMain.java

package android.autocomplete.textview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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);
		
		final String[] colors = {"red","green","blue","white","black"};
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,colors);
		
		AutoCompleteTextView text1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
		text1.setAdapter(adapter);
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Output

android autocompletetextview example