Android click on EditText won't show softkeyboard - android

It happens when the user dismisses the softkeyboard and then tries to click/gain focus on the EditText again, nothing happens only the cursor is shown - I want to show the keyboard again.
I've tried:
Using an onclick event
Using a focuschanged event
Changing properties of the EditText (focusable etc...)
Note: I am currently using Paranoid Android. The EditText is Multiline.

I found the solution, I just had to remove the following attribute from my EditText:
android:textIsSelectable="true"

Please start by omitting any requestFocus defined for your EditText.
there's a known bug that prevents keyboard from showning if the latter is set.
If that doesn't work for you, create a focus listener and in it programmatically open the virt keyboard:
editTxt.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// show keyboard
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTxt, 0);
}
}
});

Here is a solution:
final InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
EditText e= (EditText) findViewById(R.id.editText1);
e.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imm.showSoftInput(e, InputMethodManager.SHOW_IMPLICIT);
}
});
e.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if(actionId==EditorInfo.IME_ACTION_GO){
imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
//Do you work here
}
return false;
}
});
and the edittext will be:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionGo"/>

Related

Edittext Keyboard Not Showing After First Input

So I need an input box for text in my android app.
I have this in my xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inputCodePA"
android:textSize="18sp"
android:inputType="text"
android:hint="#string/inputHint"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/arrowRightPA"
android:layout_toEndOf="#+id/arrowRightPA">
<requestFocus/>
</EditText>
and this:
public void setOnClickListners(){
final EditText inputBox = (EditText)findViewById(R.id.inputCodePA);
inputBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputBox.clearFocus();
inputBox.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputBox, InputMethodManager.SHOW_FORCED);
}
});
inputBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
inputBox.setText("");
inputBox.clearFocus();
inputBox.requestFocus();
return false;
}
});
}
For my code in my activity's class (setOnClickListner() is called in my onCreate()).
But, whenever after I type something into the EditText box and press Enter, I can not open the keyboard to enter text again.
I know I'm making a really basic error, but I can't seem to figure out what.
After all, this is the first time an EditText failed me.
HA!
I found out the problem!
It was because I had a ScrollView below the EditText in the code (which made the ScrollView become 'in front'), which did not allow the user to click on the view at all!
Hope this helps other people who makes these stupid mistakes!

Hiding softKeyboard when overriding standard android keyboard

