In this blog, I will show how to achieve AutoComplete TextBox property, and how it works.
Java Code:
MainActivity.java
package com.example.autocomplete;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements TextWatcher {
AutoCompleteTextView autocomplete;
ListView listview;
ArrayList<String> listitems = new ArrayList<String>();
SQLiteDatabase sqlitedatabase;
Button save, cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqlitedatabase = this
.openOrCreateDatabase("sample", MODE_PRIVATE, null);
try {
sqlitedatabase
.execSQL("create table if not exists student(name varchar(30))");
} catch (Exception e) {
System.err.println("error" + e.toString());
}
save = (Button) findViewById(R.id.btnsave);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String value = autocomplete.getText().toString();
try {
sqlitedatabase.execSQL("insert into student (name) values('"
+ value + "')");
Toast.makeText(getApplicationContext(), "value stored",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
startActivity(intent);
} catch (Exception ex) {
System.err.println("error");
}
}
});
cancel = (Button) findViewById(R.id.btncancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
startActivity(intent);
}
});
Cursor c = sqlitedatabase.rawQuery("select distinct name from student",
null);
if (c != null) {
if (c.moveToFirst()) {
do {
String Auto = c.getString(c.getColumnIndex("name"));
listitems.add(Auto);
} while (c.moveToNext());
}
}
c.close();
final String[] arrayOfStrings;
arrayOfStrings = listitems.toArray(new String[listitems.size()]);
autocomplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autocomplete.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
arrayOfStrings));
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:completionThreshold="1"
android:ems="10" >
<requestFocus />
</AutoCompleteTextView>
<TextView
android:id="@+id/txtviewenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp"
android:paddingRight="2dp"
android:text="Enter Text"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btnsave"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Save"
android:textSize="20sp"
android:textStyle="bold" >
</Button>
<Button
android:id="@+id/btncancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Cancel"
android:textSize="20sp"
android:textStyle="bold" >
</Button>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
Screen Shots:
Save text in Local database.
Again open the application, try to enter the text, the available words starts with the letter entered pop ups.
Java Code:
MainActivity.java
package com.example.autocomplete;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements TextWatcher {
AutoCompleteTextView autocomplete;
ListView listview;
ArrayList<String> listitems = new ArrayList<String>();
SQLiteDatabase sqlitedatabase;
Button save, cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqlitedatabase = this
.openOrCreateDatabase("sample", MODE_PRIVATE, null);
try {
sqlitedatabase
.execSQL("create table if not exists student(name varchar(30))");
} catch (Exception e) {
System.err.println("error" + e.toString());
}
save = (Button) findViewById(R.id.btnsave);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String value = autocomplete.getText().toString();
try {
sqlitedatabase.execSQL("insert into student (name) values('"
+ value + "')");
Toast.makeText(getApplicationContext(), "value stored",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
startActivity(intent);
} catch (Exception ex) {
System.err.println("error");
}
}
});
cancel = (Button) findViewById(R.id.btncancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
startActivity(intent);
}
});
Cursor c = sqlitedatabase.rawQuery("select distinct name from student",
null);
if (c != null) {
if (c.moveToFirst()) {
do {
String Auto = c.getString(c.getColumnIndex("name"));
listitems.add(Auto);
} while (c.moveToNext());
}
}
c.close();
final String[] arrayOfStrings;
arrayOfStrings = listitems.toArray(new String[listitems.size()]);
autocomplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autocomplete.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
arrayOfStrings));
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:completionThreshold="1"
android:ems="10" >
<requestFocus />
</AutoCompleteTextView>
<TextView
android:id="@+id/txtviewenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp"
android:paddingRight="2dp"
android:text="Enter Text"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btnsave"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Save"
android:textSize="20sp"
android:textStyle="bold" >
</Button>
<Button
android:id="@+id/btncancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Cancel"
android:textSize="20sp"
android:textStyle="bold" >
</Button>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
Screen Shots:
Again open the application, try to enter the text, the available words starts with the letter entered pop ups.
No comments:
Post a Comment