0

I have two EditText and two AutoCompleteTextView in my layout. But I like to hide the keyboard only for the AutoCompleteTextView. But it does not hide the keyboard and when I touch the AutoCompleteTextView, Keyboard still appear, how can I hide the keyboard? I implemented as

final View addView = getLayoutInflater().inflate(R.layout.addnewtracker, null);                     
        final TrackerInfo newInfo = new TrackerInfo();          

        String[] type = {"Vehicle", "Person", "Pet", "Others"}; 
        final AutoCompleteTextView actvtype1 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView1);             
        ArrayAdapter<String> typeadapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,type);          
        actvtype1.setThreshold(1);
        actvtype1.setAdapter(typeadapter);
        actvtype1.setTextColor(Color.BLACK);            

        String[] model = {"TS102", "TS103"};
        final AutoCompleteTextView actvtype2 = (AutoCompleteTextView) addView.findViewById(R.id.autoCompleteTextView2);
        ArrayAdapter<String> modeladapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_multiple_choice,model);
        actvtype2.setThreshold(1);
        actvtype2.setAdapter(modeladapter);
        actvtype2.setTextColor(Color.BLACK);            

        final AlertDialog.Builder alert = new AlertDialog.Builder(this).setTitle("New Tracker").setView(addView);
        InputMethodManager keyboard = (InputMethodManager)
        getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromInputMethod(actvtype1.getWindowToken(), 0);
        keyboard.hideSoftInputFromInputMethod(actvtype2.getWindowToken(), 0); 
        alert.setPositiveButton("ADD", new DialogInterface.OnClickListener() 
        {


            @SuppressLint("SimpleDateFormat")
            public void onClick(DialogInterface dialog, int whichButton) 
            {                   


            }
        }).setNegativeButton("Cancel", null).show();

My layout is as follow

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/IDnumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/IDnumber" />

    <EditText
        android:id="@+id/IDeditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:inputType="text">        
        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/SimCardNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Sim_card_number" />

    <EditText
        android:id="@+id/SimCardEdit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
         />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/description" />   

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"
        android:dropDownHeight="100dp"  
        android:text=""/>

    <TextView
        android:id="@+id/model"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Tracker_model"         
        />    
    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"         
        android:dropDownHeight="100dp"  
        android:text=""/>



</LinearLayout>
batuman
  • 7,066
  • 26
  • 107
  • 229

4 Answers4

8

I'm not sure if this is exactly what you're looking for but this is the code that is used to force the keyboard from popping up at the bottom of the screen:

actvtype1.setInputType(InputType.TYPE_NULL);

This will definitely stop the keyboard appearing when you click on that TextView. Hope it helps!

Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
edwoollard
  • 12,245
  • 6
  • 43
  • 74
  • I tried actvtype1.setInputType(0); It stops both keyboard and my auto complete text. I just want to stop keyboard. – batuman Nov 07 '13 at 10:02
  • I presume that you could use this line in the onCreate: this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); and then you could also try adding it to an onClick for that TextView? – edwoollard Nov 07 '13 at 10:09
  • 1
    Now it works. I set setonclicklistener for the AutoCompleteTextView. Then inside I changed to showDropDown. My implementation is as shown in the link http://stackoverflow.com/questions/19792701/autocompletetextview-implementation-on-the-alertdialog/19833710#19833710 – batuman Nov 07 '13 at 10:43
2

Request for the focus on autotext as :

   <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView2"
                ..
                .../>
         <requestFocus />

And then use this code:

 if(autocomplete.hasfocus()){
    hideSoftKeyboard();
    }


    private void hideSoftKeyboard() {
        if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText)
        {
             InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
             imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }

     }
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
  • Thanks for the suggestion. But now I can make it work as in the way shown in the following link ttp://stackoverflow.com/questions/19792701/autocompletetextview-implementation-on-the-alertdialog/19833710#19833710 – batuman Nov 07 '13 at 10:43
  • 1
    dude same can be done using this code,u just have to add one single line of code to show dropdown. – Pankaj Arora Nov 07 '13 at 11:40
  • Thanks, @CodingTiger this indeed was the easiest way to do it for me – Boy Oct 09 '14 at 17:43
0

To hide keyboard from onItemClick after clicking on list item in AutoCompleteTextView

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
     InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
     // whatever else should be done
}
tony gil
  • 9,424
  • 6
  • 76
  • 100
-1

use this code in your Autocomplete TextView onClick

  YourClass.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Mahesh Gawhane
  • 348
  • 3
  • 20