Lock listview scroll on an event - java

How do I freeze the listview scroll when keyboard pops up and then regain scroll after keyboard is hidden.
I cannot seem to start anywhere

In On ScrollState Change check keyboard is open or not:
public void onScrollStateChanged(AbsListView view, int scrollState) {
//check keyboard is open?
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
writeToLog("Software Keyboard was shown");
} else {
writeToLog("Software Keyboard was not shown");
}
}
and stop listview with
listView.setScrollContainer(false);

Related

Problem hiding keyboard when scrolling to an EditText field

I have a ScrollView that contains several EditText and AutoCompleteTextView views. I'd like to hide the keyboard if the user scrolls or taps out of the text box.
So I added this to my Fragments onViewCreated()
myScrollView?.viewTreeObserver?.addOnScrollChangedListener {
hideKeyboard()
}
The problem is, when I start scrolling the keyboard hides, BUT when I click an EditText or AutoCompleteTextView, the view starts to scroll to that field to show the keyboard, but because, the scrollview has the addOnScrollChangedListener it hides the keyboard. And the view just jumps back and forth with hiding and showing the keyboard.
What is the correct way to handle this scenario?
The hideKeyboard() comes from an extension class
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
fun Fragment.hideKeyboard() {
view?.let {
activity?.hideKeyboard(it)
}
}

How can I force the keyboard to appear?

When the user clicks on an editText the keyboard is brought up for the user to type. Can I make the keyboard to appear when a user clicks on a button instead of the editText? Can the numpad appear instead of the normal keyboard?
Can i make the keyboard to appear when a user clicks on a button
instead of the editText?
Yes, you need to set the focus and pop up the keyboard using InputMethodManager
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Focus the field.
editText.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
Can the numpad appear instead of the normal keyboard?
Yes, using input type
Either in the xml tag of edittext
<EditText...
android:inputType="number"/>
or in java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
public static void toggleKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
} catch (Exception e) {
Log.e("On show keyboard error: %s", e.getMessage());
}
}

Soft keyboard auto show when use AsyncTask

I don't know why when I use AsyncTask to load data to an Adapter, after AsyncTask finishes, the soft keyboard shows on screen.
There is a Edittext in the activity. If I delete the EditText on the Activity, after the AsyncTask finishes, the soft keyboard does not show.
I use this code on onPostExecute() but it does not work:
protected void onPostExecute(String[] result) {
hideSoftKeyboard(SearchActivity.this);
RecommendAdapter mAdap = new RecommendAdapter(con, mLocation);
mListview.setAdapter(mAdap);
if (dialog.isShowing()) {
dialog.dismiss();
}
}
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
Now I know the answer.It because I use so it always focus on .Add the android:focusable="true"
android:focusableInTouchMode="true" to the Layout so the soft keyboard not show.

Show Soft Keyboard when EditText Appears

I have this code in my android app, for an alert dialog and keyboard, and when the dialog is shown I want the keyboard to appear. But its not and I am not sure why?
Here is my code:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.i(TAG, "in focus");
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
}
}
});
But the keyboard does not appear even though the edit text is in focus, and I tell it to appear why, doesn't it come up?
Thanks for the help in advance.
I tested your code and the keyboard opened when alert.show() was executed. If you are using an emulator be sure that you have the virtual keyboard enabled. Otherwise a keyboard will never appear on the device screen.

How to make the virtual keyboard go away?

I have an EditBox in my AlertDialog popup. I made it to popup the virtual keyboard the moment the AlertDialog pops up (so that I don't need to click on that white field to show the keyboard), this way:
InputMethodManager imm = (InputMethodManager)
Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
But when I'm done typing I need to click "Done" on the keyboard and then OK in AlertDialog. The problem is, that users go right to the OK button once they're done typing and after clicking OK, the virtual keyboard stays onscreen. They now have to click back button on their device. How to clear the keyboard once the OK button is pressed?
Here's my whole AlertDialog code, if it helps:
case R.id.bKonacno:
}
LayoutInflater layoutInflaterK = LayoutInflater.from(context);
View promptViewK = layoutInflaterK.inflate(R.layout.popup_answer, null);
AlertDialog.Builder alertDialogBuilderK = new AlertDialog.Builder(context);
// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilderK.setView(promptViewK);
final EditText inputK = (EditText)promptViewK.findViewById(R.id.userInput);
InputMethodManager imm = (InputMethodManager)
Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
alertDialogBuilderK
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
//some code of mine
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertK = alertDialogBuilderK.create();
alertK.show();
break;
You can do the following when Ok/Cancel button is pressed.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Read Close/hide the Android Soft Keyboard for more. Hope it helps.
Try adding the following to your buttons:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Categories