Android: How to open keyboard for editing EditText when click button? - android

my case is: I have one EditText field that is with disabled focus.
Beside EditText field I has two buttons for Input methods. So I want when click first button: to open soft keybord and edit text in EditText field. I try many ways with:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
and doesn't work for me. Only way to open soft keyboard is:
toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
but there is no way to edit info from EditText field.
May you suggest me how to open keyboard and edit text for some EditText when click button.
Thanks a lot!
Edited:
So, EditText is not focusable be default. When I click Keyboard button - should be focusable, then show me soft keyboard to enter text and appear in EditText. Other method to insert is A-B-C button which not required keyboard. It will be something like Morse code input - touch and hold A-B-C button :) I'll try suggested example to implement in my case. Thank you guys :)

Thanks guys for your help :) I used all suggestions that you gave me, searched and tested a lot of other scripts and finally my code is working :)
Here is my final code:
InputEditText = (EditText) findViewById(R.id.InputText);
public void InputData() {
/* Keyboard Button Action */
KeyboardButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "On Keyboard Button click event!");
InputEditText.requestFocus();
InputEditText.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);
}
});
}
It may be useful for someone :)
Thank you!

The design you've described isn't recommended. You're violating the focusable attribute's purpose which is not to control whether the user can alter the text in a EditText component.
If you plan to provide an alternative input method because the use case seems to require this (e.g. you allow only a certain set of symbols in the editable text field) then you should probably disable text editing altogether for the time the user isn't allowed to change the value of the field.
Declare your editable field:
<EditText
android:id="#+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
notice that its focusable attribute is left with the default value. That's ok, we'll handle that later. Declare a button which will start editing process:
<Button
android:id="#+id/button_show_ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start editing" />
Now, in your Activity declare:
private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;
And in onCreate() do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ime_activity);
// Find out our editable field.
editText2 = (EditText)findViewById(R.id.edit_text_2);
// Save its key listener which makes it editable.
originalKeyListener = editText2.getKeyListener();
// Set it to null - this will make the field non-editable
editText2.setKeyListener(null);
// Find the button which will start editing process.
buttonShowIme = (Button)findViewById(R.id.button_show_ime);
// Attach an on-click listener.
buttonShowIme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Restore key listener - this will make the field editable again.
editText2.setKeyListener(originalKeyListener);
// Focus the field.
editText2.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
}
});
// We also want to disable editing when the user exits the field.
// This will make the button the only non-programmatic way of editing it.
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// If it loses focus...
if (!hasFocus) {
// Hide soft keyboard.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0);
// Make it non-editable again.
editText2.setKeyListener(null);
}
}
});
}
I hope the code with all the comments is self-explanatory. Tested on APIs 8 and 17.

Try this :
final EditText myedit2 = (EditText) findViewById(R.id.myEditText2);
Button btsmall = (Button) findViewById(R.id.BtSmall);
btsmall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
myedit2.requestFocus();
}
});

I Have Work this code for open a keybord when button click.
Like .
btn1 = (Button) findViewById(R.id.btn1);
edt1 = (EditText) findViewById(R.id.edt1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
edt1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt1, InputMethodManager.SHOW_IMPLICIT);
}
});
its completed work.

LinearLayout ll_about_me =(LinearLayout) view.findViewById(R.id.ll_about_me);
ll_about_me.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mEtEmpAboutYou.requestFocus();
mEtEmpAboutYou.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEtEmpAboutYou, InputMethodManager.SHOW_FORCED);
return true;
}
});

For those that uses fragments you can use InputMethodManager that way:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
Full Code:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}
});

I hope this help
EditText txt_categorie = findViewById(R.id.txt_categorie);
txt_categorie.requestFocus();

Related

EditText enable and disable on button click

I have a edit text view and button right side to it. When I click the button edittext has to get focus and keyboard has to appear. I programmitically
tried following.
name_text = (EditText)findViewById(R.id.name_text);
name_edit = (ImageView)findViewById(R.id.name_edit_icon);
name_text.setFocusable(false);
name_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name_text.setFocusable(true);
//name_text.setEnabled(true);
}
});
I tried all possiblities, none is worked for me. Please help me. Thanks.
Write this code inside the click event to TOGGLE the keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

How to hide the keyboard, and add text to EditText and move the cursor

I have an EditText and a Button. When you press EditText, I want to not show the keyboard, And when you press the Button, I want to type a number 1 on the EditText.
The observation I want to cursor does not disappear.
When you press the 1 1 writes
When you press the Del licked
Can be controlled in the text
Without the appearance of the keyboard
try these codes:
your_edit_text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
your_edit_text.setInputType(InputType.TYPE_NULL);
EditText.setText("1");
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
your_edit_text.setSelection(your_edit_text.getText().length()); // move cursor to end
}
#Karim Michel. You can use
For First Case .Hide Keyboard
ETOBJ.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
InputMethodManager inputManager = (InputMethodManager) context.
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
return false;
}
});
And when Press on Button you can use
setCursorVisible(false);
setText("1");

Android : show keyboard in edittext

Am stuck with a pretty simple issue in the Android App am coding. I have a list of EditText objects, one in each row.
When the user long presses the EditText, I need to show the keyboard. When the user does a long press, then I call this method :
private void setNameAsEditable(View rowView, boolean setToEditable) {
EditText textView = (EditText) rowView
.findViewById(R.id.edittext_name);
textView.setFocusableInTouchMode(setToEditable);
textView.setFocusable(setToEditable);
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
}
The edittext becomes editable (the underline and the cursor appears, as you can see below)
but the keyboard does not come up.
I tried various solutions from stackoverflow (like this), but in vain.
I even tried
this.getWindow().setSoftInputMode ( WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
I want the soft-keyboard to come up as soon as the user long-presses the EditText. Can someone please help?
Try it, for me it works fine.
et =(EditText) findViewById(R.id.edittext_name);
imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
et.setText("hide key board");
et.setFocusable(false);
et.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
et.setFocusableInTouchMode(true);
imm.showSoftInput(et, 0);
et.setText("show key board long pressed");
return false;
}
});
This is what worked finally ( Amit's answer + Tamilan's answer) Thanks so much!
private void setNameAsEditable (View rowView, boolean setToEditable) {
EditText textView = (EditText) rowView
.findViewById(R.id.edittext_name);
textView.setFocusableInTouchMode(setToEditable);
textView.setFocusable(setToEditable);
InputMethodManager imm = (InputMethodManager) getContext().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
if (setToEditable) {
textView.requestFocus();
imm.showSoftInput(textView, 0);
}
}
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
Check if you have android:focusable="false" or android:focusableInTouchMode="false" in your xml layout.

force soft keyboard to show when EditText gets focus [duplicate]

This question already has answers here:
How to show soft-keyboard when edittext is focused
(48 answers)
Closed 9 years ago.
I have an EditText that I am passing focus to programmatically. But when I do, I want the keyboard to show up as well (and then go down when that EditText lose focus). Right now, the user has to click on the EditText to get the keyboard to show up -- even thought the EditText already has focus.
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateVisible" />
Add this to manifest file...
This is how I show the ketyboard:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
set this for your activity in your manifest to pop keyboard automatically when your screen comes containing EditText box
<activity android:windowSoftInputMode="stateAlwaysVisible" ... />
To hide keyboard on losing focus set a OnFocusChangeListener for the EditText .
In the onCreate()
EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);
Add this class
private class MyFocusChangeListener implements OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus){
if(v.getId() == R.id.textbox && !hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
To show the keyboard, use the following code.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
To hide the keyboard,, use the code below.
et is the reference to the EditText
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
In order to do it based on focus listener you should go for:
final InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}else{
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
imm.toggleSoftInput(0, 0);
}
});
Hope this helps.
Regards!

How To Display the Soft Keyboard in my Application with the Galaxy Tab [duplicate]

