I'm really struggling with this, so I turn to you good people :)
Goal: during OnCreate set a value in the AutoCompleteTextView and close the keyboard.
Activity: contains 2 views, the AutoCompleteTextView and a Button
In OnCreate I call this;
private void SoftKeyboardClose()
{
IBinder windowToken;
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.ac_city);
Button myBtn = (Button) findViewById(R.id.btn_search);
windowToken = myBtn.getWindowToken();
if (windowToken == null)
{
windowToken = this.getWindow().getDecorView().getWindowToken();
}
if (windowToken == null)
{
windowToken = findViewById(R.id.ac_city).getWindowToken();
}
if (windowToken == null)
{
windowToken = new View(this).getWindowToken();
}
// close it
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager == null)
{
Log.e(TAG, "inputmanage is null");
} else if (windowToken == null)
{
Log.e(TAG, "getWindowToken is null");
} else
{
inputManager.hideSoftInputFromWindow(windowToken, 0);
}
myBtn.requestFocus();
if (myBtn.hasFocus())
{
Log.e(TAG, "success - btn has focus");
} else
{
Log.e(TAG, "fail - btn doesn't have focus");
}
}
getWindowToken always returns null.
In response to hpedrorodrigues I tried this;
public class ZZZActivity extends Activity
{
private static final String[] COUNTRIES = new String[]{"Belgium", "France", "Italy", "Germany", "Spain"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zzz);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_zzz, COUNTRIES);
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.myACTV);
textView.setAdapter(adapter);
closeKeyboard(this);
}
public void closeKeyboard(final Activity activity)
{
final View view = activity.getCurrentFocus();
if (view != null)
{
final InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
activity.getCurrentFocus() returned null.