Solution of POPUP Layout of android Soft Keyboard

  • 2021-10-16 02:37:34
  • OfStack

I'm working on a soft keyboard, which is doing well, but I don't know how to customize a long-key pop-up window.

My Keyboard View:


<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:keyPreviewLayout="@layout/preview"
android:keyBackground="@drawable/key_selector"
android:shadowRadius="0.0"
android:keyTextColor="#000000"
/>

My keyboard layout:


<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:keyHeight="10%p">

<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%p">
 <Key android:codes="113" android:keyLabel="q" />
 <Key android:codes="119" android:keyLabel="w" />
 <Key android:codes="101" android:keyLabel="e" 
  />
 <Key android:codes="114" android:keyLabel="r" />
 <Key android:codes="116" android:keyLabel="t" />
 <Key android:codes="121" android:keyLabel="y"
   android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="yýÿ"/>
 <Key android:codes="117" android:keyLabel="u"
   android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="u ú u ũû ü "/>
 <Key android:codes="105" android:keyLabel="i" 
   android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="i í § ĩîï"/>
 <Key android:codes="111" android:keyLabel="o" 
   android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="o ó  ôö" />
 <Key android:codes="112" android:keyLabel="p" />
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%p">
 <Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left" android:horizontalGap="5%p"
   android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="a á à  âä"/>
 <Key android:codes="115" android:keyLabel="s" />
 <Key android:codes="100" android:keyLabel="d" />
 <Key android:codes="102" android:keyLabel="f" />
 <Key android:codes="103" android:keyLabel="g" />
 <Key android:codes="104" android:keyLabel="h" />
 <Key android:codes="106" android:keyLabel="j" />
 <Key android:codes="107" android:keyLabel="k" />
 <Key android:codes="108" android:keyLabel="l" />
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%p">
 <Key android:codes="3"  android:keyIcon="@drawable/keyboard_shift_off" 
   android:keyHeight="7.6%p" android:keyWidth="13.7%p"/>
 <Key android:codes="122" android:keyLabel="z" android:horizontalGap="1%p"/>
 <Key android:codes="120" android:keyLabel="x" />
 <Key android:codes="99"  android:keyLabel="c"
   android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="cç"/>
 <Key android:codes="118" android:keyLabel="v" />
 <Key android:codes="98"  android:keyLabel="b" />
 <Key android:codes="110" android:keyLabel="n"
   android:popupKeyboard="@xml/keyboard_popup" android:popupCharacters="nñ"/>
 <Key android:codes="109" android:keyLabel="m" />
 <Key android:codes="-5"  android:keyIcon="@drawable/sym_keyboard_delete_dim"
  android:keyHeight="7.6%p" android:keyWidth="13.7%p" 
  android:horizontalGap="1%p"/>
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyHeight="8%p" android:keyWidth="9.6%p">
 <Key android:codes="-16" android:keyIcon="@drawable/keyboard_symbol"
   android:keyHeight="7.6%p" android:keyWidth="18.7%p"/>
 <Key android:codes="44"  android:keyLabel="," android:horizontalGap="1%p"/>
 <Key android:codes="32"  android:keyIcon="@drawable/sym_keyboard_feedback_space" android:keyWidth="40%p"/>
 <Key android:codes="46"  android:keyLabel="."/>
 <Key android:codes="-3"  android:keyIcon="@drawable/keyboard_go" 
  android:keyHeight="7.6%p" android:keyWidth="18.5%p" android:horizontalGap="1%p"/>
</Row>

Keyboard pop-up XML:


<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:keyHeight="10%p">
</Keyboard>

I tried to put keyBackground and background attributes anywhere, but failed. I tried to put:


android:popupLayout="@layout/keyboard"

... get nullpointer on keyboardView, maybe I entered the wrong XML in this parameter?

In the keyboard pop-up XML I put here:


android:popupKeyboard="@xml/keyboard_popup"

I can change the layout size, key size, key spacing, and so on, but I can't change the color or background.

The key preview is also doing well, and I put it on keyboardView:


android:keyPreviewLayout="@layout/preview"

... How it works. I think the pop-up window should be like 1, but it's not like this.

How do I customize a pop-up window with long buttons?

Then this is not what I am looking for, but solves this problem.

I created my own keyboard view and popped up 1 pop-up window to show 1 key pressed for a long time.


public class MyKeyboardView extends KeyboardView{
 @Override
 protected boolean onLongPress(final Key popupKey) {
  final View custom = LayoutInflater.from(context)
  .inflate(R.layout.popup_layout, new FrameLayout(context));
  popup = new PopupWindow(context);
  popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
  popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  popup.showAtLocation(this, Gravity.NO_GRAVITY, popupKey.x, popupKey.y-50);
 }
}

In this way, you can customize the pop-up window in any way in xml.

But this is not the correct answer, if you know a better way to answer this question.

http://stackoverflow.com/questions/34799775/soft-keyboards-popup-layout


Related articles: