EditText Keyboard disable but the touch focus has to there - android

I am designing my own keyboard, so i dont want the default keyboard to appear.
I am able to achieve this, but the focus on selection is missing.
How can i have the focus but disable the Keyboard alone on EditText in Android?

add this listener for edittext:
edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
hideSoftKeyboard();
}
}
});
and add this method to activity:
public static void hideSoftKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}

Related

Hide Numeric Keyboard ONLY WHEN clicked outside any of the Edit Text

I have three different Edit Text with restriction of entering only numeric values, thus tapping on any one of the Edit Text opens NUMERIC KEYBOARD.
I tried to implement setOnFocusChangeListener for all Edit Text which ensures that when user tap anywhere outside any of the Edit Text hide the keyboard.
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
editText3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
And here is the implementation of 'hideKeyBoard'
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
This works fine, however, when I change focus from one EditText to another EditText, keyboard hides. This behaviour might be frustrating for user.
How can I deal with it and avoid unnecessary hiding of keyboard.
P.S I need to ensure that keyboard is numeric not alphabetical in any case.
Thank you MarianoZorrila for correcting me on my approach to the problem.Here are the changes I performed to solve this problem.
Gave an Id to my Relative Layout
<RelativeLayout
...
...
android:id="#+id/parent">
Modifications to Java File
View parent = findViewById(R.id.parent);
parent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
hideKeyboard(view);
}
}
});

Android´s keyboard doesn´t appear

I have a problem with android´s keyboard. I have a fragment who has an EditText in the top of the screen and below has a nested fragment that is a QR scanner. When I click on the EditText to write something the keyboard doesn´t appear if I don´t do a long click to my device´s menu button. I am trying to show programatically when I focus the EditText but I´m not able. Here is my code, thank you in advance.
EditText caja_edicion =(EditText) getActivity().findViewById(R.id.textcodigo);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(caja_edicion, InputMethodManager.SHOW_IMPLICIT);
try adding this:
caja_edicion.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(caja_edicion, InputMethodManager.SHOW_FORCED);
} else {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(caja_edicion.getWindowToken(), 0);
}
}
});
I have change to this because I am in fragment but it doesn´t work and I don´t know why:
final EditText caja_edicion =(EditText) getActivity().findViewById(R.id.textcodigo);
caja_edicion.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(caja_edicion, InputMethodManager.SHOW_FORCED);
} else {
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(caja_edicion.getWindowToken(), 0);
}
}
});

Dismiss keyboard when EditText loses focus

I have an EditText that I want to control the keyboard. When the EditText has focus the keyboard should appear, then as soon as I click on any other view, I want the keyboard to disappear. I try the following code but it did work
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
}
});
Assuming your outermost layout is RelativeLayout(You can do the similar thing for others as well), you can do something like following:
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
layout = (RelativeLayout) findViewById(R.id.yourOutermostLayout);
onTapOutsideBehaviour(layout);
}
private void onTapOutsideBehaviour(View view) {
if(!(view instanceof EditText) || !(view instanceof Button)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(YourCurrentActivity.this);
return false;
}
});
}
}
\\Function to hide keyboard
private static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
In onTapOutsideBehaviour function here, other that your EditText and Button views, if user clicks anywhere else, it will hide the keyboard. If you have any complex custom layout, you can exclude other views on which if user clicks, it does not hide the keyboard.
It worked for me. Hope it helps you.

Knowing when Edit text is done being edited

How do I know when my edit text is done being edited? Like when the user selects the next box, or presses the done button on the soft keyboard.
I want to know this so I can clamp the input. It looks like text watcher's afterTextChanged happens after each character is entered. I need to do some calculations with the input, so I would like to avoid doing the calculation after each character is entered.
thanks
By using something like this
meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
switch (actionId){
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_NEXT:
case EditorInfo.IME_ACTION_PREVIOUS:
yourcalc();
return true;
}
return false;
}
});
EditText inherits setOnFocusChangeListener which takes an implementation of OnFocusChangeListener.
Implement onFocusChange and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.
EDIT
To handle both cases - edit text losing focus OR user clicks "done" button - create a single method that gets called from both listeners.
private void calculate() { ... }
btnDone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
calculate();
}
});
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
calculate();
}
});
Using an EditText object xml defined like this:
<EditText
android:id="#+id/create_survey_newquestion_editText_minvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="4"
android:imeOptions="actionDone"
android:inputType="number" />
We can capture its text i) when the user clicks the Done button on the soft keyboard (OnEditorActionListener) or ii) when the EditText has lost the user focus (OnFocusChangeListener) which now is on another EditText:
/**
* 3. Set the min value EditText listener
*/
editText= (EditText) this.viewGroup.findViewById(R.id.create_survey_newquestion_editText_minvalue);
editText.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
String input;
if(actionId == EditorInfo.IME_ACTION_DONE)
{
input= v.getText().toString();
MyActivity.calculate(input);
return true; // consume.
}
return false; // pass on to other listeners.
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
String input;
EditText editText;
if(!hasFocus)
{
editText= (EditText) v;
input= editText.getText().toString();
MyActivity.calculate(input);
}
}
});
This works for me. You can hide the soft keyboard after made the calculations using a code like this:
private void hideKeyboard(EditText editText)
{
InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
Edit: added return values to onEditorAction
I have done this simple thing, when focus is shifted from edit text get the text. This will work if user shifts the focus to select other views like a button or other EditText or any view.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
EditText editText = (EditText) v;
String text = editText.getText().toString();
}
}
});
To be more highlevel you may use TextWatcher's afterTextChanged() method.

Android: Dialog box show soft keyboard automatically when focus is on an EditText not working

Android: show soft keyboard automatically when focus is on an EditText
I've read this post that automatically shows the virtual keyboard when a dialog box is shown. however, it's not working for me. any ideas why? eventhough the edit text is automatically on focus when the dialogbox appear, the event doesn't trigger. I've also read the onpostresume answer but I don't know how to apply it. any help is appreciated.
final Dialog dialog = new Dialog(ThesisI.this);
dialog.setContentView(R.layout.budget_dialog);
final EditText et = (EditText) dialog.findViewById(R.id.textComments);
final Button enter = (Button) dialog.findViewById(R.id.buttonEnter);
final Button cancel = (Button) dialog.findViewById(R.id.buttonCancel);
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
/**cancel */
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.show();
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
however, i noticed that if I change focus to the button then focus again to the edit text. this event works, using this code below.
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
}
}
});
any idea on how to apply it?
What you can do is try using postDelayed(Runnable) for EditText as below,
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);
Just try adding the below line before "et.setOnFocusChangeListener"
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(et, 2);

Categories

Resources