Improve emoji pane layout

The measurement function was wrong in some situations. Set a width for
columns and properly configure the GridView.
This commit is contained in:
Jules Aguillon 2022-12-11 16:20:51 +01:00
parent 87d21685ca
commit 199ca5cf03
5 changed files with 52 additions and 65 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:hardwareAccelerated="false">
<juloo.keyboard2.EmojiGroupButtonsBar android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<juloo.keyboard2.EmojiGridView android:id="@+id/emoji_grid" android:background="?attr/colorKeyboard" android:layout_width="fill_parent" android:layout_height="@dimen/emoji_grid_height"/>
<juloo.keyboard2.EmojiGridView android:id="@+id/emoji_grid" android:layout_width="fill_parent" android:layout_height="@dimen/emoji_grid_height" android:orientation="vertical" android:numColumns="auto_fit" android:columnWidth="45sp" android:background="?attr/colorKeyboard"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<juloo.keyboard2.EmojiKeyButton style="@style/emojiKeyButton" key="switch_back_emoji"/>
<juloo.keyboard2.EmojiKeyButton style="@style/emojiKeyButton" android:layout_weight="4" key="space"/>

View File

@ -17,8 +17,10 @@
<item name="android:textColor">?attr/emoji_key_text</item>
<item name="android:textSize">18dp</item>
</style>
<!-- Passed to TextView.setTextAppearance -->
<style name="emojiGridButton">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:gravity">center</item>
<item name="android:textSize">@dimen/emoji_text_size</item>
<item name="android:textColor">?attr/emoji_color</item>
</style>

View File

@ -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<Emoji, Integer> 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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}