diff options
| author | krasimir <krasimir@chalmers.se> | 2010-06-10 13:38:15 +0000 |
|---|---|---|
| committer | krasimir <krasimir@chalmers.se> | 2010-06-10 13:38:15 +0000 |
| commit | 07da5a43c9aedba5832819d1bef163b2998282d3 (patch) | |
| tree | 2b371cd91a4eec7e948936427fc8f8e3bf50a897 /src/android/Fridge | |
| parent | d6f32b3bcd03e7fe806a1b64cd370ba78dc00aa7 (diff) | |
AndroidUI: implemented search in the bag of words
Diffstat (limited to 'src/android/Fridge')
4 files changed, 133 insertions, 29 deletions
diff --git a/src/android/Fridge/default.properties b/src/android/Fridge/default.properties index 402f98cc7..19c96655d 100644 --- a/src/android/Fridge/default.properties +++ b/src/android/Fridge/default.properties @@ -10,4 +10,4 @@ # Indicates whether an apk should be generated for each density. split.density=false # Project target. -target=android-2 +target=android-4 diff --git a/src/android/Fridge/res/layout/main.xml b/src/android/Fridge/res/layout/main.xml index 1e531d52c..67ce499fb 100644 --- a/src/android/Fridge/res/layout/main.xml +++ b/src/android/Fridge/res/layout/main.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/main_view" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" @@ -7,9 +8,9 @@ > <se.fnord.android.layout.PredicateLayout android:id="@+id/magnets_sentence" - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:padding="5dip"/> + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:padding="5dip"/> <View android:layout_height="8dip" diff --git a/src/android/Fridge/src/org/grammaticalframework/fridge/FridgeMagnets.java b/src/android/Fridge/src/org/grammaticalframework/fridge/FridgeMagnets.java index de404630a..32c71ba0d 100644 --- a/src/android/Fridge/src/org/grammaticalframework/fridge/FridgeMagnets.java +++ b/src/android/Fridge/src/org/grammaticalframework/fridge/FridgeMagnets.java @@ -4,8 +4,10 @@ import java.util.Arrays; import android.os.*; import android.app.*; -import android.content.Context; +import android.content.*; +import android.text.*; import android.view.*; +import android.view.inputmethod.*; import android.widget.*; import android.graphics.*; import se.fnord.android.layout.*; @@ -15,6 +17,9 @@ public class FridgeMagnets extends Activity { String[] words = {"hello","buy","I","you","have","please","where", "how","go","Gothenburg","London","rakia","wine", "whisky","man","woman","boy","girl","to"}; + + private Controller controller = new Controller(); + private EditText searchBox = null; @Override public void onCreate(Bundle savedInstanceState) { @@ -22,37 +27,125 @@ public class FridgeMagnets extends Activity { setContentView(R.layout.main); Arrays.sort(words); - + + refreshBagOfWords(null); + + View main = findViewById(R.id.main_view); + main.setFocusableInTouchMode(true); + main.setOnKeyListener(controller); + } + + private void applyMagnetStyles(TextView view) { + view.setTextColor(Color.BLACK); + view.setBackgroundColor(Color.WHITE); + view.setSingleLine(true); + view.setPadding(2, 2, 2, 2); + view.setClickable(true); + } + + private void refreshBagOfWords(String prefix) { PredicateLayout l = (PredicateLayout) findViewById(R.id.magnets_bag); + + l.removeAllViews(); + for (int i = 0; i < words.length; i++) { - Magnet t = new Magnet(this); + if (prefix != null && !words[i].startsWith(prefix)) + continue; + + TextView t = new TextView(this); t.setText(words[i]); + t.setOnTouchListener(controller); + applyMagnetStyles(t); l.addView(t, new PredicateLayout.LayoutParams(3, 3)); - } - } - - private static class Magnet extends TextView { - public Magnet(Context context) { - super(context); - setTextColor(Color.BLACK); - setBackgroundColor(Color.WHITE); - setSingleLine(true); - setPadding(2, 2, 2, 2); - setClickable(true); - } + } + } - @Override - public boolean onTouchEvent(MotionEvent event) { + private void addWord(String word) { + PredicateLayout l = (PredicateLayout) findViewById(R.id.magnets_sentence); + + TextView t = new TextView(this); + t.setText(word); + applyMagnetStyles(t); + l.addView(t, new PredicateLayout.LayoutParams(3, 3)); + } + + private void showSearchBox() { + if (searchBox != null) + return; + + PredicateLayout l = (PredicateLayout) findViewById(R.id.magnets_sentence); + + EditText edit = new EditText(this); + edit.setInputType(InputType.TYPE_CLASS_TEXT | + InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); + edit.addTextChangedListener(controller); + edit.setOnKeyListener(controller); + applyMagnetStyles(edit); + + l.addView(edit, new PredicateLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT, + 3, 3)); + edit.requestFocus(); + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(edit, 0); + + searchBox = edit; + } + + private void hideSearchBox() { + if (searchBox == null) + return; + + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(searchBox.getWindowToken(), 0); + + PredicateLayout l = (PredicateLayout) findViewById(R.id.magnets_sentence); + l.removeView(searchBox); + + refreshBagOfWords(null); + + searchBox = null; + } + + private class Controller implements View.OnKeyListener, View.OnTouchListener, TextWatcher { + + @Override + public boolean onKey(View view, int keyCode, KeyEvent event) { + if (event.getAction() == KeyEvent.ACTION_DOWN) { + if (searchBox == null && keyCode == KeyEvent.KEYCODE_SEARCH) { + showSearchBox(); + return true; + } else if (searchBox != null && keyCode == KeyEvent.KEYCODE_SEARCH) { + hideSearchBox(); + return true; + } + } + return false; + } + + @Override + public boolean onTouch(View view, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { - Activity activity = (Activity) getContext(); - PredicateLayout l = (PredicateLayout) activity.findViewById(R.id.magnets_sentence); - - Magnet t = new Magnet(activity); - t.setText(getText()); - l.addView(t, new PredicateLayout.LayoutParams(3, 3)); + hideSearchBox(); + addWord(((TextView) view).getText().toString()); + return true; } - return super.onTouchEvent(event); + return false; } + + @Override + public void afterTextChanged(Editable arg0) { + } + + @Override + public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { + } + + @Override + public void onTextChanged(CharSequence text, int arg1, int arg2, int arg3) { + refreshBagOfWords(text.toString()); + } } }
\ No newline at end of file diff --git a/src/android/Fridge/src/se/fnord/android/layout/PredicateLayout.java b/src/android/Fridge/src/se/fnord/android/layout/PredicateLayout.java index cd528a9d1..4734d4618 100644 --- a/src/android/Fridge/src/se/fnord/android/layout/PredicateLayout.java +++ b/src/android/Fridge/src/se/fnord/android/layout/PredicateLayout.java @@ -29,7 +29,17 @@ public class PredicateLayout extends ViewGroup { * @param vertical_spacing Pixels between items, vertically
*/
public LayoutParams(int horizontal_spacing, int vertical_spacing) {
- super(0, 0);
+ this(0, 0, horizontal_spacing, vertical_spacing);
+ }
+
+ /**
+ * @param width
+ * @param height
+ * @param horizontal_spacing Pixels between items, horizontally
+ * @param vertical_spacing Pixels between items, vertically
+ */
+ public LayoutParams(int width, int height, int horizontal_spacing, int vertical_spacing) {
+ super(width, height);
this.horizontal_spacing = horizontal_spacing;
this.vertical_spacing = vertical_spacing;
}
|
