- Static AutoCompleteTextView Creation in Android
- Dynamic AutoCompleteTextView Creation in Android
- AutoCompleteTextView from MySql Database
- RunTime Add Item to AutoCompleteTextView
Note Download ADT Plugin Here.
Here I am using,
OS : Linux (Ubuntu 12.04)
Eclipse : Juno (Version 4.2.0)
Android API Level : 3 to 'n' as per need
Emulator API Level : It will be displayed in output image
Here I am using,
OS : Linux (Ubuntu 12.04)
Eclipse : Juno (Version 4.2.0)
Android API Level : 3 to 'n' as per need
Emulator API Level : It will be displayed in output image
A. Static AutoCompleteTextView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:ems="10"
android:hint="AutoCompleteTextView" />
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.autocompletetextview_static;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str[]={"Arun","Mathev","Vishnu","Vishal","Arjun",
"Arul","Balaji","Babu","Boopathy","Godwin","Nagaraj"};
AutoCompleteTextView t1 = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,str);
t1.setThreshold(1);
t1.setAdapter(adp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="balaji.autocompletetextview_static"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 5 : Open res ->values ->strings.xml and add following code :
<resources>
<string name="app_name">AutoCompleteTextView_Static</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
Step 6 : Our output will be like this :
B. Dynamic AutoCompleteTextView Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:textSize="18dp"
android:textColor="#FF0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Dynamic AutoCompleteTextView" />
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.autocompletextview_dynamic;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
RelativeLayout rl;
AutoCompleteTextView auto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str[]={"Arun","Mathev","Vishnu","Vishal","Arjun",
"Arul","Balaji","Babu","Boopathy","Godwin","Nagaraj"};
rl = (RelativeLayout) findViewById(R.id.rl);
auto = new AutoCompleteTextView (MainActivity.this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params.leftMargin = 80;
params.topMargin = 130;
auto.setLayoutParams(params);
auto.setEms(10);
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,str);
auto.setThreshold(1);
auto.setAdapter(adp);
rl.addView(auto);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="balaji.autocompletextview_dynamic"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 5 : Open res ->values ->strings.xml and add following code :
<resources>
<string name="app_name">AutoCompleTextView_Dynamic</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
Step 6 : Our output will be like this :
C. AutoCompleteTextView from MySql Database
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.
Step 2 : Create a Database and Table like below :
Step 3 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:ems="13"
android:hint="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
Step 4 : Open src -> package -> MainActivity.java and add following code :
package balaji.autocompletetextview_mysql_db;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
public class MainActivity extends Activity {
InputStream is=null;
String result=null;
String line=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/text.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("Pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
final String[] str1 = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
str1[i]=json.getString("name");
}
final AutoCompleteTextView text = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
final List<String> list = new ArrayList<String>();
for(int i=0;i<str1.length;i++)
{
list.add(str1[i]);
}
Collections.sort(list);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(getApplicationContext(), android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
text.setThreshold(1);
text.setAdapter(dataAdapter);
text.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), list.get(arg2).toString(),
Toast.LENGTH_SHORT).show();
}
});
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Step 5 : Open text.php and add following code :
<?php
$host='127.0.0.1';
$uname='root';
$pwd='password';
$db='android';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$r=mysql_query("select * from class",$con);
while($row=mysql_fetch_array($r))
{
$cls[]=$row;
}
print(json_encode($cls));
mysql_close($con);
?>
Step 6 : Open AndroidManifest.xml and add following code :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="balaji.autocompletetextview_mysql_db"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 7 : Open res ->values ->strings.xml and add following code :
<resources>
<string name="app_name">AutoCompleteTextView_MySql_DB</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
Step 8 : Our output will be like this :
D. RunTime Add Item to AutoCompleteTextView
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.
Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :
<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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="Enter Text to Add"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Button" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="40dp"
android:hint="AutoCompleteTextView"
android:textSize="20dp" />
</RelativeLayout>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package balaji.autocompletetextview_runtime;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
List<String> li;
AutoCompleteTextView auto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
li=new ArrayList<String>();
li.add("Item 1");
li.add("Item 2");
li.add("Item 3");
auto=(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
final EditText et1=(EditText) findViewById(R.id.editText1);
Button b1=(Button) findViewById(R.id.button1);
add();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
li.add(et1.getText().toString());
et1.setText(null);
add();
Toast.makeText(getBaseContext(), "Item Added",
Toast.LENGTH_SHORT).show();
}
});
}
private void add() {
// TODO Auto-generated method stub
ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,li);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
auto.setThreshold(1);
auto.setAdapter(adp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="balaji.autocompletetextview_runtime"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 5 : Open res ->values ->strings.xml and add following code :
<resources>
<string name="app_name">AutoCompleteTextView_RunTime</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
Step 6 : Our output will be like this :
http://sampleprogramz.com/android/autocompletetextview.php