Custom AutocompleteTextView :
Hi friends as i have already discussed about AutoCompleteTextView in my previous post
Click here for previous example of AutoCompleteTextView.
And now in this example we will going to see the example of how to implement the CustomAdapet for AutoCompleteTextView.
So lets start with new Project.
as follow
To show string with image as......
OR
To show Only string as .......
So lets star as .....
Project Directory will be as....
activity_main.xml
Its a layout file for MainActivity class
<?xml version="1.0" encoding="utf-8"?>
<
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" tools:context="com.ygutte.customactv.MainActivity"> <
AutoCompleteTextView
android:id="@+id/ACTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textAutoComplete|textAutoCorrect" android:background="@color/colorPrimaryDark" android:layout_marginTop="20dp" android:textColor="#fff" android:hint="State.." android:paddingLeft="10dp" android:textSize="30sp" />
</RelativeLayout>
indi_item.xml
This is layout for individual items i.e. strings or string and image we going to display as suggestion
so go to res->drawable folder-> create new XML layout file as..
indi_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="sample"/>
<!-- To have just string view remove the following code of image tag-->
<ImageView
android:id="@+id/location"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/location"
android:layout_alignBottom="@+id/tv"
android:layout_toRightOf="@+id/tv"
android:layout_toEndOf="@+id/tv"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp" />
</RelativeLayout>
Now lets create CustomAdapter which will inflate suggestion list to AutoCompleteTextView asProject -> new Class as.
name it as.
CustomAdapter.java
package com.ygutte.customactv;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import java.util.ArrayList;
/** * Created by Yogeshwar on 7/7/2016. */
public class CustomAdapter extends ArrayAdapter<String> implements Filterable
{
private ArrayList<String> allList;
private ArrayList<String> mValues;
private ArrayFilter mFilter;
public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> allList) {
super(context, resource, textViewResourceId, allList);
this.allList = allList;
mValues = new ArrayList<String>(allList);
}
@Override public int getCount() {
return allList.size();
}
@Override public String getItem(int position) {
return allList.get(position);
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
private Object lock;
@Override protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mValues == null) {
synchronized (lock) {
mValues = new ArrayList<String>(allList);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
ArrayList<String> list = new ArrayList<String>(
mValues);
results.values = list;
results.count = list.size();
}
} else {
final String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values = mValues;
int count = values.size();
ArrayList<String> newValues = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
String item = values.get(i);
if (item.toLowerCase().contains(prefixString)) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results.values != null) {
allList = (ArrayList<String>) results.values;
} else {
allList = new ArrayList<String>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
Now the main code i.e. MainActivity class as
package com.ygutte.customactv; import android.app.Activity; import android.os.Bundle; import java.util.ArrayList; import android.widget.AutoCompleteTextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<String> findArrayList = new ArrayList<String>(); findArrayList.add("Maharastra"); findArrayList.add("Manipur"); findArrayList.add("MadhyaPradesh"); findArrayList.add("Karnatak"); findArrayList.add("Andra Pradesh"); findArrayList.add("Telangana"); findArrayList.add("Udisa Pradesh"); findArrayList.add("Meghalaya"); findArrayList.add("Delhi"); findArrayList.add("Kashmir"); CustomAdapter adapter = new CustomAdapter(this,R.layout.indi_item, R.id.tv, findArrayList); AutoCompleteTextView ACTV = (AutoCompleteTextView) findViewById(R.id.ACTV); ACTV.setAdapter(adapter); } }
To Download the code click here...
Happy coading..............
No comments:
Post a Comment