How to make QWERTY Keyboard Layout with Buttons Functionality in Android

Share Post
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Please Subscribe Youtube| Like Facebook | Follow Twitter

For Kotlin 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

On App Start we have initialized keyboard buttons from layout file using initKeyboardButton() method.

private void initKeyboardButton() {
        a=(Button) findViewById(R.id.A);
        b=(Button) findViewById(R.id.B);
        c=(Button) findViewById(R.id.C);
        d=(Button) findViewById(R.id.D);
        e=(Button) findViewById(R.id.E);
        f=(Button) findViewById(R.id.F);
        g=(Button) findViewById(R.id.G);
        h=(Button) findViewById(R.id.H);
        i=(Button) findViewById(R.id.I);
        j=(Button) findViewById(R.id.J);
        k=(Button) findViewById(R.id.K);
        l=(Button) findViewById(R.id.L);
        m=(Button) findViewById(R.id.M);
        n=(Button) findViewById(R.id.N);
        o=(Button) findViewById(R.id.O);
        p=(Button) findViewById(R.id.P);
        q=(Button) findViewById(R.id.Q);
        r=(Button) findViewById(R.id.R);
        s=(Button) findViewById(R.id.S);
        t=(Button) findViewById(R.id.T);
        u=(Button) findViewById(R.id.U);
        v=(Button) findViewById(R.id.V);
        w=(Button) findViewById(R.id.W);
        x=(Button) findViewById(R.id.X);
        y=(Button) findViewById(R.id.Y);
        z=(Button) findViewById(R.id.Z);
        CAPS =(Button) findViewById(R.id.CAPS);
    }

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.

public void onClickButton(View view) {
        switch (view.getId()){
            case R.id.SPACE:
                onSpace();
                break;
            case R.id.CAPS:
                changeCaps();
                break;
            case R.id.DEL:
                onDelete();
                break;
            case R.id.A:
                onType("A");
                break;
            case R.id.B:
                onType("B");
                break;
            case R.id.C:
                onType("C");
                break;
            case R.id.D:
                onType("D");
                break;
            case R.id.E:
                onType("E");
                break;
            case R.id.F:
                onType("F");
                break;
            case R.id.G:
                onType("G");
                break;
            case R.id.H:
                onType("H");
                break;
            case R.id.I:
                onType("I");
                break;
            case R.id.J:
                onType("J");
                break;
            case R.id.K:
                onType("K");
                break;
            case R.id.L:
                onType("L");
                break;
            case R.id.M:
                onType("M");
                break;
            case R.id.N:
                onType("N");
                break;
            case R.id.O:
                onType("O");
                break;
            case R.id.P:
                onType("P");
                break;
            case R.id.Q:
                onType("Q");
                break;
            case R.id.R:
                onType("R");
                break;
            case R.id.S:
                onType("S");
                break;
            case R.id.T:
                onType("T");
                break;
            case R.id.U:
                onType("U");
                break;
            case R.id.V:
                onType("V");
                break;
            case R.id.W:
                onType("W");
                break;
            case R.id.X:
                onType("X");
                break;
            case R.id.Y:
                onType("Y");
                break;
            case R.id.Z:
                onType("Z");
                break;
        }
    }

private void onType(String character){
       String text = textContainer.getText().toString();
       if(caps){
           character=character.toUpperCase();
       }
       else{
           character=character.toLowerCase();
       }
       textContainer.setText(text + character);
    }

private void onSpace(){
        String text = textContainer.getText().toString();
        textContainer.setText(text + " ");
    }

private void onDelete(){
        String text=textContainer.getText().toString();
        if(text.length()>0){
            textContainer.setText(removeLastChar(text));
        }
    }

private void changeCaps(){
        caps=!caps;
        setCaps(caps);
    }

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.java

