Android Hide Soft Keyboard from EditText while not losing cursor - android

I've come about as far as this which gets me halfway there, but not quite.
I have a dialer Fragment that has all the usual Buttons to enter a number including backspace, so I don't need the soft keyboard. I'd also like to give the user the ability to paste text (long click... works fine per default), as well as to edit what has been entered so I need the cursor.
The easiest way I found to make sure the soft keyboard doesn't pop up if the user clicks inside the EditText is to set the inputType to null - but that kills the cursor as well.
So, how do I declare my EditText and what kind of commands should I launch to have my EditText field never ever show the soft keyboard no matter what the user attempts, but still retain paste functionality and the cursor?
I've also tried android:windowSoftInputMode="stateAlwaysHidden" in my manifest, but to no avail.

This worked for me:
// Update the EditText so it won't popup Android's own keyboard, since I have my own.
EditText editText = (EditText)findViewById(R.id.edit_mine);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});

I have finally found a (for me) working solution to this.
First part (in onCreate):
// Set to TYPE_NULL on all Android API versions
mText.setInputType(InputType.TYPE_NULL);
// for later than GB only
if (android.os.Build.VERSION.SDK_INT >= 11) {
// this fakes the TextView (which actually handles cursor drawing)
// into drawing the cursor even though you've disabled soft input
// with TYPE_NULL
mText.setRawInputType(InputType.TYPE_CLASS_TEXT);
}
In addition, android:textIsSelectable needs to be set to true (or set in onCreate) and the EditText must not be focused on initialization. If your EditText is the first focusable View (which it was in my case), you can work around this by putting this just above it:
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" >
<requestFocus />
</LinearLayout>
You can see the results of this in the Grapher application, free and available in Google Play.

Setting the flag textIsSelectable to true disables the soft keyboard.
You can set it in your xml layout like this:
<EditText
android:id="#+id/editText"
...
android:textIsSelectable="true"/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.

Best solution from #Lupsaa here:
Setting the flag textIsSelectable to true disables the soft keyboard.
You can set it in your xml layout like this:
<EditText
android:id="#+id/editText"
...
android:textIsSelectable="true"/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.

If your min SDK is 21, you can this method from java code:
editText.setShowSoftInputOnFocus(false);
Credits to Chen Su article.

use
android:windowSoftInputMode="stateHidden"
in your manifest file instead of android:windowSoftInputMode="stateAlwaysHidden"

This is what I did.
First, in manifest inside activity
android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
Second, in onCreate if inside activity or onActivityCreated if inside fragment
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideSoftKeyboard(v);
}
});
Do not forget to request focus to the editText
editText.requestFocus();
Then add the hideSoftKeyboard(v) method same as the other answer.
private void hideSoftKeyboard(View v){
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
The key here is to requestFocus before clicking the EditText. If without focus, first click will make the keyboard show up(my experience). However, this is applied if you have a single EditText in an activity. With this, you still can type with custom keyboard(if any), can copy and paste, and cursor is still visible.

The exact functionality that you require is provided by setting the flag textIsSelectable in EditText to true. With this, the cursor will still be present, and you'll be able to select/copy/cut/paste, but SoftKeyboard will never show. Requires API 11 and above.
You can set it in your xml layout like this:
<EditText
android:textIsSelectable="true"
...
/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
For anyone using API 10 and below, hack is provided here :
https://stackoverflow.com/a/20173020/7550472

This works perfectly (for me) in 2 steps:
<activity... android:windowSoftInputMode="stateHidden"> in manifest file
Add these properties in your editText XML code
android:focusable="true"
android:focusableInTouchMode="true
You have to put both 1 and 2, only then it will work.
Cheers

EditText text = (EditText) findViewById(R.id.text);
if (Build.VERSION.SDK_INT >= 11) {
text.setRawInputType(InputType.TYPE_CLASS_TEXT);
text.setTextIsSelectable(true);
} else {
text.setRawInputType(InputType.TYPE_NULL);
text.setFocusable(true);
}

First add android:windowSoftInputMode="stateHidden" in your manifest file, under the activity. like this
<activity... android:windowSoftInputMode="stateHidden">
The on your xml add this android:textIsSelectable="true" . This will make the pointer visible.
Then on onCreate method of the activity, add this:
EditText editText = (EditText)findViewById(R.id.edit_text);
edit_text.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager inputMethod = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethod!= null) {
inputMethod.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});