I'm trying to override the soft input keyboard for a singular EditText field. I've mostly been following this excellent example -- setting the XML to inputType="text", and then, within the onCreate:
EditText amount = (EditText) findViewById(R.id.amount_edit_text);
final EditText amt = amount;
amount.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
showCustomKeyboard(v);
} else {
hideCustomKeyboard();
}
}
});
amount.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showCustomKeyboard(v);
}
});
amount.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType();
edittext.setInputType(InputType.TYPE_NULL);
edittext.onTouchEvent(event);
edittext.setInputType(inType);
return true;
}
});
This works for the most part -- clicking on the EditText brings up my custom keyboard, but there's always a jump. It's very quick, but it'll show my keyboard stacking on top of the standard keyboard, and then the standard keyboard will collapse and my keyboard will be left. And at times, it will arbitrarily not collapse and simply just stack...
Is there any way to override the standard keyboard with my own without this jump?
I ended up using a Runnable to close the standard keyboard before showing my custom keyboard. It's not quite perfect, but it doesn't stack the two keyboards like it used to. I'll keep looking for a better solution though.
within the else statement of my OnFocusChangeListener:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
showCustomKeyboard(v);
}
}, 250);
((InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(), 0);
Try to hide the softKeyboard when you are showing yours, using the following code:
InputMethodManager mgr = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(amount.getWindowToken(), 0);

Can not open soft keyboard in EditText control

I want to create 1 edit text with the below condition:
- User can not focus on this control in normal.
- When user click on this control, soft-keyborad is displayed and user can input into this control
- When user press enter on this soft-keyborad or back on device, it is closed and back to normal view with control is not focus.
I tried the below code but not work :(
When starting, control is not focus: ok
When click on control, at the first click, control is focus but not display soft-keyborad
In the second click, display soft-keyborad
When press enter button in soft-keyborad: back to screen with control is not focus: OK
When press back button device, back to screen with control is still focus : not ok
public void onCreate(Bundle savedInstanceState)
{
final EditText txtSearch = (EditText)this.findViewById(R.id.p60004_txt_search_str);
txtSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtSearch.setFocusable(true);//(false);
txtSearch.setFocusableInTouchMode(true);
txtSearch.requestFocus();
}
});
txtSearch.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
String strSearch = txtSearch.getText().toString();
if (strSearch != null && strSearch != ""){
searchFriend(UserAPIConstants.FRIEND_SEARCH_TYPE_SC, strSearch);
}
hideSoftKeyboard(v);
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
}
return false;
}
});
public void hideSoftKeyboard (View view) {
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your xml which has edittext put these values for the layout node
android:focusable="true"
android:focusableInTouchMode="true"
and dont put any focusable or focusable in touch mode attributes for your edittext..
Then in your code in onKey method remove thse lines..
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
and put
txtSearch.clearFocus();
And You should override this method
onBackPressed()
like this..
#Override
public void onBackPressed() {
txtSearch.clearFocus();
//hide the soft keyboard..
}
try commenting
hidekeyboard(v);
and the changes that Alex Lockwood suggested.
use this in onClick()
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
to close the keypad use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

Kindle Fire Keyboard Buttons

I have an EditText field in my app:
<EditText
android:id="#+id/task_buy_edit_mass_editText1"
android:minWidth="100dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:imeOptions="actionDone" >
</EditText>
The problem is that on my Kindle Fire, the keyboard that appears when the user wants to edit the EditText has no Done button, just a button that hides the soft keyboard
That wouldn't be a problem if I could intercept the 'hide keyboard' button that I highlighted on the above screenshot. However, it doesn't seem to trigger the OnEditorActionListener I've attached to the EditText, so I'm a bit stuck what to do aside from having my own 'Done' button in my actual layout to take focus away from the EditText, hide the keyboard and make any changes that result from the number being changed
Is there a way to intercept the 'hide keyboard' button, or is there some alternative solution?
Incidentally, I have a regular EditText view elsewhere in my app that has no imeOptions nor specifies inputType and that does have a Return key
Here's the rest of the code for reference:
editText.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE || actionId==EditorInfo.IME_ACTION_NEXT || actionId==EditorInfo.IME_NULL || event.getKeyCode()==KeyEvent.FLAG_EDITOR_ACTION || event.getKeyCode()==KeyEvent.KEYCODE_ENTER){
closeKeyboard(v, done_button);
return true;
}
return false;
}});
// set done button for keyboards without correct IME options
final EditText finalEditText = editText;
editText.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
done_button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
closeKeyboard(finalEditText, v);
}});
done_button.setVisibility(View.VISIBLE);
return false;
}
});
.
private void closeKeyboard(TextView v, View buttonv){
demand.setAmount(Double.parseDouble((v.getText().toString()))); // update the game
// close keyboard
InputMethodManager inputManager = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
v.clearFocus();
buttonv.setVisibility(View.GONE);
}
android:inputType="textMultiLine" and android:singleLine="true" as #James Wald indicated works.
EDIT:
Doing the above will make it the EditText multi line. Instead android:imeOptions="actionGo" worked for my purposes on all devices (AFAIK).

can i write my own event on android:imeOptions="actionSearch"?

i am using android:imeOptions="actionSearch"
in editText and my question is can i write my own event if user presses Searchbutton on Softkeyboard?
actualy i want to perform functionality of softkeyboard search button similar to button we use on android activity.
any help would be appriciated.
This is what I ended up using:
EditText SearchEditText = (EditText) findViewById(R.id.txtMapSearch);
SearchEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
// search pressed and perform your functionality.
}
return false;
}
});
Call setOnEditorActionListener() on the EditText to register a TextView.OnEditorActionListener that will be invoked when the user taps the action button on the soft keyboard.

Categories

Resources