Foobar
Foobar

Reputation: 8487

Android how to use Layout Inflator

I am making a soft keyboard for Android. However the design of my keyboard is very non-traditional, this means I do not want to use the traditional Keyboard XML Layout and instead I want to use a Relative Layout.

This is the tutorial I followed in order to create the input service: http://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

I was looking at ways to use a Relative Layout instead of the normal Keyboard Layout file and I found this stack overflow post:
Android: Non-Keyboard IME

It seems I have to use the Layout Inflator to create a stand alone view for the keyboard. However I did not understand what to do from the solution presented in the stackoverflow post.

So could someone explain how to use a Relative Layout file to design the keyboard for a custom keyboard instead of the default Keyboard Layout File?

EDIT:

Here is the file structure of my project:

enter image description here

custom_keyboard_layout is a Relative Layout file that I want my keyboard to use.

It looks like this:

    <?xml version="1.0" encoding="utf-8"?>
<!-- This is a very rough test version of the layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.roymunson.vroy.customtestkeyboard.IntroScreen">

<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:text="Custom Button." />
</RelativeLayout>

My Keyboard.xml file looks like this:

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

My customInputMethod.java file is a InputMethodService it looks like this:

    package com.roymunson.vroy.customtestkeyboard;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.View;


public class customInputMethod extends InputMethodService {

    private Keyboard keyboard;

    @Override public void onInitializeInterface() {
        keyboard = new Keyboard(this, R.xml.custom_keyboard_layout);
    }

    @Override
    public View onCreateInputView() {
        KeyboardView mInputView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
        mInputView.setKeyboard(keyboard);
        return mInputView;
    }

}

I thought this should work and let me use the Relative Layout file for my custom keyboard but it is not. Since I'm really new to this I have no idea what I'm doing wrong.

What am I getting wrong?

Upvotes: 1

Views: 610

Answers (1)

Foobar
Foobar

Reputation: 8487

Here is the solution that Mike M. provided in the comments on my post.

First change the directory structure to this:

enter image description here

Note: I removed the keyboard.xml file since it did not seem to be used in this new approach, however it might be needed again.

Then I changed the code to:

    package com.roymunson.vroy.customtestkeyboard;

import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.view.View;
import android.widget.RelativeLayout;


public class customInputMethod extends InputMethodService {

    private Keyboard keyboard;

    @Override public void onInitializeInterface() {
        keyboard = new Keyboard(this, R.layout.custom_keyboard_layout);
    }

    @Override
    public View onCreateInputView() {

        RelativeLayout mInputView = (RelativeLayout) getLayoutInflater().inflate(R.layout.custom_keyboard_layout, null);
        return mInputView;
    }

}

Upvotes: 2

Related Questions