I am trying to create custom keyboard for android system with Buttons and it is working fine. But now I want to add key preview. I am new in android, plese help me if it is possible showing popupwindow from InputMethodService.
Thank you in advance.
layout/key_preview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/tvKeyPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
android:text="1"
android:textAlignment="center"
android:textColor="@color/black"
android:background="@color/white"/>
</RelativeLayout>
layout/number_key_pad.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="5dp"
android:background="@color/teal_700">
<!-- 789 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:weightSum="4"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button style="@style/Button_Key_Style" android:id="@+id/Button7" android:tag="55" android:text="7" />
<Button style="@style/Button_Key_Style" android:id="@+id/Button8" android:tag="56" android:text="8" />
<Button style="@style/Button_Key_Style" android:id="@+id/Button9" android:tag="57" android:text="8" />
</LinearLayout>
<!-- 456 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:weightSum="4"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button style="@style/Button_Key_Style" android:id="@+id/Button4" android:tag="52" android:text="4" />
<Button style="@style/Button_Key_Style" android:id="@+id/Button5" android:tag="53" android:text="5" />
<Button style="@style/Button_Key_Style" android:id="@+id/Button6" android:tag="54" android:text="6" />
</LinearLayout>
<!-- 123 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:weightSum="4"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button style="@style/Button_Key_Style" android:id="@+id/Button1" android:tag="49" android:text="1" />
<Button style="@style/Button_Key_Style" android:id="@+id/Button2" android:tag="50" android:text="2" />
<Button style="@style/Button_Key_Style" android:id="@+id/Button3" android:tag="51" android:text="3" />
</LinearLayout>
<!-- ctr -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:weightSum="4"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button style="@style/Button_Key_Style" android:id="@+id/ButtonHash" android:tag="55" android:text="#" />
<Button style="@style/Button_Key_Style" android:id="@+id/Button0" android:tag="56" android:text="0" />
<Button style="@style/Button_Key_Style" android:id="@+id/ButtonStrick" android:tag="57" android:text="*" />
<Button style="@style/Button_Key_Style" android:id="@+id/ButtonDelete" android:tag="-1" android:text="\u232b" />
</LinearLayout>
</LinearLayout>
import android.inputmethodservice.InputMethodService;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputConnection;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
public class ButtonsKeyboard extends InputMethodService {
final int KEY_CODE_DELETE = -1;
View layout_KeyPad, layout_KeyPreview;
PopupWindow keyPopup;
@Override
public View onCreateInputView() {
layout_KeyPad = getLayoutInflater().inflate(R.layout.number_key_pad,null);
layout_KeyPreview = getLayoutInflater().inflate(R.layout.key_preview,null);
TextView tvkeyPreiew = layout_KeyPreview.findViewById(R.id.tvKeyPreview);
Button button1 = layout_KeyPad.findViewById(R.id.Button1);
Button button2 = layout_KeyPad.findViewById(R.id.Button2);
Button button3 = layout_KeyPad.findViewById(R.id.Button3);
Button buttonDelete = layout_KeyPad.findViewById(R.id.ButtonDelete);
keyPopup = new PopupWindow(getApplicationContext());
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int keyCode = Integer.parseInt(v.getTag().toString());
if(keyCode>0){
tvkeyPreiew.setText(button1.getText());
keyPopup.setContentView(layout_KeyPreview);
keyPopup.setWidth(button1.getWidth()+4);
keyPopup.setHeight(button1.getHeight()+4);
int x = (int)button1.getX() -4;
int y = (int)button1.getY() -10;
keyPopup.showAtLocation(button1, Gravity.NO_GRAVITY,x,y);
writeText(keyCode);
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int keyCode = Integer.parseInt(v.getTag().toString());
if(keyCode>0){
tvkeyPreiew.setText(button2.getText());
keyPopup.setContentView(layout_KeyPreview);
keyPopup.setWidth(button2.getWidth()+4);
keyPopup.setHeight(button2.getHeight()+4);
int x = (int)button2.getX() -4;
int y = (int)button2.getY() -10;
keyPopup.showAtLocation(button2, Gravity.NO_GRAVITY,x,y);
writeText(keyCode);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int keyCode = Integer.parseInt(v.getTag().toString());
if(keyCode>0){
tvkeyPreiew.setText(button3.getText());
keyPopup.setContentView(layout_KeyPreview);
keyPopup.setWidth(button3.getWidth()+4);
keyPopup.setHeight(button3.getHeight()+4);
int x = (int)button3.getX() -4;
int y = (int)button3.getY() -10;
keyPopup.showAtLocation(button3, Gravity.NO_GRAVITY,x,y);
writeText(keyCode);
}
}
});
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputConnection ic = getCurrentInputConnection();
ic.deleteSurroundingText(1,0);
}
});
return layout_KeyPad;
}
private void writeText(int keyCode){
InputConnection ic = getCurrentInputConnection();
if(ic == null) return;
char code = (char) keyCode;
ic.commitText(String.valueOf(code),1);
keyPopup.dismiss();
}
}
I is working but no popupwindow is displayed.
You can Add Popup View in Kotlin Like this.
Here popup.showAsDropDown will open the popup which you want to show on view.