i found this very useful code and it work as charm, it head the Keyboard totaly, but keeping cursor and you can copy past, move the cursor...ect
using :
hideSoftKeyboard(editText);
methode :
public void hideSoftKeyboard(EditText edit) {
if (android.os.Build.VERSION.SDK_INT <= 10) {
edit.setInputType(InputType.TYPE_NULL);
} else {
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setSoftInputShownOnFocus;
setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setSoftInputShownOnFocus.setAccessible(true);
setSoftInputShownOnFocus.invoke(edit, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}

EditText editText = (EditText)findViewById(R.id.edit_mine);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
ha... this is the correct way of doing...this job done... this gonna work !

You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this
Works Perfectly for me!! Try this.. :)`
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Related

Disable soft keyboard for an Edittext

I have an EditText where I want to handle the inputs myself, so I don't want the soft keyboard to show up when I click it (or when selection changes, focus changed, long clicked, etc). However, I still want to be able select the text, change cursor position, copy/past, etc.
I have tried putting android:windowSoftInputMode="stateAlwaysHidden" in the manifest, but that doesn't seems to do much. I also tried adding the following
edittext.setOnTouchListener(new OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
which disables the keyboard, but also prevent the cursor from working.
Currently I'm basically trying to add listeners for all the situations where the keyboard might pop up to toggle it off, but this is very clunky and I can't catch all the cases. Is there a better way to disable the soft keyboard for a particular EditText or fragment?
Obviously the best solution would be if Google gave an inputType that works like this.
The following tends to work. It will sometimes flicker as the keyboard is loaded and then is instantly murdered. But, it just listens for when you click on the textview and then when that happens it murders the keyboard.
It can't account for things like if the textfield gets focus some other way, but for my purposes (I have a textfield for a barcode reader that gets a barcode read into it by a barcode reader (hardware keyboard)), so a softkeyboard makes no sense.
editView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
I also added the other line you might want in there and commented it out. Namely if you want to hide the Android launched soft keyboard or if a user loads the keyboard by holding menu if that might close too.
Try this code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Disable IME for this application
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
setContentView(R.layout.activity_layout);

OnClick event only works second time on edittext

I have an edittext, and when the user clicks this edittext I want to show an alertdialog.
My code is the following :
edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(true);
edt.requestFocus();
edt.setCursorVisible(false);
edt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.
But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.
How do I fix this?
Simply try to add this to your XML file. Your keyboard pops up when widget gains focus.
So to prevent this behaviour set focusable to false. Then normal use OnClickListener.
<EditText
android:focusable="false"
...
/>
Now, it should works.
You can use onTouch instead of onClick, so it doesn't matter if the EditText has focus or not.
edt.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
return false;
}
});
Nothing much to do you just have to
edt.setFocusable(false);
If focusableInTouchMode is true then touch is triggered in second touch only, so unless you want that case use false for focusableInTouchMode. and if you want to enable the focusability in the view set focusable true
<EditText android:focusable="true" android:focusableInTouchMode="false" ... />
make your alert dialog box appear on
setOnFocusChangedListener()
You should add onFocusChangeListener:
edt.setKeyListener(null);
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
edt.callOnClick();
}
}
});
edt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
Avoid using a FocusChangeListener since it will behave erratically when you don't really need it (eg. when you enter an activity). Just set an OnTouchListener along with your OnClickListener like this:
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
This will cause your EditText to receive focus before your onClick call.
Instead of setting input type use "Editable=false" and "Focus=false" if you don't require keyboard.
It maybe helpful to you.
This was a real problem for me when trying to reproduce a "click" sound from the EditText when the soft keyboard pops up; I was only getting a click every second time. What fixed it for me was the the opposite of what worked for #neaGaze. This worked for me in my_layout.xml :
<EditText android:focusable="true" android:focusableInTouchMode="true" ... />
It allows the click sound/event to happen each time when user enters the EditText, while also allowing the soft keyboard to show. You have to handle the OnClickListener of course for this to happen, even if you do nothing with it, like so :
myEditText = (EditText) findViewById(R.id.myEditText);
...
// implement the onClick listener so we get the click sound and event if needed
myEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//do something or nothing; up to you
}
});
Speaking of that pesky soft keyboard, if I finished from my Dialog style Activity with the soft keyboard up, no matter what I tried the keyboard remained up when I was returned to MainActivity. I had tried all the usual suggestions such as Close/Hide the Android soft keyboard , How to close Android soft keyboard programmatically etc. None of that worked.
In my case I did not need the soft keyboard in MainActivity. What did work was the following in my AndroidManifest.xml file, within the MainActivity section
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>

How to hide Android soft keyboard on EditText

I have an Activity with some EditText fields and some buttons as a convenience for what normally would be used to populate those fields. However when we the user touches one of the EditText fields the Android soft keyboard automatically appears. I want it to remain hidden by default, unless the user long presses the menu button. I have search for a solution to this and found several answers, but so far I can't get them to work.
I have tried the following:
1 - In the onCreate method,
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
2 - Also in the onCreate method,
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
3 - and fIn the Manifest file,
<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/>
None of these methods work. Whenever the user clicks on the EditText field, the soft keyboard appears. I only want the soft keyboard to appear if the user explicitly shows it by long pressing the menu key.
Why isn't this working?
This will help you
editText.setInputType(InputType.TYPE_NULL);
Edit:
To show soft keyboard, you have to write following code in long key press event of menu button
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
You need to add the following attribute for the Activity in your AndroidManifest.xml.
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
/>
After long time looking into TextView class I found a way to prevent keyboard to appears. The trick is hide it right after it appears, so I searched a method that is called after keyboard appear and hide it.
Implemented EditText class
public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* This method is called before keyboard appears when text is selected.
* So just hide the keyboard
* #return
*/
#Override
public boolean onCheckIsTextEditor() {
hideKeyboard();
return super.onCheckIsTextEditor();
}
/**
* This methdod is called when text selection is changed, so hide keyboard to prevent it to appear
* #param selStart
* #param selEnd
*/
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
hideKeyboard();
}
private void hideKeyboard(){
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
and style
<com.my.app.CustomViews.NoImeEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:background="#null"
android:textSize="#dimen/cell_text" />
I sometimes use a bit of a trick to do just that. I put an invisible focus holder somewhere on the top of the layout. It would be e.g. like this
<EditText android:id="#id/editInvisibleFocusHolder"
style="#style/InvisibleFocusHolder"/>
with this style
<style name="InvisibleFocusHolder">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:inputType">none</item>
</style>
and then in onResume I would call
editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
editInvisibleFocusHolder.requestFocus();
That works nicely for me from 1.6 up to 4.x
My test result:
with setInputType:
editText.setInputType(InputType.TYPE_NULL);
the soft keyboard disappears, but the cursor will also disappear.
with setShowSoftInputOnFocus:
editText.setShowSoftInputOnFocus(false)
It works as expected.
The soft keyboard kept rising even though I set EditorInfo.TYPE_NULL to the view.
None of the answers worked for me, except the idea I got from nik431's answer:
editText.setCursorVisible(false);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
The following line is exactly what is being looked for. This method has been included with API 21, therefore it works for API 21 and above.
edittext.setShowSoftInputOnFocus(false);
There seems to be quite a variety of ways of preventing the system keyboard from appearing, both programmatically and in xml. However, this is the way that has worked for me while supporting pre API 11 devices.
// prevent system keyboard from appearing
if (android.os.Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
} else {
editText.setRawInputType(InputType.TYPE_NULL);
editText.setFocusable(true);
}
Let's try to set the below properties in your xml for EditText
android:focusableInTouchMode="true" android:cursorVisible="false".
if you want to hide the softkeypad at launching activity please go through this link
Three ways based on the same simple instruction:
a). Results as easy as locate (1):
android:focusableInTouchMode="true"
among the configuration of any precedent element in the layout, example:
if your whole layout is composed of:
<ImageView>
<EditTextView>
<EditTextView>
<EditTextView>
then you can write the (1) among ImageView parameters and this will grab android's attention to the ImageView instead of the EditText.
b). In case you have another precedent element than an ImageView you may need to add (2) to (1) as:
android:focusable="true"
c). you can also simply create an empty element at the top of your view elements:
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" />
This alternative until this point results as the simplest of all I've seen. Hope it helps...
Simply Use
EditText.setFocusable(false); in activity
or
use in xml
android:focusable="false"
Simply use below method
private fun hideKeyboard(activity: Activity, editText: EditText) {
editText.clearFocus()
(activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(editText.windowToken, 0)
}
weekText = (EditText) layout.findViewById(R.id.weekEditText);
weekText.setInputType(InputType.TYPE_NULL);
Hide the keyboard
editText.setInputType(InputType.TYPE_NULL);
Show Keyboard
etData.setInputType(InputType.TYPE_CLASS_TEXT);
etData.setFocusableInTouchMode(true);
in the parent layout
android:focusable="false"
public class NonKeyboardEditText extends AppCompatEditText {
public NonKeyboardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
return false;
}
}
and add
NonKeyboardEditText.setTextIsSelectable(true);
I also faced the same problem, I fixed that via this method,
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// do something..
}
closeKeyborad();
return true;
}
return false;
}
});
Call that function before return true.
private void closeKeyborad() {
View view = this.getCurrentFocus();
if (view != null){
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken() , 0);
}
}

Keyboard not shown when i click on edittextview in android?

When i click on the edittextview then some times keyboard shown or some times keyboard are not shown.
In android 2.1 it show the keyboard when i click on the edittextview
but when i start same application it on android 2.2 then it not show the keyboard.
Help me how to show that problem.
OK, This might be a late response, but it worked.
I have met this problem on android 2.1 and 2.3.x(not tested on other versions of SDKs).
I noticed a strange thing that when my click on the EditText was unable to open the keyboard, I pressed the BACK button to show an alert dialog and then I canceled(closed) it, and clicked the EditText again, now the keyboard was brought to life again.
From that I can conclude that the keyboard will always show for the EditText if the EditText does not previously own focus(showing an alert dialog over the EditText view will make the EditText to lose focus).
so call the function below on your EditText when it is brought to front:
mEditText.clearFocus();
or
parentViewThatContainsEditTextView.clearFocus();
I had a similar problem on Galaxy S3 (displaying EditText controls on a PopupWindow - the keyboard was never showing). This solved my issue:
final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
[....]
popUp.setFocusable(true);
popUp.update();
I didn't want to EditText lose a focus using editText.clearFocus(). Came up to this solution.
#Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT < 11) {
editText.clearFocus();
editText.requestFocus();
}
}
here's a possible solution:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable()) {
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
code is based on the next link:
http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/
In my case it was in a PopupWindow and I simply needed to call popupWindow.setFocusable(true)
I had this same problem when displaying an EditText in a DialogFragment. Despite the EditText getting focus (i.e., when clicked, it showed the flashing caret), the keyboard did not display.
My solution was to add a dummy EditText to the uppermost view of my DialogFragment.
<EditText
android:id="#+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/fix"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>
Possible scenarios:
1) On clicking the EditText, usually the keyboard comes up. But if you press the back key button in the emulator the keyboard (not the screen keyboard) dimisses.
2) In code you can disable the keyboard on clicking the EditText by setting a flag.
InputMethodManager inputmethodmgr= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputmethodmgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
It works like a charm, In a case if you even want to hide on click of the edittextView.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayKeyboard();
}
});
private void displayKeyboard(){
if (textView != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(textView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
In your parent view check if there is android:descendantFocusability="blocksDescendants"
remove it.

Spinner does not get focus

I'm using 4 EditText fields and 2 spinners in an activity. The order of these components are 2 EditText, then 2 spinners and then 2 EditText fields.
The problem occurs when I transfer focus (with the help of soft keyboard next button) from EditText to spinner, spinner does not get the focus and the focus is transferred to the next EditText field that was placed after the spinners.
I have used requestfocus() on spinner, but it did not work.
How do I make sure the spinner gets focus?
I managed to find a solution other then repositioning my Spinner. In the EditText before the spinner, add this listener:
editTextBefore.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
hideKeyboard();
textView.clearFocus();
spinner.requestFocus();
spinner.performClick();
}
return true;
}
});
You also need to add these line to able spinner to get focus:
spinner.setFocusable(true); // can be done in XML preferrable
My hideKeyboard function was just a visual detail that I wanted to add so the keyboard get hidden:
private void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
Hope I have helped in this tricky question.
The flag InputMethodManager.HIDE_NOT_ALWAYS can be found in the documentation.
Thanks, I have solved by doing the following:
I set the Spinner object on top (within the onCreate method) just to make sure that my code gets executed first
I used the following:
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
s1.setFocusable(true);
s1.setFocusableInTouchMode(true);
s1.requestFocus();
This is a shot in the dark, but try setting the focusable property (in XML or in code; whatever way you are doing it) to true on the spinner.
http://developer.android.com/reference/android/view/View.html#attr_android:focusable
EDIT: Also, see this question: Can't manage to requestFocus a Spinner
I just had the same problem. I solved it using the nextFocusDown/Up/Left/Right properties.
<EditText
android:id="#+id/tPhone"
...
android:focusableInTouchMode="true"
android:focusable="true"
android:nextFocusDown="#+id/sCountry"/>
<Spinner
android:id="#+id/sCountry"
....
android:focusableInTouchMode="true"
android:focusable="true"
android:nextFocusUp = "#+id/tPhone"
android:nextFocusDown="#+id/tStreet"/>
<EditText
android:id="#+id/tStreet"
...
android:visibility="gone"
android:focusableInTouchMode="true"
android:focusable="true"
android:nextFocusUp = "#+id/sCountry"/>
But why this is even neccessary... beats me.

Categories

Resources