How to get selected value from AutocompleteTextView in android

Access entered String data from Autocompletetextview on button click.

Accessing autocompletetextview string data from multiple selection is very easy because there are already a pre define function available for retrieve entered value. So here is the complete tutorial for How to get selected value from AutocompleteTextView in android .

android-project-download-code-button

How to get selected value from AutocompleteTextView in android.

Complete source code for MainActivity.java file.

package com.android_examples.com.getselectedvalueautocompletetextview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

 AutoCompleteTextView TypeText;
 Button ShowEnteredValueButton;
 String[] TextStringValue = {"Tutorials","tutorials with code", "tutorials with examples" };
 String HoldAutocompletetextview; 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 TypeText = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewAcess);
 ShowEnteredValueButton = (Button)findViewById(R.id.button1);
 ArrayAdapter<String> ValueName = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, TextStringValue);
 TypeText.setAdapter(ValueName);
 TypeText.setThreshold(3);
 
 ShowEnteredValueButton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 HoldAutocompletetextview = TypeText.getText().toString();
 
 Toast.makeText(MainActivity.this, HoldAutocompletetextview, Toast.LENGTH_SHORT).show();
 }
 });
 
 }

 @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);
 }
}

Complete source code for activity_main.xml layout file.

<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"
 tools:context="com.android_examples.com.getselectedvalueautocompletetextview.MainActivity" >

 <AutoCompleteTextView
 android:id="@+id/autoCompleteTextViewAcess"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="75dp"
 android:ems="10"
 >

 <requestFocus />
 </AutoCompleteTextView>

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/autoCompleteTextViewAcess"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="86dp"
 android:text="Click here for show seleced value" />

</RelativeLayout>

Screenshot:

How to get selected value from AutocompleteTextView in android

Click Here To Download get selected value from AutocompleteTextView in android project.

Leave a Reply

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