Search code examples
androidandroid-tablayoutandroid-keypad

Display virtual keyboard in android TabLayout


I'm using TabLayout to display different input methods. The first tab contains buttons and the fourth tab should display the standard keyboard embedded in this TabLayout. Here a screenshot how it should look like:

enter image description here

The TabLayout works so far. I tried to create a layout XML file with a KeyboardView. But the app doesn't show a keyboard:

<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    />

If I'm using a simple Textview, the app displays the text ... so the TabLayout itself is working:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="This is a tab layout"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

My question is how can I create a simple keyboard and display it within the TabLayout? Thank you!


Solution

  • In my fragment I needed some lines of code, for example I had to add a Layout XML:

    public class SQLConsoleTab2Fragment extends SQLConsoleFragment implements KeyboardView.OnKeyboardActionListener {
    
        public KeyboardView keyboard123;
        public View Tab2View;
    
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //execute(v)
    
    
            Tab2View = inflater.inflate(R.layout.tab2, container, false);
            keyboard123 = (KeyboardView) Tab2View.findViewById(R.id.keyboard123);
    
            Keyboard k1 = new Keyboard(Tab2View.getContext(), R.xml.qwerty_keyboard);
    
    
    Tab2View.findViewById(R.id.keyboard123);
            keyboard123.setKeyboard(k1);
            keyboard123.setEnabled(true);
            keyboard123.setPreviewEnabled(true);
    
            keyboard123.setOnKeyboardActionListener(this);
    
            return Tab2View;
        }
    }