Skip to content

Instantly share code, notes, and snippets.

@chankruze
Created July 5, 2019 16:16
Show Gist options
  • Save chankruze/bec172c6c302ae91373f73b946616576 to your computer and use it in GitHub Desktop.
Save chankruze/bec172c6c302ae91373f73b946616576 to your computer and use it in GitHub Desktop.
In App Keyboard
package in.geekofia.baseconverter;
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.LinearLayout;
import com.google.android.material.button.MaterialButton;
public class GeekofiaKeyboard extends LinearLayout implements View.OnClickListener {
private MaterialButton button1, button2, button3, button4,
button5, button6, button7, button8,
button9, button0, buttonDelete, buttonEnter;
private SparseArray<String> keyValues = new SparseArray<>();
private InputConnection inputConnection;
public GeekofiaKeyboard(Context context) {
this(context, null, 0);
}
public GeekofiaKeyboard(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GeekofiaKeyboard(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 = findViewById(R.id.button_1);
button1.setOnClickListener(this);
button2 = findViewById(R.id.button_2);
button2.setOnClickListener(this);
button3 = findViewById(R.id.button_3);
button3.setOnClickListener(this);
button4 = findViewById(R.id.button_4);
button4.setOnClickListener(this);
button5 = findViewById(R.id.button_5);
button5.setOnClickListener(this);
button6 = findViewById(R.id.button_6);
button6.setOnClickListener(this);
button7 = findViewById(R.id.button_7);
button7.setOnClickListener(this);
button8 = findViewById(R.id.button_8);
button8.setOnClickListener(this);
button9 = findViewById(R.id.button_9);
button9.setOnClickListener(this);
button0 = findViewById(R.id.button_0);
button0.setOnClickListener(this);
buttonDelete = findViewById(R.id.button_delete);
buttonDelete.setOnClickListener(this);
buttonEnter = 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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:text="1" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:text="2" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_weight="1"
android:text="3" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_4"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_5"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_7"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_8"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_9"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_0"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_delete"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="1"
app:icon="@drawable/ic_backspace"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/button_enter"
android:layout_width="0dp"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Enter" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment