AutoCompleteTextView, subclass of EditText with Auto-complete function AutoCompleteTextView  is a subclass of EditText that shows completion...

Autotextcomple exmple

22:04 0 Comments

AutoCompleteTextView, subclass of EditText with Auto-complete function

AutoCompleteTextView is a subclass of EditText that shows completion suggestions automatically while the user is typing.


To implement AutoCompleteTextView in your app:

Add the AutoCwompleteTextView to your layout: main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select your favourite actor:"/>

<AutoCompleteTextView
android:id="@+id/autoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/dinamicText"
android:textSize="40sp"
android:textStyle="italic"
android:textColor="#FF8000"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>


Set adapter using the main.java
package neo.app.istone.app_store;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private final Context context = this;
private AutoCompleteTextView favActor;
private TextView printedText;
private String selectedActor;
private ArrayAdapter<String> actorAdapter;

private static final String[] ACTORS = new String[] {
"Manikantasripadi", "Rajgopla", "swathi", "B.Radha Atha",
"banala.krishnama chary", "sarithakka"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
printedText = (TextView) findViewById(R.id.dinamicText);
favActor = (AutoCompleteTextView)findViewById(R.id.autoText);
loadData();
favActor.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedActor = (String) parent.getItemAtPosition(position);
printedText.setText(selectedActor);
}
});
}

private void loadData() {
actorAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, ACTORS);
favActor.setAdapter(actorAdapter);
favActor.setThreshold(1);

}
}


0 Comments: