I have-
EditText &
Button
How can I open a soft keyboard when one selects the edittext view, the user can write anything and it should display in the edittext.
I used the below code but the keyboard doesn't come up-
EditText edit= (EditText) findViewById(R.id.list);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
Any idea how I can open the keyboard and close it again by using back button?
This is my function to open the keyboard :
protected void showKeyBoard(final View v){
v.post(new Runnable() {
public void run() {
v.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
});
}
Make sure
android:windowSoftInputMode="stateAlwaysVisible" in manifest File.
and
edittext.requestFocus();
Related
I want to implement a tablet in Android app that keyboard auto on the screen. Now my situation is I add a edit-text in a xml file, and when clicking the edit-text, keyboard appears, I want to show keyboard auto on.
When users click on the check mark, the keyboard disappears, but I want that keyboard will not be dismissed and clicking on check mark means checking the input.
Any samples or help is appreciated!
Use below method with activity
//To show the keyboard
public void showKeyboard(Activity activity) {
if(activity.getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
//To hide the keyboard
public void hideKeyboard(Activity activity) {
if(activity.getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
}
OR
Use below method with EditText
//To show the keyboard
public void showSoftKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
//To hide the keyboard
`public void hideSoftKeyboard(EditText editText) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
Use above method as per your requirement
This will work and also always try to use etText.requestfocus(); it will automatically open a keyboard
and
public static void ShowKeyboard(Activity activity, View v)
{
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
view.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
Check with this:
Add this on your checkbox click
To Show Soft Keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EDITABLE_VIEW,InputMethodManager.SHOW_IMPLICIT);
OR
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
To Hide Soft Keyboard:
InputMethodManager imm = InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(FOCUSABLE_VIEW.getWindowToken(), 0);
Here, “FOCUSABLE_VIEW” can be any view which is visible on screen like
Refer:http://chintanrathod.com/show-hide-soft-keyboard-programmatically-in-android/
Below are the methods to show and hide keyboard. Have a look.
public static void ShowKeyboard(Activity activity, View view) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
view.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
public static void HideKeyBoard(Activity activity) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (inputMethodManager.isAcceptingText())
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
If you want to open keyboard on activity start, you can add following code in <activity> tag in manifest:
android:windowSoftInputMode="stateVisible"
this will show the keyboard whenever you start activity.
You can use the following code,
editext.requestfocus();
this will automatically brings the keyboard.
Do not use,
android:windowSoftInputMode="stateHidden"
inside your android manifest file...
just remove "android:windowSoftInputMode" from your Activity.
I have a problem and i not find answer in google.
I open a keyboard with button:
private void openKeyboard() {
keyboardButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 3);
}
});
}
But my layout going crap after keyboard is visible. How to open it with lost any parent from parent?
try with this snippet..
InputMethodManager inputMethodManager =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
linearLayout.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
or try this
In your manifest file, try adding the following to the <activity> that you want to show the keyboard when the activity starts:
android:windowSoftInputMode="stateVisible"or "stateAlwaysVisible"
or try this too..
youredittext.requestFocus();
youredittext.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(youredittext, InputMethodManager.SHOW_FORCED);
I have a meme creator application, I have two text fields and a button, I want when the button is pressed to hide the keyboard, is this possible?
public void dismissKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (null != activity.getCurrentFocus())
imm.hideSoftInputFromWindow(activity.getCurrentFocus()
.getApplicationWindowToken(), 0);
}
Activity have to be passed to this method, Keyboard will get dismissed.
You can hide sof keyboard with this lines
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Put this in the onClick(View view) event.
You need to import android.view.inputmethod.InputMethodManager;
The keyboard will hides when you click the button.
What you want should already be happening. When you click the button, the focus changes from the text field to the button, so the keyboard will automatically hide.
EditText editText = (EditText)findViewById(R.id.textBox);
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
this should be in button click event
Android Kotlin
On button click hide the keyboard in kotlin
fun dismissKeyboard(activity: Activity) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (null != activity.currentFocus) imm.hideSoftInputFromWindow(
activity.currentFocus!!.applicationWindowToken, 0
)
}
And use in your class like this
button.setOnClickListener {
dismissKeyboard(this)
}
I am creating a Fragment inside an Activity.I have an EditText in the MainActivity layout and the Fragment layout comes under that EditText.My issue is when I click the EditText the soft keyboard is not showing.
In EditText layout I create
<EditText
android:id="#+id/edt_searchContact"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textCursorDrawable="#drawable/color_cursor"
android:background="#drawable/edit_text_line_contacts"
android:focusable="true"
android:clickable="true"
android:hint="Search..."/>
<requestFocus/>
for showing the soft keyboard the code that I given in the Fragment
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchContact, InputMethodManager.SHOW_IMPLICIT);
In Manifest
android:windowSoftInputMode="adjustPan"
I have 3 Fragment in one Activity,when one Fragment comes that EditText is going to Visible in main Activity layout.
Can anyone please help me
Thanks in advance :)
Try this in your activity
editText = (EditText)findViewById(R.id.txt_yourName);
// Request focus and show soft keyboard automatically
editText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
On your Activity's onResume() Method you can write this:
EditText et = (EditText) findViewById(R.id.et);
et.requestFocus();
InputMethodManager mngr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mngr.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
Try this one :
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(edt_searchContact, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
The below method worked for me-
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
I have an Activity with an EditText, a button and a ListView. The purpose is to type a search screen in the EditText, press the button and have the search results populate this list.
This is all working perfectly, but the virtual keyboard is behaving strange.
If I click the EditText, I get the virtual keyboard. If I click the "Done" button on the virtual keyboard, it goes away. However, if I click my search button before clicking "Done" on the virtual keyboard, the virtual keyboard stays and I can't get rid of it. Clicking the "Done" button does not close the keyboard. It changes the "Done" button from "Done" to an arrow and remains visible.
Thanks for your help
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
I put this right after the onClick(View v) event.
You need to import android.view.inputmethod.InputMethodManager;
The keyboard hides when you click the button.
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
return true;
}
return false;
}
});
Use Below Code
your_button_id.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
}
});
You should implement OnEditorActionListener for your EditView
public void performClickOnDone(EditView editView, final View button){
textView.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
hideKeyboard();
button.requestFocus();
button.performClick();
return true;
}
});
And you hide keyboard by:
public void hideKeybord(View view) {
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
You should also fire keyboard hiding in your button using onClickListener
Now clicking 'Done' on virtual keyboard and button will do the same - hide keyboard and perform click action.
For Activity,
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
For Fragments, use getActivity()
getActivity().getSystemService();
getActivity().getCurrentFocus();
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
In your case, as you only have one EditText, the bellow solution is the best one, I believe. If you had more than one EditText component, then either you would need to focus on the EditText that you wanted to close, or call the bellow function for every EditText. For you though, this works brilliantly:
editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
Basically, the EditText already has a function that is intended to deal with what to do after the keyboard has been used. We just pass that we want to close the keyboard.
Add the following code inside your button click event:
InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
This solution works perfect for me:
private void showKeyboard(EditText editText) {
editText.requestFocus();
editText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
editText.setSelection(editText.getText().length());
}
private void closeKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
Try this...
For Showing keyboard
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
For Hide keyboard
InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
Kotlin example:
val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
from Fragment:
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
from Activity:
inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
You use this code in your button click event
// 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);
}
Crash Null Point Exception Fix:
I had a case where the keyboard might not open when the user clicks the button. You have to write an if statement to check that getCurrentFocus() isn't a null:
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(getCurrentFocus() != null) {
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
If you set android:singleLine="true", automatically the button hides the keyboard¡