package com.programtown.example;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textContainer;
    private boolean caps=true;
    private Button a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,CAPS;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textContainer = (TextView)findViewById(R.id.textContainer);
        initKeyboardButton();
    }

    private void initKeyboardButton() {
        a=(Button) findViewById(R.id.A);
        b=(Button) findViewById(R.id.B);
        c=(Button) findViewById(R.id.C);
        d=(Button) findViewById(R.id.D);
        e=(Button) findViewById(R.id.E);
        f=(Button) findViewById(R.id.F);
        g=(Button) findViewById(R.id.G);
        h=(Button) findViewById(R.id.H);
        i=(Button) findViewById(R.id.I);
        j=(Button) findViewById(R.id.J);
        k=(Button) findViewById(R.id.K);
        l=(Button) findViewById(R.id.L);
        m=(Button) findViewById(R.id.M);
        n=(Button) findViewById(R.id.N);
        o=(Button) findViewById(R.id.O);
        p=(Button) findViewById(R.id.P);
        q=(Button) findViewById(R.id.Q);
        r=(Button) findViewById(R.id.R);
        s=(Button) findViewById(R.id.S);
        t=(Button) findViewById(R.id.T);
        u=(Button) findViewById(R.id.U);
        v=(Button) findViewById(R.id.V);
        w=(Button) findViewById(R.id.W);
        x=(Button) findViewById(R.id.X);
        y=(Button) findViewById(R.id.Y);
        z=(Button) findViewById(R.id.Z);
        CAPS =(Button) findViewById(R.id.CAPS);
    }

    public void onClickButton(View view) {
        switch (view.getId()){
            case R.id.SPACE:
                onSpace();
                break;
            case R.id.CAPS:
                changeCaps();
                break;
            case R.id.DEL:
                onDelete();
                break;
            case R.id.A:
                onType("A");
                break;
            case R.id.B:
                onType("B");
                break;
            case R.id.C:
                onType("C");
                break;
            case R.id.D:
                onType("D");
                break;
            case R.id.E:
                onType("E");
                break;
            case R.id.F:
                onType("F");
                break;
            case R.id.G:
                onType("G");
                break;
            case R.id.H:
                onType("H");
                break;
            case R.id.I:
                onType("I");
                break;
            case R.id.J:
                onType("J");
                break;
            case R.id.K:
                onType("K");
                break;
            case R.id.L:
                onType("L");
                break;
            case R.id.M:
                onType("M");
                break;
            case R.id.N:
                onType("N");
                break;
            case R.id.O:
                onType("O");
                break;
            case R.id.P:
                onType("P");
                break;
            case R.id.Q:
                onType("Q");
                break;
            case R.id.R:
                onType("R");
                break;
            case R.id.S:
                onType("S");
                break;
            case R.id.T:
                onType("T");
                break;
            case R.id.U:
                onType("U");
                break;
            case R.id.V:
                onType("V");
                break;
            case R.id.W:
                onType("W");
                break;
            case R.id.X:
                onType("X");
                break;
            case R.id.Y:
                onType("Y");
                break;
            case R.id.Z:
                onType("Z");
                break;
        }
    }

    private void onType(String character){
       String text = textContainer.getText().toString();
       if(caps){
           character=character.toUpperCase();
       }
       else{
           character=character.toLowerCase();
       }
       textContainer.setText(text + character);
    }

    private void onDelete(){
        String text=textContainer.getText().toString();
        if(text.length()>0){
            textContainer.setText(removeLastChar(text));
        }
    }

    private static String removeLastChar(String str) {
        return str.substring(0, str.length() - 1);
    }

    private void onSpace(){
        String text = textContainer.getText().toString();
        textContainer.setText(text + " ");
    }

    private void changeCaps(){
        caps=!caps;
        setCaps(caps);
    }

    private void setCaps(boolean caps) {
        if(caps){
            largeCaps();
            CAPS.setTypeface(Typeface.DEFAULT_BOLD);
        }
        else{
            smallCaps();
            CAPS.setTypeface(Typeface.DEFAULT);
        }
    }

    private void largeCaps() {
        a.setText("A");
        b.setText("B");
        c.setText("C");
        d.setText("D");
        e.setText("E");
        f.setText("F");
        g.setText("G");
        h.setText("H");
        i.setText("I");
        j.setText("J");
        k.setText("K");
        l.setText("L");
        m.setText("M");
        n.setText("N");
        o.setText("O");
        p.setText("P");
        q.setText("Q");
        r.setText("R");
        s.setText("S");
        t.setText("T");
        u.setText("U");
        v.setText("V");
        w.setText("W");
        x.setText("X");
        y.setText("Y");
        z.setText("Z");
    }

    private void smallCaps() {
        a.setText("a");
        b.setText("b");
        c.setText("c");
        d.setText("d");
        e.setText("e");
        f.setText("f");
        g.setText("g");
        h.setText("h");
        i.setText("i");
        j.setText("j");
        k.setText("k");
        l.setText("l");
        m.setText("m");
        n.setText("n");
        o.setText("o");
        p.setText("p");
        q.setText("q");
        r.setText("r");
        s.setText("s");
        t.setText("t");
        u.setText("u");
        v.setText("v");
        w.setText("w");
        x.setText("x");
        y.setText("y");
        z.setText("z");
    }

}

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


Share Post
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Leave a Reply

Your email address will not be published. Required fields are marked *