jan

09

Autocomplete Android

Posted by : admin | On : 9 janvier 2012

Android developper doc :

http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Simple example :

http://www.botskool.com/geeks/how-create-auto-complete-textbox-android

Cook Book

http://androidcookbook.com/ViewTOC.seam

Run autocomplete android

http://stackoverflow.com/questions/7596508/autocomplete-data-retrived-via-http-request-not-hardcded-when-user-type-in-tex

with filter interface

http://stackoverflow.com/questions/8653260/autocomplete-in-android-not-working-with-dynamic-data

http://www.vingtseptpointsept.fr/2011/08/20/bricolons-un-peu-avec-idref/

1) Create below class in your activity

private class CostomTextWatcher implements TextWatcher {

@Override
public void afterTextChanged(Editable s) {
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0) {
//Make HTTP connection and retrives autocomplete strings from webservice
}

}
}

2) set this above class to edittext using below way

etSearch.addTextChangedListener(new CostomTextWatcher());

You have to make http connection using Background Thread best is AsyncTask

 

 

 
Edit Text recherche dynamique

http://code.google.com/p/android-threshold-edittext/

Autre exemple

http://code.google.com/p/androidsearchexample/downloads/list

Search Suggestion On android
Exemple synthétique :

http://matthias.jimdo.com/blog/

Exemple pratique complet:

http://weblog.plexobject.com/?p=1689

explication globale

http://developer.android.com/guide/topics/search/search-dialog.html

Exemple de recherche Edit Text dynamique

package com.MobileAnarchy.Android.ThresholdEditText;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.TextView;

public class ThresholdEditTextActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final TextView standardInput = (TextView)findViewById(R.id.TextViewStandardTextChange);
final TextView threasholdInput = (TextView)findViewById(R.id.TextViewThreasholdTextChange);

// Get the EditText reference
ThresholdEditText editText = (ThresholdEditText)findViewById(R.id.EditTextInput);

// Subscribe to the standard TextChanged events
editText.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable arg0) {
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
standardInput.setText(arg0);
}

});

// You can manually set the threshold value (default is 500ms)
editText.setThreshold(1000);

// Subscribe to the OnTresholdTextChanged event
editText.setOnThresholdTextChanged(new ThresholdTextChanged() {

@Override
public void onThersholdTextChanged(Editable text) {
Log.d(« Test », « Threshold text changed event was called –  » + text.toString());
threasholdInput.setText(text);
}

});

}
}

package com.MobileAnarchy.Android.ThresholdEditText;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

public class ThresholdEditText extends EditText {

// =========================================
// Private members
// =========================================

private int threshold;
private ThresholdTextChanged thresholdTextChanged;
private Handler handler;
private Runnable invoker;
private boolean disableThresholdOnEmptyInput;

// =========================================
// Constructors
// =========================================

public ThresholdEditText(Context context) {
super(context);
initAttributes(null);
init();
}

public ThresholdEditText (Context context, AttributeSet attrs) {
super(context, attrs);
initAttributes(attrs);
init();
}

// =========================================
// Public properties
// =========================================

/**
* Get the current threshold value
*/
public int getThreshold() {
return threshold;
}

/**
* Set the threshold value (in milliseconds)
* @param threshold Threshold value
*/
public void setThreshold(int threshold) {
this.threshold = threshold;
}

/**
* @return True = the callback will fire immediately when the content of the EditText is emptied
* False = The threshold will be used even on empty input
*/
public boolean isDisableThresholdOnEmptyInput() {
return disableThresholdOnEmptyInput;
}

/**
* @param disableThresholdOnEmptyInput Set to true if you want the callback to fire immediately when the content of the EditText is emptied
*/
public void setDisableThresholdOnEmptyInput(boolean disableThresholdOnEmptyInput) {
this.disableThresholdOnEmptyInput = disableThresholdOnEmptyInput;
}

/**
* Set the callback to the OnThresholdTextChanged event
* @param listener
*/
public void setOnThresholdTextChanged(ThresholdTextChanged listener) {
this.thresholdTextChanged = listener;
}

// =========================================
// Private / Protected methods
// =========================================

/**
* Load properties values from xml layout
*/
private void initAttributes(AttributeSet attrs) {
if (attrs != null) {
String namespace= »http://schemas.android.com/apk/res/com.MobileAnarchy.ThresholdEditText »;

// Load values to local members
this.threshold = attrs.getAttributeIntValue(namespace, « threshold », 500);;
this.disableThresholdOnEmptyInput = attrs.getAttributeBooleanValue(namespace, « disableThresholdOnEmptyInput », true);;
}
else {
// Default threshold value is 0.5 seconds
threshold = 500;

// Default behaviour on emptied text – no threshold
disableThresholdOnEmptyInput = true;
}
}

/**
* Initialize the private members with default values
*/
private void init() {

handler = new Handler();

invoker = new Runnable() {

@Override
public void run() {
invokeCallback();
}

};

this.addTextChangedListener(new TextWatcher() {

@Override
public void afterTextChanged(Editable s) { }

@Override
public void beforeTextChanged(CharSequence s, int start, int count,    int after) { }

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

// Remove any existing pending callbacks
handler.removeCallbacks(invoker);

if (s.length() == 0 && disableThresholdOnEmptyInput)
{
// The text is empty, so invoke the callback immediately
invoker.run();
}
else {
// Post a new delayed callback
handler.postDelayed(invoker, threshold);
}
}

});
}

/**
* Invoking the callback on the listener provided (if provided)
*/
private void invokeCallback() {
if (thresholdTextChanged != null) {
thresholdTextChanged.onThersholdTextChanged(this.getText());
}
}

}

package com.MobileAnarchy.Android.ThresholdEditText;

import android.text.Editable;

public interface ThresholdTextChanged {
void onThersholdTextChanged(Editable text);
}

Laisser un commentaire

Your email address will not be published. Required fields are marked *