Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created June 14, 2018 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/6de72833977c9467f53c207509dbe153 to your computer and use it in GitHub Desktop.
Save ssaurel/6de72833977c9467f53c207509dbe153 to your computer and use it in GitHub Desktop.
MyKeyboard class for the In-App Keyboard tutorial on the SSaurel's Channel
package com.ssaurel.inappkeyboard;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputConnection;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyKeyboard extends LinearLayout implements View.OnClickListener {
private Button button1, button2, button3, button4,
button5, button6, button7, button8,
button9, button0, buttonDelete, buttonEnter;
private SparseArray<String> keyValues = new SparseArray<>();
private InputConnection inputConnection;
public MyKeyboard(Context context) {
this(context, null, 0);
}
public MyKeyboard(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
LayoutInflater.from(context).inflate(R.layout.keyboard, this, true);
button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button_2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button_3);
button3.setOnClickListener(this);
button4 = (Button) findViewById(R.id.button_4);
button4.setOnClickListener(this);
button5 = (Button) findViewById(R.id.button_5);
button5.setOnClickListener(this);
button6 = (Button) findViewById(R.id.button_6);
button6.setOnClickListener(this);
button7 = (Button) findViewById(R.id.button_7);
button7.setOnClickListener(this);
button8 = (Button) findViewById(R.id.button_8);
button8.setOnClickListener(this);
button9 = (Button) findViewById(R.id.button_9);
button9.setOnClickListener(this);
button0 = (Button) findViewById(R.id.button_0);
button0.setOnClickListener(this);
buttonDelete = (Button) findViewById(R.id.button_delete);
buttonDelete.setOnClickListener(this);
buttonEnter = (Button) findViewById(R.id.button_enter);
buttonEnter.setOnClickListener(this);
keyValues.put(R.id.button_1, "1");
keyValues.put(R.id.button_2, "2");
keyValues.put(R.id.button_3, "3");
keyValues.put(R.id.button_4, "4");
keyValues.put(R.id.button_5, "5");
keyValues.put(R.id.button_6, "6");
keyValues.put(R.id.button_7, "7");
keyValues.put(R.id.button_8, "8");
keyValues.put(R.id.button_9, "9");
keyValues.put(R.id.button_0, "0");
keyValues.put(R.id.button_enter, "\n");
}
@Override
public void onClick(View view) {
if (inputConnection == null)
return;
if (view.getId() == R.id.button_delete) {
CharSequence selectedText = inputConnection.getSelectedText(0);
if (TextUtils.isEmpty(selectedText)) {
inputConnection.deleteSurroundingText(1, 0);
} else {
inputConnection.commitText("", 1);
}
} else {
String value = keyValues.get(view.getId());
inputConnection.commitText(value, 1);
}
}
public void setInputConnection(InputConnection ic) {
inputConnection = ic;
}
}
@MrzAhmadi
Copy link

this lines has a problem in some device

     String value = keyValues.get(view.getId());
            inputConnection.commitText(value, 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment