HOW TO MAKE QWERTY KEYBOARD LAYOUT WITH BUTTONS FUNCTIONALITY IN ANDROID/Kotlin
Please Subscribe Youtube| Like Facebook | Follow Twitter
For Java follow this article
Introduction
In this article we will learn how to make QWERTY Keyboard layout with buttons functionality in Android.
Our Example
In our example we have at the bottom of screen keyboard qwerty layout build using button inside linear layout. Typed text will be written on text view at the top. Caps, Delete and Space functionality is also added.
Steps
Follow below steps
1) Create QWERTY layout in xml layout file
2) Code QWERTY Keyboard functionality
3) Run and test your application
1) Create QWERTY layout in xml layout file
Inside activity_main.xml we will create qwerty layout using Linear Layout. Below is xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
>
<TextView
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
/>
<LinearLayout
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/Q"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Q"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/W"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="W"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/E"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="E"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/r"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="R"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/T"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="T"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/Y"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Y"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/U"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="U"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/I"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="I"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/O"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="O"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/P"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="P"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/EXTRA1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="invisible"
/>
<Button
android:id="@+id/A"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/S"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="S"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/D"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="D"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/F"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="F"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/G"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="G"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/H"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="H"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/J"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="J"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/K"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="K"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/L"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="L"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/EXTRA2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="invisible"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/CAPS"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="CAP"
android:maxLines="1"
android:padding="10dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/Z"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Z"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/X"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="X"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/C"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/V"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="V"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/B"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/N"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="N"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/M"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="M"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/DEL"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="DEL"
android:maxLines="1"
android:padding="10dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:onClick="onClickButton"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/SPACE"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Space"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:onClick="onClickButton"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
2) Code QWERTY Keyboard functionality
onClickButton() method will be called whenever keyboard buttons are clicked. Ontype() method will be called for letters, which will then check for caps lock and write letter to text view. Similarly on Space button click will call Onspace() method which will add space. On DEL button click will call onDelete() method which will remove last letter if present in textview. On CAP button click will call changeCaps() method which will change caps of the letters on keyboard layout.
fun onClickButton(view: View) {
when (view.id) {
R.id.SPACE -> onSpace()
R.id.CAPS -> changeCaps()
R.id.DEL -> onDelete()
R.id.A -> onType("A")
R.id.B -> onType("B")
R.id.C -> onType("C")
R.id.D -> onType("D")
R.id.E -> onType("E")
R.id.F -> onType("F")
R.id.G -> onType("G")
R.id.H -> onType("H")
R.id.I -> onType("I")
R.id.J -> onType("J")
R.id.K -> onType("K")
R.id.L -> onType("L")
R.id.M -> onType("M")
R.id.N -> onType("N")
R.id.O -> onType("O")
R.id.P -> onType("P")
R.id.Q -> onType("Q")
R.id.r -> onType("R")
R.id.S -> onType("S")
R.id.T -> onType("T")
R.id.U -> onType("U")
R.id.V -> onType("V")
R.id.W -> onType("W")
R.id.X -> onType("X")
R.id.Y -> onType("Y")
R.id.Z -> onType("Z")
}
}
private fun onType(character: String) {
var char = character;
val text = textContainer.text.toString()
char = if (caps) {
char.toUpperCase()
} else {
char.toLowerCase()
}
textContainer.text = text + char
}
private fun onDelete() {
val text = textContainer.text.toString()
if (text.length > 0) {
textContainer.text = removeLastChar(text)
}
}
private fun onSpace() {
val text = textContainer.text.toString()
textContainer.text = "$text "
}
private fun changeCaps() {
caps = !caps
setCaps(caps)
}
private fun setCaps(caps: Boolean) {
if (caps) {
largeCaps()
CAPS.typeface = Typeface.DEFAULT_BOLD
} else {
smallCaps()
CAPS.typeface = Typeface.DEFAULT
}
}
Whole Code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
>
<TextView
android:id="@+id/textContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
/>
<LinearLayout
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/Q"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Q"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/W"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="W"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/E"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="E"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/r"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="R"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/T"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="T"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/Y"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Y"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/U"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="U"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/I"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="I"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/O"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="O"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/P"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="P"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/EXTRA1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="invisible"
/>
<Button
android:id="@+id/A"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="A"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/S"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="S"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/D"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="D"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/F"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="F"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/G"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="G"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/H"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="H"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/J"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="J"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/K"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="K"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/L"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="L"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/EXTRA2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="invisible"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/CAPS"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="CAP"
android:maxLines="1"
android:padding="10dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/Z"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Z"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/X"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="X"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/C"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/V"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="V"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/B"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="B"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"
/>
<Button
android:id="@+id/N"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="N"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/M"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="M"
android:padding="-2dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:textAllCaps="false"
android:onClick="onClickButton"/>
<Button
android:id="@+id/DEL"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="DEL"
android:maxLines="1"
android:padding="10dp"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:onClick="onClickButton"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/SPACE"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Space"
android:textStyle="bold"
app:autoSizeTextType="uniform"
android:onClick="onClickButton"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.kt
package com.programtown.example
import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private var caps = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onClickButton(view: View) {
when (view.id) {
R.id.SPACE -> onSpace()
R.id.CAPS -> changeCaps()
R.id.DEL -> onDelete()
R.id.A -> onType("A")
R.id.B -> onType("B")
R.id.C -> onType("C")
R.id.D -> onType("D")
R.id.E -> onType("E")
R.id.F -> onType("F")
R.id.G -> onType("G")
R.id.H -> onType("H")
R.id.I -> onType("I")
R.id.J -> onType("J")
R.id.K -> onType("K")
R.id.L -> onType("L")
R.id.M -> onType("M")
R.id.N -> onType("N")
R.id.O -> onType("O")
R.id.P -> onType("P")
R.id.Q -> onType("Q")
R.id.r -> onType("R")
R.id.S -> onType("S")
R.id.T -> onType("T")
R.id.U -> onType("U")
R.id.V -> onType("V")
R.id.W -> onType("W")
R.id.X -> onType("X")
R.id.Y -> onType("Y")
R.id.Z -> onType("Z")
}
}
private fun onType(character: String) {
var char = character;
val text = textContainer.text.toString()
char = if (caps) {
char.toUpperCase()
} else {
char.toLowerCase()
}
textContainer.text = text + char
}
private fun onDelete() {
val text = textContainer.text.toString()
if (text.length > 0) {
textContainer.text = removeLastChar(text)
}
}
private fun onSpace() {
val text = textContainer.text.toString()
textContainer.text = "$text "
}
private fun changeCaps() {
caps = !caps
setCaps(caps)
}
private fun setCaps(caps: Boolean) {
if (caps) {
largeCaps()
CAPS.typeface = Typeface.DEFAULT_BOLD
} else {
smallCaps()
CAPS.typeface = Typeface.DEFAULT
}
}
private fun largeCaps() {
A.text = "A"
B.text = "B"
C.text = "C"
D.text = "D"
E.text = "E"
F.text = "F"
G.text = "G"
H.text = "H"
I.text = "I"
J.text = "J"
K.text = "K"
L.text = "L"
M.text = "M"
N.text = "N"
O.text = "O"
P.text = "P"
Q.text = "Q"
r.text = "R"
S.text = "S"
T.text = "T"
U.text = "U"
V.text = "V"
W.text = "W"
X.text = "X"
Y.text = "Y"
Z.text = "Z"
}
private fun smallCaps() {
A.text = "a"
B.text = "b"
C.text = "c"
D.text = "d"
E.text = "e"
F.text = "f"
G.text = "g"
H.text = "h"
I.text = "i"
J.text = "j"
K.text = "k"
L.text = "l"
M.text = "m"
N.text = "n"
O.text = "o"
P.text = "p"
Q.text = "q"
r.text = "r"
S.text = "s"
T.text = "t"
U.text = "u"
V.text = "v"
W.text = "w"
X.text = "x"
Y.text = "y"
Z.text = "z"
}
companion object {
private fun removeLastChar(str: String): String {
return str.substring(0, str.length - 1)
}
}
}
3) Run and test your application
Conclusion
In this post we have learned how to add QWERTY Keyboard Layout in Android
Please Subscribe Youtube| Like Facebook | Follow Twitter