Rajiv Thakur
Rajiv Thakur

Reputation: 61

Closing android default keyboard onClick of EditText

Okay! So there are many answers to this question. But none of it worked for me. Maybe I am going wrong somewhere.

What I want to do is that, I don't want the default android soft keyboard popping up when I click on my EditText.

Here is my xml file

Activity_main.xml

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="0dp"
        android:layout_marginTop="0dp"
        android:orientation="horizontal"
        android:focusableInTouchMode="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="INR"
            android:textSize="28dp"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="55dp"
            ></TextView>

        <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_marginLeft="15dp"
            android:src="@drawable/indiaflag"
            />

        <EditText
            android:id="@+id/secondText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:layout_marginLeft="70dp"
            android:textSize="28dp"
            android:text=""
            />
    </LinearLayout>

Here is the code for hiding the keyboard, I am using InputMethodManager to no show the keyboard on click of the EditText

secondText = (EditText) findViewById(R.id.secondText);
 InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(secondText, InputMethodManager.HIDE_NOT_ALWAYS);

I am using this code in OnCreate just after the declaration of the secondText, But this does not work. I even tried an alternate method of using the above code on setOnClickListener. But still no Success.

Can anyone tell me where am I going wrong? Thanks in advance

Upvotes: 0

Views: 112

Answers (2)

Ramesh Kumar
Ramesh Kumar

Reputation: 1249

Use This function for hide keyboared

 public static void hide_keyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view == null) 
        view = new View(activity);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Upvotes: 1

Jas
Jas

Reputation: 3212

Add:

secondText.setInputType(InputType.TYPE_NULL);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(secondText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

Instead of

secondText.setInputType(InputType.TYPE_NULL);

use

 OnTouchListener otl = new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              Layout layout = ((EditText) v).getLayout();
              float x = event.getX() + secondText.getScrollX();
              int offset = layout.getOffsetForHorizontal(0, x);
              if(offset>0)
                  if(x>layout.getLineMax(0))
                      secondText.setSelection(offset);     // touch was at end of text
                  else
                      secondText.setSelection(offset - 1);
              break;
              }
          return true;
          }
      };
      secondText.setOnTouchListener(otl);

Upvotes: 0

Related Questions