Android: tags autocompletion

This commit is contained in:
jendib 2014-12-04 02:28:44 +01:00
parent a7987386e1
commit b42b195245
7 changed files with 153 additions and 21 deletions

View File

@ -4,10 +4,18 @@ import android.os.Bundle;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Spinner; import android.widget.Spinner;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.sismics.docs.adapter.TagAutoCompleteAdapter;
import com.sismics.docs.ui.view.TagsCompleteTextView;
import com.sismics.docs.util.PreferenceUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/** /**
* Document edition activity. * Document edition activity.
@ -34,9 +42,21 @@ public class DocumentEditActivity extends ActionBarActivity {
languageSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, languageSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item,
new String[] { "French", "English", "Japanese" })); new String[] { "French", "English", "Japanese" }));
MultiAutoCompleteTextView tagsEditText = (MultiAutoCompleteTextView) findViewById(R.id.tagsEditText); JSONObject tags = PreferenceUtil.getCachedJson(this, PreferenceUtil.PREF_CACHED_TAGS_JSON);
tagsEditText.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, if (tags == null) {
new String[]{"Caluire", "Appartement", "Banque", "Assurance"})); finish();
return;
}
JSONArray tagArray = tags.optJSONArray("stats");
List<JSONObject> tagList = new ArrayList<>();
for (int i = 0; i < tagArray.length(); i++) {
tagList.add(tagArray.optJSONObject(i));
}
TagsCompleteTextView tagsEditText = (TagsCompleteTextView) findViewById(R.id.tagsEditText);
tagsEditText.allowDuplicates(false);
tagsEditText.setAdapter(new TagAutoCompleteAdapter(this, 0, tagList));
} }
@Override @Override

View File

@ -0,0 +1,53 @@
package com.sismics.docs.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.sismics.docs.R;
import com.tokenautocomplete.FilteredArrayAdapter;
import org.json.JSONObject;
import java.util.List;
/**
* Tag auto-complete adapter.
*
* @author bgamard.
*/
public class TagAutoCompleteAdapter extends FilteredArrayAdapter<JSONObject> {
public TagAutoCompleteAdapter(Context context, int resource, List<JSONObject> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater vi = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.tag_autocomplete_item, parent, false);
}
// Fill the view
JSONObject tag = getItem(position);
TextView textView = (TextView) view;
textView.setText(tag.optString("name"));
Drawable drawable = textView.getCompoundDrawables()[0].mutate();
drawable.setColorFilter(Color.parseColor(tag.optString("color")), PorterDuff.Mode.MULTIPLY);
textView.setCompoundDrawables(drawable, null, null, null);
textView.invalidate();
return view;
}
@Override
protected boolean keepObject(JSONObject tag, String s) {
return tag.optString("name").toLowerCase().startsWith(s.toLowerCase());
}
}

View File

@ -2,6 +2,10 @@ package com.sismics.docs.ui.view;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -11,18 +15,46 @@ import android.widget.TextView;
import com.sismics.docs.R; import com.sismics.docs.R;
import com.tokenautocomplete.TokenCompleteTextView; import com.tokenautocomplete.TokenCompleteTextView;
import org.json.JSONObject;
/**
* Auto-complete text view displaying tags.
*
* @author bgamard
*/
public class TagsCompleteTextView extends TokenCompleteTextView { public class TagsCompleteTextView extends TokenCompleteTextView {
public TagsCompleteTextView(Context context) {
super(context);
init();
}
public TagsCompleteTextView(Context context, AttributeSet attrs) { public TagsCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
init();
}
public TagsCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setFilters(new InputFilter[] {});
} }
@Override @Override
protected View getViewForObject(Object object) { protected View getViewForObject(Object object) {
String p = (String)object; JSONObject tag = (JSONObject) object;
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView view = (TextView) inflater.inflate(R.layout.tag_complete_item, (ViewGroup) getParent(), false); View view = inflater.inflate(R.layout.tag_autocomplete_token, (ViewGroup) getParent(), false);
view.setText(p); TextView textView = (TextView) view.findViewById(R.id.tagTextView);
textView.setText(tag.optString("name"));
Drawable drawable = textView.getCompoundDrawables()[0].mutate();
drawable.setColorFilter(Color.parseColor(tag.optString("color")), PorterDuff.Mode.MULTIPLY);
textView.setCompoundDrawables(drawable, null, null, null);
textView.invalidate();
return view; return view;
} }

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ddd"/>
<corners android:radius="16dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_label_white_24dp"
android:drawableStart="@drawable/ic_label_white_24dp"
android:text="Appartement"
android:drawablePadding="8dp"
android:padding="12dp"
android:gravity="center_vertical"
android:textSize="16sp"
android:textColor="#666"
android:singleLine="true"
android:ellipsize="end"/>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/tag_autocomplete_token_bg">
<TextView
android:id="@+id/tagTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_label_white_24dp"
android:drawableStart="@drawable/ic_label_white_24dp"
android:text="Appartement"
android:drawablePadding="8dp"
android:padding="8dp"
android:gravity="center"
android:textSize="16sp"
android:textColor="#666"/>
</LinearLayout>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edtTxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ccc"
android:drawablePadding="2dp"
android:padding="8dp"
android:shadowColor="#FFFFFF"
android:shadowDy="1"
android:shadowRadius="0.01"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />