KCN
KCN

Reputation: 462

In my project keyboard should fixed in my Activity itself

In my project keyboard should fixed in my Activity itself without hiding that but in my code the keyboard automatically visible upon starting activity but problem is by clicking back button the keyboard was invisible.

So can any one suggest answer to fix keyboard in my activity

Thank you in advance

enter image description here

See like in this image my keyboard want to be fixed in this activity .Example like a calculator keyboard want to be fixed under edit text

Upvotes: 1

Views: 382

Answers (2)

KCN
KCN

Reputation: 462

I got solution for my question.Without using softkeyboard I developed the fixed custom keyboard in my activity

public class keyboardActivity1 extends Activity {

    private EditText et;
    Keyboard keyboard;
    KeyboardView keyview;
    boolean caps = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keyboard1);
        //create Keyboard object
        keyboard = new Keyboard(this, R.xml.keypad);
        //create KeyboardView object
        keyview = (KeyboardView) findViewById(R.id.customkeyboard);
        //attache the keyboard object to the KeyboardView object
        keyview.setKeyboard(keyboard);
        //show the keyboard
        keyview.setVisibility(KeyboardView.VISIBLE);
        //take the keyboard to the front
        keyview.bringToFront();
        //register the keyboard to receive the key pressed
        keyview.setOnKeyboardActionListener(new KeyList());
        et = (EditText) findViewById(R.id.txt_edit);

    }

    class KeyList implements KeyboardView.OnKeyboardActionListener {
        public void onKey(View v, int keyCode, KeyEvent event) {

        }
        public void onText(CharSequence text) {

        }

        public void swipeLeft() {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {

        }

        public void swipeUp() {

        }

        public void swipeDown() {

        }

        public void swipeRight() {

        }

        // @TargetApi(Build.VERSION_CODES.KITKAT)
        public void onPress(int primaryCode) {
            // InputConnection ic = getCurrentInputConnection();


            if (primaryCode == -5) { //take the last character out when delete button is pressed.
                String text = et.getText().toString();
                if (et.length() > 0) {
                    text = text.substring(0, text.length() - 1);
                    et.setText(text);
                    et.setSelection(text.length());
                }
            } else if (primaryCode == -1) {

                caps = !caps;
                keyboard.setShifted(caps);
                keyview.invalidateAllKeys();
               // char code = (char) primaryCode;
                //  if (Character.isLetter(code) && caps) {
               // code = Character.toUpperCase(code);
              //  et.append("" + code);


            } else if (primaryCode == 10) {
                String text = et.getText().toString();
                et.setText(text);

            } else {
                char code = (char) primaryCode;

                // else if(Character.isLetter(code) && caps)){


                if (Character.isLetter(code) && caps) {
                    // char code = (char) primaryCode;

                    code = Character.toUpperCase(code);
                    //et.setText(code);
                    et.append(String.ValueOf(code));
                } else { // char code = (char) primaryCode;
                    code = Character.toLowerCase(code);
                    et.append(String.ValueOf(code));
                }
            }
        }

        @Override
        public void onRelease(int i) {

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_keyboard_activity1, menu);
        return true;
    }

}

keyboard activity.xml

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/txt_edit"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="top"
        />

    <android.inputmethodservice.KeyboardView
        android:visibility="gone"
        android:id="@+id/customkeyboard"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="bottom"
        android:keyPreviewLayout="@layout/preview"

        />

Create keyboard code under res/xml/keypad.xml

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:keyWidth="40%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="50dp"
    >
    <Row>
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/>
        <Key android:codes="119" android:keyLabel="w"/>
        <Key android:codes="101" android:keyLabel="e"/>
        <Key android:codes="114" android:keyLabel="r"/>
        <Key android:codes="116" android:keyLabel="t"/>
        <Key android:codes="121" android:keyLabel="y"/>
        <Key android:codes="117" android:keyLabel="u"/>
        <Key android:codes="105" android:keyLabel="i"/>
        <Key android:codes="111" android:keyLabel="o"/>
        <Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="97" android:keyLabel="a" android:keyEdgeFlags="left"/>
        <Key android:codes="115" android:keyLabel="s"/>
        <Key android:codes="100" android:keyLabel="d"/>
        <Key android:codes="102" android:keyLabel="f"/>
        <Key android:codes="103" android:keyLabel="g"/>
        <Key android:codes="104" android:keyLabel="h"/>
        <Key android:codes="106" android:keyLabel="j"/>
        <Key android:codes="107" android:keyLabel="k"/>
        <Key android:codes="108" android:keyLabel="l"/>
        <Key android:codes="64" android:keyLabel="\@" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="-1" android:keyLabel="^" android:keyEdgeFlags="left" android:isSticky="true"/>
        <Key android:codes="122" android:keyLabel="z"/>
        <Key android:codes="120" android:keyLabel="x"/>
        <Key android:codes="99" android:keyLabel="c"/>
        <Key android:codes="118" android:keyLabel="v"/>
        <Key android:codes="98" android:keyLabel="b"/>
        <Key android:codes="110" android:keyLabel="n"/>
        <Key android:codes="109" android:keyLabel="m"/>
        <Key android:codes="46" android:keyLabel="."/>
        <Key android:codes="35" android:keyLabel="\#" android:keyEdgeFlags="right"/>
    </Row>
    <Row android:rowEdgeFlags="bottom">
        <Key android:codes="44" android:keyLabel="," android:keyWidth="10%p"  android:keyEdgeFlags="left"/>
        <Key android:codes="47" android:keyLabel="/" android:keyWidth="10%p" />
        <Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="40%p" android:isRepeatable="true"/>
        <Key android:codes="-5" android:keyLabel="DEL" android:keyWidth="20%p" android:isRepeatable="true"/>
        <Key android:codes="-10" android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right"/>
    </Row>
</Keyboard>

Upvotes: 2

Long Phan
Long Phan

Reputation: 56

We have 2 ways to hidden the keyboard:

1: You can set it always hidden when the activity running

android:windowSoftInputMode="stateHidden|stateAlwaysHidden"

2: You can using java code to hidden the keyboard

public static void hideKeyboardMachine(Activity activity) {
    InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view != null) {
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

I hope it helpful for you :)

Upvotes: 1

Related Questions