I want the Android Keyboard to pop-up on the startup of my activity. A simple google search shows that you just have to requestFocus which I do in my .xml, but it still doesn't pop up. Am I making any minor mistake that is causing this not to work?
Testing on:
Physical 4.1
Emulator 2.2
layout.xml:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="To:"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
This works:
myEditText.setOnFocusChangeListener( new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
Try this code:
EditText input = (EditText) findViewById(R.id.editText1);
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
Related
I'm struggling to get my keyboard to display in my EditText. When I click the EditText for the first time, the keyboard displays and behaves normally.
When I press the back button to hide the keyboard and click the same EditText again, the keyboard fails to display. I'd really appreciate any help. The relevant code is below.
NewEntry:
assessor.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
EditText email = (EditText) context.findViewById(R.id.email);
email.requestFocus();
return true;
}
Layout:
<EditText
android:layout_height="72dp"
android:layout_width="wrap_content"
android:id="#+id/assessor"
android:textColor="#color/black"
android:textSize="20dp"
android:background="#android:color/transparent"
android:fontFamily="sans-serif-light"
android:layout_marginLeft="72dp"
android:layout_marginRight="16dp"
android:hint="Assessor"
android:inputType="text"
android:imeOptions="actionNext"
android:ems="10"
android:windowSoftInputMode="adjustPan"
/>
Try this:
set an onClickListener for the EditText, and when it is clicked it will request focus
EditTextName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
editText.requestFocus();
}
I don't know if this will solve your problem, but at least i tried :P
I have a small function to open the soft keyboard when a EditText is programmatically focused as shown here...
public void getUserName() {
EditText tv = (EditText)findViewById(R.id.user_info_name);
tv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
showDialog("Focused!");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
}
});
tv.selectAll();
tv.requestFocus();
}
However, the soft keyboard doesn't appear automatically but the dialog DOES show stating focused. In order to get the keyboard to appear I have to click inside the EditText.
My XML is as follows...
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#id/user_info_name"
android:editable="true"
android:hint="#string/user_info_name"
android:inputType="textCapWords|textPersonName"
android:textColor="#color/blue_gray"
android:maxLength="50"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:enabled="true"
android:focusable="true"
android:focusableInTouchMode="true" />
Can someone please advise why it's not working or what I am missing / failing to address.
Thanks in advance as always.
SOLVED: The following change to the function fixed the issue...
public void getUserName() {
EditText tv = (EditText)findViewById(R.id.user_info_name);
tv.selectAll();
tv.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(this.INPUT_METHOD_SERVICE);
imm.showSoftInput(tv,InputMethodManager.SHOW_IMPLICIT);
}
In my app after some event i want my user to fill EditText. In order to do that, i request focus on this EditText and try to show keyboard using this code:
public void onGivenEvent(int which) {
mMyDialog.dismiss(getActivity());
mMyEditField.requestFocus();
InputMethodManager imm =
(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mMyEditField, InputMethodManager.SHOW_IMPLICIT);
}
xml for mMyEditField looks like this:
<EditText
android:id="#+id/edit_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="#null"
android:digits="0123456789.,"
android:freezesText="true"
android:hint="some hint"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxLength="15"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textColor="#android:color/black"
android:textSize="16sp" />
The funny thing is, this code doesn't show keyboard on first attempt. When this code is executed once, after device rotation keyboard is shown properly. I had similiar problem with android:selectAllOnFocus="true"not working correctly, but i used workaround - I set OnFocusChangeListener with mMyEditField.selectAll() inside.
Any ideas how to fix this issue?
Call This method from your onGivenEvent() function
public void showSoftKeyboard() {
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
If i click on my EditText, the virtual keyboard simple not shows up. The cursor is shown, but no keyboard to type on.
I even tried it with manually open but just no works.
Here is my code:
public class CreateNote extends Activity {
EditText titleEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createnote);
titleEdit = (EditText) findViewById(R.id.titleEdit);
titleEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) CreateNote.this
.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(titleEdit, 0);
}
});
}
}
Snippet of Layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#989898" >
<EditText
android:id="#+id/titleEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittextdrawale"
android:ems="10"
android:textColor="#fff"
android:textColorHint="#fff" >
<requestFocus />
</EditText>
</FrameLayout>
What could be the reason of playing hide and seek of my virtual keyboard ?
I test on real device, not on emulator.
Try with this, it worked for me.
EditText etHorseName = (EditText) getView().findViewById(R.id.horseName);
etHorseName.clearFocus();
in onCreate() or where you want.
Late answer but here is how to solve it without adding code, just remove this from your XML:
<requestFocus />
No idea why the keyboard does not show up when this is set... It does show up however if you first loose the focus and then click on the edit text. I had the problem on Android 2.3.6 but it worked on 4.1.2, so maybe it was an early bug.
It is just a default behavior , you not suppose to do it manually, remove below part from your code.
titleEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) CreateNote.this
.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(titleEdit, 0);
}
});
Try to hide and show the keyboard with this code:
InputMethodManager imm = (InputMethodManager) this.getSystemService(Service.INPUT_METHOD_SERVICE);
// To show keyboard
imm.showSoftInput(titleEdit, 0);
// To hide keyboard
imm.hideSoftInputFromWindow(titleEdit.getWindowToken(), 0);
I am facing a weird problem in edittext control of android.
The issue is, when i type anything in softkeyboard. the text is not getting printed in editbox.
Here is the code
mEditview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
});
mEditview.requestFocus();
layout code is added
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/filter_text"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="Search"/>
Here is what i get as output in emulator. Why its coming like this? how to fix this problem