From 199ca5cf03f1778d9a41622f4b0ed02912c2a20b Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 11 Dec 2022 16:20:51 +0100 Subject: [PATCH] Improve emoji pane layout The measurement function was wrong in some situations. Set a width for columns and properly configure the GridView. --- res/layout/emoji_pane.xml | 2 +- res/values/styles.xml | 4 +- srcs/juloo.keyboard2/EmojiGridView.java | 32 ++++---------- .../juloo.keyboard2/EmojiGroupButtonsBar.java | 42 +++++++++++++++++-- srcs/juloo.keyboard2/EmojiTypeButton.java | 37 ---------------- 5 files changed, 52 insertions(+), 65 deletions(-) delete mode 100644 srcs/juloo.keyboard2/EmojiTypeButton.java diff --git a/res/layout/emoji_pane.xml b/res/layout/emoji_pane.xml index 2931282..bdce8e0 100644 --- a/res/layout/emoji_pane.xml +++ b/res/layout/emoji_pane.xml @@ -1,7 +1,7 @@ - + diff --git a/res/values/styles.xml b/res/values/styles.xml index 159ac73..94a8551 100644 --- a/res/values/styles.xml +++ b/res/values/styles.xml @@ -17,8 +17,10 @@ ?attr/emoji_key_text 18dp - diff --git a/srcs/juloo.keyboard2/EmojiGridView.java b/srcs/juloo.keyboard2/EmojiGridView.java index 7668c55..191f758 100644 --- a/srcs/juloo.keyboard2/EmojiGridView.java +++ b/srcs/juloo.keyboard2/EmojiGridView.java @@ -2,9 +2,8 @@ package juloo.keyboard2; import android.content.Context; import android.content.SharedPreferences; -import android.graphics.Typeface; import android.util.AttributeSet; -import android.view.Gravity; +import android.view.ContextThemeWrapper; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; @@ -14,16 +13,14 @@ import android.widget.TextView; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; -import java.util.Set; import java.util.HashSet; +import java.util.Set; public class EmojiGridView extends GridView implements GridView.OnItemClickListener { public static final int GROUP_LAST_USE = -1; - public static final int COLUMN_WIDTH = 192; - private static final String LAST_USE_PREF = "emoji_last_use"; private Emoji[] _emojiArray; @@ -38,7 +35,6 @@ public class EmojiGridView extends GridView super(context, attrs); Emoji.init(context.getResources()); setOnItemClickListener(this); - setColumnWidth(COLUMN_WIDTH); loadLastUsed(); setEmojiGroup((_lastUsed.size() == 0) ? 0 : GROUP_LAST_USE); } @@ -58,13 +54,6 @@ public class EmojiGridView extends GridView saveLastUsed(); // TODO: opti } - @Override - public void onMeasure(int wSpec, int hSpec) - { - super.onMeasure(wSpec, hSpec); - setNumColumns(getMeasuredWidth() / COLUMN_WIDTH); - } - private Emoji[] getLastEmojis() { final HashMap map = _lastUsed; @@ -121,14 +110,11 @@ public class EmojiGridView extends GridView return getContext().getSharedPreferences("emoji_last_use", Context.MODE_PRIVATE); } - private static class EmojiView extends TextView + static class EmojiView extends TextView { public EmojiView(Context context) { super(context); - setTextAppearance(context, R.style.emojiGridButton); - setGravity(Gravity.CENTER); - setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT)); } public void setEmoji(Emoji emoji) @@ -137,15 +123,15 @@ public class EmojiGridView extends GridView } } - private static class EmojiViewAdpater extends BaseAdapter + static class EmojiViewAdpater extends BaseAdapter { - private Context _context; + Context _button_context; - private Emoji[] _emojiArray; + Emoji[] _emojiArray; public EmojiViewAdpater(Context context, Emoji[] emojiArray) { - _context = context; + _button_context = new ContextThemeWrapper(context, R.style.emojiGridButton); _emojiArray = emojiArray; } @@ -171,9 +157,9 @@ public class EmojiGridView extends GridView EmojiView view = (EmojiView)convertView; if (view == null) - view = new EmojiView(_context); + view = new EmojiView(_button_context); view.setEmoji(_emojiArray[pos]); - return (view); + return view; } } } diff --git a/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java b/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java index f736ddf..1d02568 100644 --- a/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java +++ b/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java @@ -2,11 +2,18 @@ package juloo.keyboard2; import android.content.Context; import android.util.AttributeSet; -import android.widget.LinearLayout; +import android.view.ContextThemeWrapper; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; import android.widget.LinearLayout.LayoutParams; +import android.widget.LinearLayout; public class EmojiGroupButtonsBar extends LinearLayout { + private EmojiGridView _emoji_grid = null; + public EmojiGroupButtonsBar(Context context, AttributeSet attrs) { super(context, attrs); @@ -19,8 +26,37 @@ public class EmojiGroupButtonsBar extends LinearLayout } } - private void add_group(int id, String symbol) + void add_group(int id, String symbol) { - addView(new EmojiTypeButton(getContext(), id, symbol), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.f)); + addView(this.new EmojiGroupButton(getContext(), id, symbol), + new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.f)); + } + + EmojiGridView get_emoji_grid() + { + if (_emoji_grid == null) + _emoji_grid = (EmojiGridView)((ViewGroup)(getParent())).findViewById(R.id.emoji_grid); + return _emoji_grid; + } + + class EmojiGroupButton extends Button implements View.OnTouchListener + { + int _group_id; + + public EmojiGroupButton(Context context, int group_id, String symbol) + { + super(new ContextThemeWrapper(context, R.style.emojiTypeButton), null, 0); + _group_id = group_id; + setText(symbol); + setOnTouchListener(this); + } + + public boolean onTouch(View view, MotionEvent event) + { + if (event.getAction() != MotionEvent.ACTION_DOWN) + return false; + get_emoji_grid().setEmojiGroup(_group_id); + return true; + } } } diff --git a/srcs/juloo.keyboard2/EmojiTypeButton.java b/srcs/juloo.keyboard2/EmojiTypeButton.java deleted file mode 100644 index cd12325..0000000 --- a/srcs/juloo.keyboard2/EmojiTypeButton.java +++ /dev/null @@ -1,37 +0,0 @@ -package juloo.keyboard2; - -import android.content.Context; -import android.view.ContextThemeWrapper; -import android.view.MotionEvent; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; - -/* Emoji "types" are groups. This class is misnamed. */ - -public class EmojiTypeButton extends Button - implements View.OnTouchListener -{ - private int _emojiType; - - static private final int DEFAULT_GROUP = 0; - - public EmojiTypeButton(Context context, int group_id, String symbol) - { - super(new ContextThemeWrapper(context, R.style.emojiTypeButton), null, 0); - _emojiType = group_id; - setText(symbol); - setOnTouchListener(this); - } - - public boolean onTouch(View view, MotionEvent event) - { - EmojiGridView emojiGrid; - - if (event.getAction() != MotionEvent.ACTION_DOWN) - return (false); - emojiGrid = (EmojiGridView)((ViewGroup)(getParent().getParent())).findViewById(R.id.emoji_grid); - emojiGrid.setEmojiGroup(_emojiType); - return (true); - } -}