Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display custom keyboard when clicking on edittext in android

I have a custom keyboard in my application. question is How to didplay this keyboard when click on the edittext.I an using setonfocuschangre listener ,now the custon keyboaed appears when the edittext focus is changed.but i want to show this keyboard whenever i click on the edittext..one info I forget to put here the edittext is within the fragment.

like image 394
Senthilvel S Avatar asked Sep 11 '14 11:09

Senthilvel S


Video Answer


2 Answers

You can actually create an InputConnection for your CustomKeyBoard and then set this connection to your EditText

Using InputConnection, your CustomKeyBoard will replace the android keyboard and work as default.

You can get code from Here: https://medium.com/@ssaurel/creating-an-in-app-keyboard-for-your-android-apps-a0a6c6e1e289

keyboard.xml :

<?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="horizontal">

        <Button
            android:id="@+id/button_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>

        <Button
            android:id="@+id/button_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2"/>

        <Button
            android:id="@+id/button_3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"/>

        <Button
            android:id="@+id/button_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4"/>

        <Button
            android:id="@+id/button_5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6"/>

        <Button
            android:id="@+id/button_7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7"/>

        <Button
            android:id="@+id/button_8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="8"/>

        <Button
            android:id="@+id/button_9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="9"/>

        <Button
            android:id="@+id/button_0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="0"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Delete"/>

        <Button
            android:id="@+id/button_enter"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Enter"/>

    </LinearLayout>

</merge>

MyKeyBoard.java :

public class MyKeyboard extends LinearLayout implements View.OnClickListener {

private Button button1, button2, button3, button4,
                button5, button6, button7, button8,
                button9, button0, buttonDelete, buttonEnter;

private SparseArray<String> keyValues = new SparseArray<>();
private InputConnection inputConnection;

public MyKeyboard(Context context) {
    this(context, null, 0);
}

public MyKeyboard(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MyKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    LayoutInflater.from(context).inflate(R.layout.keyboard, this, true);
    button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(this);
    button2 = (Button) findViewById(R.id.button_2);
    button2.setOnClickListener(this);
    button3 = (Button) findViewById(R.id.button_3);
    button3.setOnClickListener(this);
    button4 = (Button) findViewById(R.id.button_4);
    button4.setOnClickListener(this);
    button5 = (Button) findViewById(R.id.button_5);
    button5.setOnClickListener(this);
    button6 = (Button) findViewById(R.id.button_6);
    button6.setOnClickListener(this);
    button7 = (Button) findViewById(R.id.button_7);
    button7.setOnClickListener(this);
    button8 = (Button) findViewById(R.id.button_8);
    button8.setOnClickListener(this);
    button9 = (Button) findViewById(R.id.button_9);
    button9.setOnClickListener(this);
    button0 = (Button) findViewById(R.id.button_0);
    button0.setOnClickListener(this);
    buttonDelete = (Button) findViewById(R.id.button_delete);
    buttonDelete.setOnClickListener(this);
    buttonEnter = (Button) findViewById(R.id.button_enter);
    buttonEnter.setOnClickListener(this);

    keyValues.put(R.id.button_1, "1");
    keyValues.put(R.id.button_2, "2");
    keyValues.put(R.id.button_3, "3");
    keyValues.put(R.id.button_4, "4");
    keyValues.put(R.id.button_5, "5");
    keyValues.put(R.id.button_6, "6");
    keyValues.put(R.id.button_7, "7");
    keyValues.put(R.id.button_8, "8");
    keyValues.put(R.id.button_9, "9");
    keyValues.put(R.id.button_0, "0");
    keyValues.put(R.id.button_enter, "\n");
}

@Override
public void onClick(View view) {
    if (inputConnection == null)
        return;

    if (view.getId() == R.id.button_delete) {
        CharSequence selectedText = inputConnection.getSelectedText(0);

        if (TextUtils.isEmpty(selectedText)) {
            inputConnection.deleteSurroundingText(1, 0);
        } else {
            inputConnection.commitText("", 1);
        }
    } else {
        String value = keyValues.get(view.getId());
        inputConnection.commitText(value, 1);
    }
}

public void setInputConnection(InputConnection ic) {
    inputConnection = ic;
}

}

activity_main.xml :

<?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="com.ssaurel.inappkeyboard.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#c9c9f1"
        android:layout_margin="50dp"
        android:padding="5dp"
        android:layout_alignParentTop="true"
        />

    <com.ssaurel.inappkeyboard.MyKeyboard
        android:id="@+id/keyboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

MainActivity.java :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editText = (EditText) findViewById(R.id.editText);
        MyKeyboard keyboard = (MyKeyboard) findViewById(R.id.keyboard);
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);

        InputConnection ic = editText.onCreateInputConnection(new EditorInfo());
        keyboard.setInputConnection(ic);
    }
}
like image 96
xlPro Avatar answered Oct 23 '22 04:10

xlPro


I created a Custom Keyboard in my application using Keyboard tag. I am adding this keyboard in a RelativeLayout on my screen like.

private void createCustomKeyboard() {
  Keyboard customKeyboard = new Keyboard(getActivity(), R.layout.keyboard);
  CustomKeyboard mCustomKeyboard = new CustomKeyboard(getActivity(), this);
  mCustomKeyboard.setKeyboard(customKeyboard);
  RelativeLayout relLayKeyboard.addView(mCustomKeyboard);  
} 

If you want to use this CustomKeyboard on one or more than one EditText then you have to use below code :

EditText edtxtName = (EditText) v.findViewById(R.id.edtName);
RelativeLayout relLayKeyboard = (RelativeLayout)findViewById(R.id.relLay_keyboard);
edtxtName.setOnTouchListener(exitSoftKeyBoard);

private final OnTouchListener exitSoftKeyBoard = new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
    InputMethodManager imm = (InputMethodManager) getActivity().getApplicationContext().getSystemService(
            android.content.Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    if(v.equals(edtxtName)){
        edtxtName.requestFocus();
        relLayKeyboard.setVisibility(View.VISIBLE);
    } 
    return true;
  }
};
like image 40
Satya Avatar answered Oct 23 '22 04:10

Satya