I am trying to force the Soft Keyboard open in an Activity and grab everything that is entered as I want to handle the input myself, I don't have an EditText. Currently I have tried this but it does not work. I would like the Soft Keyboardto open below mAnswerTextView (Note: it is a TextView not EditText).
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
how do I force the Soft Keyboard open
How do I gab everything that is entered so that I can handle each character. I would like to flush each character from the Soft Keyboard after I have handled it. ie, the user should not be able to enter whole words in the Soft Keyboard.
try this to force open soft keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
then you can to use this code to close the keyboard:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
You'll probably need to have an editable text area of some kind to take focus. You can probably have one invisible or on a transparent background with no cursor, though. You may need to toy around with the focusability settings for the view.
Use a TextWatcher to check for edits to that EditText with addTextChangedListener, or if you need an even-lower-level hook, set the textview's key listener with its setOnKeyListener() method. See the KeyListener documentation.
Use this call to force the soft keyboard open:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
and this one to close it:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
note that this is really not recommended - forcing the keyboard open is kind of messy. What's your use case that really necessitates your taking user input without a normal edit box and requires eating user input on a key-by-key basis without echoing it back?
To force the keyboard to open I used
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
it worked for me.
Sometimes the other answers won't work.
Here is another way..
It will force the keyboard to show when the activity starts by listening to the window focus. onWindowFocusChanged() it will clear and request focus of the EditText, then set the soft input mode to visible and set the selection to the text in the box. This should always work if you are calling it from the activity.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mEditText.clearFocus();
mEditText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setSelection(mEditText.getText().toString().length());
}
}
You may also need
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
Edit: I have also seen the keyboard not open inside nested fragments, beware of those kinds of situations.
if you want to control soft keyboard inside activity then use this code:
//create soft keyboard object
InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);
//1.USE
your_view.setFocusableInTouchMode(true); //Enable touch soft keyboard to this view
//or
your_view.setFocusable(true); //Enable keyboard to this view
imm.showInputMethod(your_view, InputMethodManager.SHOW_IMPLICIT);
//2.USE show keyboard if is hidden or hide if it is shown
imm.toggleSoftInputFromWindow(your_view.getWindowToken(),InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//or
imm.toggleSoftInputFromWindow(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//3.USE (you cannot control imm)
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//4.USE (with Dialog)
Dialog d = new Dialog(this, android.R.style.Theme_Panel);
d.getWindow().setTitle(null);
d.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
d.setOnKeyListener(keyListener);
d.setCanceledOnTouchOutside(true);
d.setCancelable(true);
d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d.show();
//to hide keyboard call:
d.dismiss();
//if you want get soft keyboard visibility call:
d.isShowing();
Sadly, as much as I'd have liked to up-vote one of the replies, none worked for me. It seems the solution is to wait for the layout phase to complete. In the code below, notice how I check if the showKeyboard method returns TRUE, and that's when I remove the global layout listener. Without doing this, it was hit and miss. Now it seems to work perfectly.
You need to do the below ideally in onResume()
#Override
public void onResume()
{
super.onResume();
ViewTreeObserver vto = txtTaskTitle.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
if (txtTaskTitle.requestFocus())
{
if (showKeyboard(getContext(), txtTaskTitle))
{
txtTaskTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
});
}
public static boolean showKeyboard(Context context, EditText target)
{
if (context == null || target == null)
{
return false;
}
InputMethodManager imm = getInputMethodManager(context);
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
boolean didShowKeyboard = imm.showSoftInput(target, InputMethodManager.SHOW_FORCED);
if (!didShowKeyboard)
{
didShowKeyboard = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
}
return didShowKeyboard;
}
Working Great.........
edt_searchfilter_searchtext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
edt_searchfilter_searchtext.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getFragmentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchfilter_searchtext, InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
Below line call when you want to open keyboard
edt_searchfilter_searchtext.requestFocus();
if(search.getText().toString().trim().isEmpty()) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}
I have tested and this is working:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Simply, using adding 2 lines will work like a charm:
If using XML
android:focusable="true"
android:focusableInTouchMode="true"
Else in Java:
view.setFocusableInTouchMode(true);
view.requestFocus();
You can use this KeyboardHelper.java class
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* Created by khanhamza on 06-Mar-17.
*/
public class KeyboardHelper {
public static void hideSoftKeyboard(Context context, View view) {
if (context == null || view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static void hideSoftKeyboardForced(Context context, View view) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0);
}
public static void hideSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void showSoftKeyboard(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
editText.requestFocus();
}
public static void showSoftKeyboardForcefully(Context context, EditText editText) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
editText.requestFocus();
}
}

Categories

Resources