Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoCompletion functionality #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import me.gujun.android.taggroup.TagGroup;
import me.gujun.android.taggroup.demo.db.TagsManager;
Expand All @@ -23,6 +25,20 @@ protected void onCreate(Bundle savedInstanceState) {

mTagGroup = (TagGroup) findViewById(R.id.tag_group);
mTagGroup.setTags(tags);
mTagGroup.setOnTagTextEntryListener(new TagGroup.OnTagTextEntryListener() {
@Override
public void onTextEntry(AutoCompleteTextView tagView, String text) {
if (text.equals("A")) {
tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Au", "Bu", "Cy"}));
} else if (text.equals("Au")) {
tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Aus", "Bul", "Cyp"}));
} else if (text.equals("Aus")) {
tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Aust", "Bulg", "Cypr"}));
} else if (text.equals("Aust")) {
tagView.setAdapter(new ArrayAdapter<>(TagEditorActivity.this, android.R.layout.simple_list_item_1, new String[]{"Australia", "Bulgaria", "Cyprus"}));
}
}
});
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ android {
}
}
// Used to push in maven
apply from: '../maven-push.gradle'
//apply from: '../maven-push.gradle'
dependencies {
}
37 changes: 36 additions & 1 deletion library/src/main/java/me/gujun/android/taggroup/TagGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.ArrowKeyMovementMethod;
Expand All @@ -26,6 +27,7 @@
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import java.util.ArrayList;
Expand Down Expand Up @@ -134,6 +136,9 @@ public class TagGroup extends ViewGroup {
/** Listener used to handle tag click event. */
private InternalTagClickListener mInternalTagClickListener = new InternalTagClickListener();

/** Listener used to handle tag text entry event. */
public OnTagTextEntryListener onTagTextEntryListener;

public TagGroup(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -457,6 +462,15 @@ public void setOnTagChangeListener(OnTagChangeListener l) {
mOnTagChangeListener = l;
}

/**
* Register a callback to be invoked when a tag text is entered.
*
* @param l the callback that will be used to inform text entry event
*/
public void setOnTagTextEntryListener(OnTagTextEntryListener l) {
this.onTagTextEntryListener = l;
}

/**
* @see #appendInputTag(String)
*/
Expand Down Expand Up @@ -553,6 +567,10 @@ public interface OnTagClickListener {
void onTagClick(String tag);
}

public interface OnTagTextEntryListener {
void onTextEntry(AutoCompleteTextView tagView, String text);
}

/**
* Per-child layout information for layouts.
*/
Expand Down Expand Up @@ -648,7 +666,7 @@ public void onClick(View v) {
/**
* The tag view which has two states can be either NORMAL or INPUT.
*/
class TagView extends TextView {
class TagView extends AutoCompleteTextView {
public static final int STATE_NORMAL = 1;
public static final int STATE_INPUT = 2;

Expand Down Expand Up @@ -717,6 +735,8 @@ public TagView(Context context, final int state, CharSequence text) {
setGravity(Gravity.CENTER);
setText(text);
setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
setRawInputType(InputType.TYPE_CLASS_TEXT);
setImeOptions(EditorInfo.IME_ACTION_GO);

mState = state;

Expand All @@ -737,6 +757,21 @@ public boolean onLongClick(View v) {
if (state == STATE_INPUT) {
requestFocus();

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 (onTagTextEntryListener != null) onTagTextEntryListener.onTextEntry(TagView.this, s.toString());
}
});

// Handle the ENTER key down.
setOnEditorActionListener(new OnEditorActionListener() {
@Override
Expand Down