Keep EditText focus when tapping on buttons - android

I have an EditText with three toggle buttons beneath it.
I want to keep the focus on the EditText AND have the keyboard stay visible when I tap on any of the three toggles. i.e. I do not want the keyboard to hide when the focus is outside the EditText (I should not see the keyboard hide then reopen).
I've tried the following to no avail:
toggleButton.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
editText.requestFocus();
// This doesn't fully work.
// Focus is on editText but keyboard still hides when I
// tap on the toggle button.
}
});
The EditText and ToggleButtons are in a fragment, and the parent activity has this configuration in the AndroidManifest.
<activity
android:name=".activities.MyActivity"
android:label="#string/m_activity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
What is the best way to fix this issue?

I think you should do that for your yourEditText, using OnFocusChangeListener
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
}
});
This means that, you will request focus whenever it is changed for you yourEditText and you will also show keyboard.

You can use the LayoutParms.Example is given below
1.Hide the keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
2.Show the Keyboard
this.getWindow().setSoftInputMode
(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
If you want to Know the other options. Refer the below link.
https://developer.android.com/reference/android/view/WindowManager.LayoutParams.
html

Related

TextEdit OnFocusChangeListener

I am trying to get the keyboard to come up when an editText view gets or has focus. I am getting the error getOnFocusChangeListener in view cannot be applied to anonymous android.view.View.OnFocusChangeListener
The error starts on new View.OnFocusChangeListener() and goes through the whole class. I can't figure out why or how to get this working.
Here is my code:
final EditText measurement = (EditText)dialog.findViewById(R.id.measurement);
measurement.getOnFocusChangeListener(new View.OnFocusChangeListener(){
#Override
public void onFocusChange(View v, boolean hasFocus){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(hasFocus){
imm.showSoftInput(measurement, InputMethodManager.SHOW_IMPLICIT);
}else{
imm.showSoftInput(measurement, InputMethodManager.HIDE);
}
}
});
Please help me fill in the gaps in my knowledge about why this isn't working
instead of
measurement.getOnFocusChangeListener
// you have to use
measurement.setOnFocusChangeListener
also you dont need to set a listener for edit text. whenever a edit text is clicked the softkeyboard will show up by itself, unless you are modifying certain behavior.

EditText Loses Focus Second Time

I have a Edit Text , and after user click on button or on soft keyboard input , i do some action in background and restart this activity again . At first time the focus is on EditText , but after redirecting the focus is lost .
One more thing , I am using a bluetooth device to enter text in the EditText area .
In the starting of activity , i am getting the editText button and calling
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
also after calling action , i am again requesting focus .
In manifest i am using :
android:windowSoftInputMode="stateAlwaysVisible"
I have also tried to show keyboard always :
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mLastNameET, InputMethodManager.SHOW_IMPLICIT);
I have tried many things , please suggest how can i make it work ?
There are two ways to do this,
First is to declare EditText.request focus() in on resume(), or second one is declare <request focus /> tag between <EditText> <request focus/> <\EditText>
You can also use this thing:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
I had similar problem and it is fixed with adding android:windowSoftInputMode="adjustPan" in Manifest like below.
I hope it work for you too.
<activity
android:name=".MyActivity"
android:windowSoftInputMode="adjustPan">
</activity>

Android softkeyboard not triggerring

I have multiple EditText on my screen and one of them is focussed. The softkeyboard does not trigger as soon as the screen pops up. I want the soft keyboard to trigger as soon as the screen pops up. It works well if I don't implement the onFocusChangeListener(). However I need the onFocusChangeListener() to detect which editText is focused. I have tried setting setFocusable(true) and setFocusableInTouchMode(true). Also i don't want to modify the android:windowSoftInputMode property in AndroidMenifest.xml. I have the following criteria :
onFocusChangeListener implemented (to detect which edittext is focused)
No modifications in AndroidMenifest.xml
Here is my code snippet
final InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
mInput.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
mIsFocused = hasFocus;
if(hasFocus)
inputMethodManager.showSoftInput(mInput, InputMethodManager.SHOW_IMPLICIT);
}
});
Any suggestions ?
You can open softkeyboard programmatically
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
You can try using inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0).
Source: Stackoverflow post

Android EditTExt query

I stuck with one issue in my current application. I have one editText and i have set it Enables = false in XML file. I have to buttons also named as EDIT and DONE.
Problem.
When application launches it works fine. EditText is not having focus and when i click on editext keyboard appears and i can edit text in edit text.
if(v==Edit)
{
//PurchaseAddressdata.setFocusable(true);
Edit.setVisibility(Button.INVISIBLE);
done.setVisibility(Button.VISIBLE);
PurchaseAddressdata.setClickable(true);
PurchaseAddressdata.setEnabled(true);
PurchaseAddressdata.setCursorVisible(true);
PurchaseAddressdata.setSelection(PurchaseAddressdata.getText().length());
}
if(v==done)
{
Edit.setVisibility(Button.VISIBLE);
done.setVisibility(Button.INVISIBLE);
PurchaseAddressdata.setCursorVisible(false);
PurchaseAddressdata.setClickable(false);
}
But the problem is when i click on editext keyboard is appearing. I want when i press Edit button then only keyboard will appear.
Thanks
Set the onFocusChangeListener listener of the edit text field to this so that it hides the keyboard when focused.
boolean display_keyboard = false;
edittext.setOnFocusChangeListener(new OnFocusChangeListener()
{
public void onFocusChange(View view, boolean arg1)
{
if (!display_keyboard)
{
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
Then when the Edit button is clicked, set displaykeyboard = true; and move the focus back to the edittext
try this code it works for me.
editText.setInputType(0);
and in onClick()
editText.setInputtype(InputType.TYPE_CLASS_TEXT);

Hide Keyboard Password Field Android

I have 2 editfields in my Custom Dialog which is called from ACtivity, among them 1 is of "trxtPassword" and other of "text" type. Keyboard doesn't appear in "text" type editbox, but just comes on "textPassword" edittext, and then doesn't go only.
I tried the following, but nothing works:
InputMethodManager inputManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//inputManager.hideSoftInputFromWindow(txt_username.getWindowToken(), 0);
//inputManager.hideSoftInputFromWindow(txt_password.getWindowToken(), 0);
If I make txt_password.setInputType(0); then others can see the password easily, so that can't be used.
What more can be doen to achieve the goal? I did to trap the onLostFocus on txt
txt_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) {
InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(LoginDialog.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
});
But unfortunately, once entered, if I click anywhere else or any checkbox, then also the focus is not lost from the txt_password field. That is only lost if I click another editText and then the onFocusChange event is fired and throws error and the application shuts down.
Any idea how to accomplish this?
Use that to keep keyboard hidden on activity start
<activity
android:name=".views.DrugstoreEditView"
android:windowSoftInputMode="stateHidden"></activity>
And there is one usefull answer: How to hide soft keyboard on android after clicking outside EditText?

Categories

Resources