Soft keyboard auto show when use AsyncTask - java

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.

Related

How to hide keyboard onResume when returning from another app for sharing [duplicate]

This question already has answers here:
How to close/hide the Android soft keyboard programmatically?
(126 answers)
Closed 3 years ago.
I am sharing some text to other apps using android default sharing
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, sharingString);
shareIntent.setType("text/plain");
startActivity(shareIntent);
The Issue:
When I share to another app i.e (WhatsApp, slack, etc) open the keyboard in that app (i.e. to edit the message, etc.), and then send or return back to my application. in some cases the keyboard remains open in that app and when I try to close the keyboard on resume using the following code it is not working.
public static void hideKeyBoard(Activity activity, View view) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (view == null) {
view = new View(activity);
}
if (imm != null) {
if(view.getWindowToken()!=null)
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
if(view.getApplicationWindowToken()!=null)
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
view.clearFocus();
}
And calling the function from onResume() of the fragment
if(getActivity()!=null){
Utils.hideKeyBoard(getActivity(),getActivity().getCurrentFocus());
}
I have found many answers related to hiding the keyboard but any answer is not working in this case.
Note: in the normal case, when I use the hideKeyBoard() method, it is working perfectly. It is just this case that is not working. Can anyone help me?
Edit
As I've mentioned above. that why this quesiton is not like the prviosly answered ones is explained in the above note. So kindle Plesase read that. And I've also tried this link. but not working.
You can try below code that may help you.
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Here is some bonus
create activity instance instead use getActivity() methos on fragment. getActivity() returns null when fragment not visible
on your fragment host activity
public static MainActivity mainActivity;
public static MainActivity getInstance() {
return mainActivity;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = this;
}
inside onResume() call hideKeyboard(MainActivity.getInstance())
Also, don't forget to add android:windowSoftInputMode="stateAlwaysHidden" in your Manifest

Getting nothing while calling performClick() function - Android

I'm new to Android developing and now I'm trying to simulate click on my AutoCompleteTextView object.
I'm expecting default android's keyboard appearance with the possibility to type something at the element
Here is a simple function, where I'm trying to perform it:
private void someTestMethodName() {
AutoCompleteTextView tagSearchInput = findViewById(R.id.autoCompleteTextView);
tagSearchInput.performClick();
}
And here is .xml element defining:
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:text="TextView"
android:layout_width="188dp"
android:layout_height="62dp"
android:layout_alignParentStart="true"
android:layout_marginStart="108dp"
android:layout_alignParentTop="true"
android:layout_marginTop="292dp"/>
Calling performClick on a TextView does not pop up the soft keyboard, but you can quite easily do that yourself:
private void someTestMethodName() {
AutoCompleteTextView tagSearchInput = findViewById(R.id.autoCompleteTextView);
showSoftKeyboard(tagSearchInput);
}
public void showSoftKeyboard(View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
}
}
More information can be found here: https://github.com/codepath/android_guides/wiki/Working-with-the-Soft-Keyboard
i never used performClick, you cant use setOnClickListener to catch a click
tagSearchInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do somthing
}
});

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());
}
}

Android Show Soft Keyboard When First Activity Starts?

I need to display the virtual keyboard when the application starts, but so far I've failed.
I use this code in method "OnCreate"to display the virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(txtBuscar.getId(), InputMethodManager.SHOW_FORCED);
this code works fine on any screen at any time, but does not work when the "first" activity begins. Why?
I tried it when I start another activity and it works, but does not work when I start the "first"activity.
I tried to put this code in the events "OnCreate"and many more .... but it seems not work.
is there anyway to "force" to display the keyboard when I start the application?
Thanks in advance.
I Found the solution:
txtPassword.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(txtPassword, 0);
}
},200);
Thanks !!!
onCreate will not be called if the activity is first brought from background. Have you tried put that code in onResume?
onCreate is called only when activity is first start or the activity is killed and user navigate to the activity again. So if the activity is still alive but in background, it will not call onCreate.
On the other hand, onResume will be called every time the activity comes to foreground (visible on screen) from background.
Here is link to activity life cycle if you are interested http://developer.android.com/reference/android/app/Activity.html.
Hope it helps.
I faced with the same issue, this method below helped me
public static void showKeyboard(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}

Show soft keyboard for dialog

I am displaying a dialog with an edittext view. However, the softkeyboard will open only if the user presses inside the editview. So I tried calling an InputMethodManager with the following code.
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);
The dialogField is the input field. However, when exactly am I supposed to do this? I tried it in the onStart() method of the dialog, but nothing happens. I also tried requesting the focus for the dialogField before, but that changes nothing.
I also tried this code
dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange (View v, boolean hasFocus)
{
if (hasFocus)
{
Main.log("here");
dialogInput.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
/*
InputMethodManager mgr =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(dialogField,0);
*/
}
}
});
in both versions. But no soft keyboard would like to appear. The Main.log is just a log, which shows me that the function is actually called. And yes, it is called.
I could get the keyboard with the SHOW_FORCED flag before the dialog opens. But then it will not close on exit. And I can only do that BEFORE I show the dialog. Inside any callbacks it does not work either.
Awesome question, I was trying to do that too and found a solution.
Using the dialog builder class AlertDialog.Builder you will have to invoke the dialog like this:
AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;
builder.set...
dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
This worked fine for me.
Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
Window window = dialog.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Kotlin
Here's the tested code.
val dialog = AlertDialog.Builder(requireContext()).apply {
setTitle(…)
setView(editText)
setPositiveButton(…)
setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.
Import AlertDialog from androidx.appcompat.app.AlertDialog.
Import WindowManager from android.view.
Dialog Fragment With Kotlin
override onStart Method
override fun onStart() {
super.onStart()
dialog.window?.
setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
)
}
if you want to close after dismiss then override dismiss method with below code
override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
Here's my solution, it's working well for dialog.
txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Maybe also you need to add this to your activity tag in AndroidManifest.xml for closing the keyboard when the dialog is dismissed.
android:windowSoftInputMode="stateAlwaysHidden"

Categories