RecyclerAdapter not shown because of the keyboard - java

I am changing the dataset of the recyclerview adapter with different search query results. I am able to populate the data as well but the populated data only shows if the keyboard is closed.
This query("a") is valid but the result is shown only after I close the keyboard

Why not remove the keyboard as soon as the data is populated.
try {
InputMethodManager imm = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}

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

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

Call function from activity to close the soft keyboard android

I'm new to mobile automation and I've been trying to find a way to close the soft keyboard on Android (using Java). The best solution I've come across so far was from this post:
Close/hide the Android Soft Keyboard
The function that I'm trying to use from there is:
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);
}
I want to be able to call this function when the keyboard pops up, but what I don't understand is how to pass through the Activity when I call this function?
Before calling the function to close the keyboard I have the following:
// Create object of  DesiredCapabilities class and specify android platform
DesiredCapabilities capabilities = DesiredCapabilities.android();
// set the capability to execute test in android app
capabilities.setCapability(MobileCapabilityType.PLATFORM, Platform.ANDROID);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Emulator_1");
capabilities.setCapability(MobileCapabilityType.VERSION, "8.0");
capabilities.setCapability("appPackage", "com.spreeza.shop.stag.debug");
capabilities.setCapability("appActivity", "com.spreeza.shop.ui.features.splash.EntryPointActivity");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
// click on the login button
driver.findElement(By.id(identifierName)).click();
// close the keyboard
hideKeyboard();
Activity => activity mean a reference of a Activity object.
Call the method as below .
hideKeyboard(YourActivity.this);
Checkout your Activity name and replace YourActivity with the name .
You can call the function from Activity as hideKeyboard(MainActivity.this) and from Fragment as hideKeyboard((Activity)getActivity())
I managed to find a simple solution that works for me:
http://discuss.appium.io/t/can-we-hide-android-soft-keyboard/6956/4
I just added in these 2 capabilities:
capabilities.setCapability("unicodeKeyboard", true);
capabilities.setCapability("resetKeyboard", true);
/**
* Hide the keyboard
*/
private void hideKeyboard() {
View view = getCurrentFocus();
if (view != null) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}

How to disable android keyboard in android application [duplicate]

This question already has answers here:
How to close/hide the Android soft keyboard programmatically?
(126 answers)
Closed 8 years ago.
I am creating a calculator app where i have an edittext where the result is shown.I want the data to be entered in the edit text only on clicking the number buttons NOT by the android keyboard.How do i disable the keyboard in my android app.Please help!
Try
editText.setInputType(EditorInfo.TYPE_NULL)
Or
((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);
Or
editText.setKeyListener(null);
Try this :
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
try below code:-
EditText ed = (EditText)findViewById(R.id.editText1);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
ed.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);
}
});
or
Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);
If other situations you can prevent the keyboard showing simply using in the manifest
android:configChanges="orientation|keyboardHidden"
In this way the Android keyboard will not auto-open but is shown only if you click directly the EditText
Just posting an Answer what #yahya already commented.
You should consider using a TextView which is not editable and hence won't make any calls to the Keyboard.
In onClick() event of your button's call the setText(String) method and the apply the text you wish. :)
Ypu can also easily append things as setText(yourTextView.getText().toString + "Your New Text");
Hi Kindly try this: For this you no need any Widgets Context...
InputMethodManager miInputManager = (InputMethodManager) Activity.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
miInputManager.hideSoftInputFromWindow(Activity.this
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Use
android:windowSoftInputMode="stateHidden"
for <activity> in Android_Manifest.xml

Android:Hide keyboard after button click

I need to hide the android keyboard after a button click.
I have seen many examples of how to do this, however, they all appear to use a specific editText object.
e.g.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
My problem is that I am building the screen dynamically, thus there could be mane edit text fields. Is there a way the keyboard can be hidden without me having to specify which editText object I am hiding it for.
You could instead set it to your layout, ie:
LinearLayout mainLayout;
// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);
// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);
This is an example assuming that your layout will be created regardless of how many EditText objects (or other objects) are placed on it.
Edit: Also, something I find very useful is to make sure that the keyboard is hidden when an activity first launches (ie: if an EditText is the first thing focused). To do that, I put this in onCreate() method of Activity:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app will crash
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
You can hide the keyboard using the following code, probably on the Button click Event :
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
If the problem is on an activity simply the following will work:
try {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
else if the code is required in a fragment do the following
try {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
This will handle the hiding of keyboard on a button click or any other event as deemed specific when written within the event block.
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
For the record and based in the answers of #burmat and #Prashant Maheshwari Andro
Let's say that you call the click button as follow. where buttonAndroidLogin_button is the Button object.
protected void onCreate(Bundle savedInstanceState) {
// your code...
buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideKeyboard((Button)v);
// ....
} // end onCreate
On the activity, you must set the next method
public void hideKeyboard(View view) {
try {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch(Exception ignored) {
}
}
It hides the input using the same button, so we don't need any linear layout, text view or any other arbitrary control. Also, the code is reusable for more buttons.
You use this code
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
IN KOTLIN :
In your fragment :
Create extension like this :
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
Then use it like this :
hideKeyboard()
In your activity :
Create extension like this :
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
Then use it like this :
hideKeyboard()
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);

Categories