Skip to content

Instantly share code, notes, and snippets.

@korniltsev
Created November 14, 2012 12:48
Show Gist options
  • Save korniltsev/4071915 to your computer and use it in GitHub Desktop.
Save korniltsev/4071915 to your computer and use it in GitHub Desktop.
AutoCompleteTextView sample
package com.example.popup_test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
AutoCompleteTextView completition;
ViewStub stub;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrowPopUp popUp = new ArrowPopUp(MyActivity.this, 400, 600);
popUp.setContentView(getLayoutInflater().inflate(R.layout.gd_quick_action_bar, null, false));
ListView root = (ListView) popUp.getContentView().findViewById(android.R.id.list);
String[] data = {"qweqweq", "asdasd a", "asdasdasd", "asdasdasd", "asdasd", "asdasd", "asdasdasd", "asdasd", "asdasd"};
root.setAdapter(new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_list_item_1, data));
popUp.show(v);
}
};
findViewById(R.id.button1).setOnClickListener(l);
findViewById(R.id.button2).setOnClickListener(l);
findViewById(R.id.button3).setOnClickListener(l);
findViewById(R.id.button4).setOnClickListener(l);
findViewById(R.id.button5).setOnClickListener(l);
// findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(final View v) {
//// ListPopupWindow popup = new ListPopupWindow(MyActivity.this);
//// popup.setAdapter(new Adapter());
//// popup.setAnchorView(v);
//// popup.setHeight(500);
//// popup.show();
//
// ViewStub stub = (ViewStub) findViewById(R.id.stub);
// if (stub!= null){
// completition = (MyAutoCompleteTextView) stub.inflate();
// completition.requestFocusFromTouch();
// } else {
// completition.setVisibility(View.VISIBLE);
// }
//
// }
// });
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewStub stubById = (ViewStub) findViewById(R.id.stub);
if (stubById != null){
completition = (AutoCompleteTextView) stubById.inflate();
Class<AutoCompleteTextView> clazz = AutoCompleteTextView.class;
// ListPopupWindow
Field listPopUpWindow = null;
for (Field f: clazz.getDeclaredFields()){
if (f.getType().equals(ListPopupWindow.class)){
listPopUpWindow = f;
break;
}
}
if (listPopUpWindow == null){
throw new RuntimeException("epic fail, AutoCompleteTextView implementation changed");
}
listPopUpWindow.setAccessible(true);
try {
ListPopupWindow p = (ListPopupWindow) listPopUpWindow.get(completition);
p.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(completition.getWindowToken(), 0);
completition.setVisibility(View.GONE);
}
});
} catch (IllegalAccessException ignore) {
}
listPopUpWindow.setAccessible(false);
completition.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(completition, InputMethodManager.SHOW_IMPLICIT);
}
}
});
completition.setAdapter(new Adapter());
completition.showDropDown();
completition.requestFocus();
completition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
completition.showDropDown();
}
});
completition.setThreshold(1);
completition.setDropDownHeight(300);
completition.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length()==0)
((Adapter)completition.getAdapter()).getFilter().filter("");
}
});
completition.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
} else {
completition.setVisibility(View.VISIBLE);
completition.requestFocus();
completition.showDropDown();
}
}
});
}
private class Adapter extends BaseAdapter implements Filterable {
ArrayList<String> original = new ArrayList<String>();
{
for (int i = 0; i < 50; i++) {
original.add("item " + i);
}
}
ArrayList<String> objects = original;
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView ret = new TextView(MyActivity.this);
ret.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
AbsListView.LayoutParams.WRAP_CONTENT));
ret.setText((CharSequence) getItem(position));
return ret;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults ret = new FilterResults();
if (constraint == null || constraint.length() == 0) {
ret.count = original.size();
ret.values = original;
return ret;
}
ArrayList<String> filtered = new ArrayList<String>();
for (String str:original){
if (str.contains(constraint))
filtered.add(str);
}
ret.values = filtered;
ret.count = filtered.size();
return ret;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
objects = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment