In this video it shows the steps to create your own custom keyboard for your Android App.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
package com.programmerworld.mycustomkeyboardapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = findViewById(R.id.editText);
editText.setShowSoftInputOnFocus(false);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
InputConnection inputConnection = editText.onCreateInputConnection(new EditorInfo());
KeyboardCustomJava keyboardCustomJava = findViewById(R.id.keyboardCustom);
keyboardCustomJava.setInputConnection(inputConnection);
}
}
package com.programmerworld.mycustomkeyboardapp;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputConnection;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
public class KeyboardCustomJava extends LinearLayout implements View.OnClickListener {
private InputConnection inputConnection;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button buttonA;
private Button buttonB;
private Button buttonC;
public KeyboardCustomJava(Context context) {
super(context);
initialization(context);
}
public KeyboardCustomJava(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initialization(context);
}
public KeyboardCustomJava(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialization(context);
}
public KeyboardCustomJava(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialization(context);
}
private void initialization(Context context){
LayoutInflater.from(context).inflate(R.layout.keybaord_custom, this, true);
button1 = findViewById(R.id.b1);
button2 = findViewById(R.id.b2);
button3 = findViewById(R.id.b3);
button4 = findViewById(R.id.b4);
button5 = findViewById(R.id.b5);
button6 = findViewById(R.id.b6);
button7 = findViewById(R.id.b7);
button8 = findViewById(R.id.b8);
button9 = findViewById(R.id.b9);
buttonA = findViewById(R.id.bA);
buttonB = findViewById(R.id.bB);
buttonC = findViewById(R.id.bC);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
button6.setOnClickListener(this);
button7.setOnClickListener(this);
button8.setOnClickListener(this);
button9.setOnClickListener(this);
buttonA.setOnClickListener(this);
buttonB.setOnClickListener(this);
buttonC.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (inputConnection == null){
return;
}
String stringValue = ((Button) view).getText().toString();
inputConnection.commitText(stringValue, 1);
}
public void setInputConnection(InputConnection inputConnectionLocal){
this.inputConnection = inputConnectionLocal;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="347dp"
android:layout_height="101dp"
android:layout_marginStart="31dp"
android:layout_marginTop="70dp"
android:ems="10"
android:hint="Test the keyboard here ..."
android:inputType="text" />
<com.programmerworld.mycustomkeyboardapp.KeyboardCustomJava
android:id="@+id/keyboardCustom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="@+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/b7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<Button
android:id="@+id/b8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<Button
android:id="@+id/b9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<Button
android:id="@+id/bB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
<Button
android:id="@+id/bC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
</LinearLayout>
</LinearLayout>
</merge>
Screenshots: