Possible Duplicate:
Close/hide the Android Soft Keyboard
First thing first I already saw this thread. I tried accepted methods given there..But nothing worked for me..
I have two screens in my app.
- First one have 2 EditText\'s - One for username and one for password
- Second one have one ListView, and an EditText - to filter the listView
In my first screen, I want username EditText to have focus on startup and Keyboard should be visible..This is my implementation (simplified by removing unnecessary/unrelated codes)..
app_login.xml
<LinearLayout android:orientation=\"vertical\" android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\" android:paddingLeft=\"20dip\"
android:paddingRight=\"20dip\">
<EditText android:id=\"@+id/username\" android:singleLine=\"true\"
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\" android:hint=\"Username\"
android:imeOptions=\"actionDone\" android:inputType=\"text\"
android:maxLines=\"1\"/>
<EditText android:id=\"@+id/password\" android:password=\"true\"
android:singleLine=\"true\"
android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"
android:hint=\"Password\" />
</LinearLayout>
AppLogin.java
class AppLogin extends Activity{
private EditText mUserNameEdit = null;
private EditText mPasswordEdit = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.app_login);
mUserNameEdit = (EditText) findViewById(R.id.username);
mPasswordEdit = (EditText) findViewById(R.id.password);
/* code to show keyboard on startup.this code is not working.*/
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mUserNameEdit, InputMethodManager.SHOW_IMPLICIT);
}//End of onCreate()
}
Well, the keyboard is not showing at start up. And my design badly require a keyboard there..
Now on to second page..I already said I have a listView and EditText there..I want my keyboard to be hidden on start up only to appear when user touches the editText..Can you believe it? whatever I tried soft Keyboard is showing when I load the activity..I am not able to hide it..
app_list_view.xml
<LinearLayout android:layout_width=\"fill_parent\" android:layout_height=\"fill_parent\"
android:orientation=\"vertical\" >
<EditText android:id=\"@+id/filter_edittext\"
android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"
android:hint=\"Search\" android:inputType=\"text\" android:maxLines=\"1\"/>
<ListView android:id=\"@id/android:list\" android:layout_height=\"fill_parent\"
android:layout_weight=\"1.0\" android:layout_width=\"fill_parent\"
android:focusable=\"true\" android:descendantFocusability=\"beforeDescendants\"/>
</LinearLayout>
AppList.java
public class MyListActivity extends ListActivity{
private EditText mfilterEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_list_view);
mFilterEditText = (EditText) findViewById(R.id.filter_edittext);
InputMethodManager imm = InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mFilterEditText.getWindowToken(), 0);
}
}
To simplify
- On Login Page (first Page) I want my keyboard to be visible on start up..
- On SecondPage I want keyboard to be hidden first, only to appear when user touches editText
And my problem is I am getting exact opposite on both occasion...Hope someone faced this issue before..BTW I am testing on simulator and HTC Desire phone..
FINAL OUTCOME
Well I got it working, with the help of all friends here.
1. To Show keyboard on startup
Two answers worked for me. One provided by @CapDroid, which is to use a handler and post it delayed..
mUserNameEdit.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(mUserNameEdit, 0);
}
},50);
Second answers is provided by @Dyarish, In fact he linked to another SO thread, which I haven\'t seen before. But the funny thing is that, this solution is given in the thread which I referenced at start. And I haven\'t tried it out because it had zero votes in a thread where all other posts have plenty of votes..Height of foolishness..
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
For me the second solution looked neat, so I decided to stick with it..But first one certainly works. Also @Dyarish\'s answer contain a clever hack of using a ScrollView below EditText to give EditText the focus..But I haven\'t tried it, but it should work. Not neat though..
2. To hide keyboard at activity start
Only one answer worked for me, which is provided by @Dyarish. And the solution is to use focusableInTouchMode settings in xml for the layout containing the editText\'s. This did the trick
<LinearLayout android:layout_width=\"fill_parent\" android:layout_height=\"fill_parent\"
android:orientation=\"vertical\" android:focusableInTouchMode=\"true\">
<EditText android:id=\"@+id/filter_edittext\"
android:layout_width=\"fill_parent\" android:layout_height=\"wrap_content\"
android:hint=\"Search\" android:inputType=\"text\" android:maxLines=\"1\"/>
<ListView android:id=\"@id/android:list\" android:layout_height=\"fill_parent\"
android:layout_weight=\"1.0\" android:layout_width=\"fill_parent\"
android:focusable=\"true\" android:descendantFocusability=\"beforeDescendants\"/>
</LinearLayout>
Anyway I end up using Dyarish\'s answer in both cases. So I am awarding the bounty to him..Thanks all other friends who tried to help me..