how to block virtual keyboard while clicking on edittext in android? - android

How to block virtual keyboard while clicking on edittext in android

Here is a website that will give you what you need.
As a summary, it provides links to InputMethodManager and View from Android Developers. It will reference to the getWindowToken inside of View and hideSoftInputFromWindow() for InputMethodManager.
A better answer is given in the link, hope this helps.
EDIT
From the link posted above, here is an example to consume the onTouch event:
editText.setOnTouchListener(otl);
private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:
myEditor.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int inType = myEditor.getInputType(); // backup the input type
myEditor.setInputType(InputType.TYPE_NULL); // disable soft input
myEditor.onTouchEvent(event); // call native handler
myEditor.setInputType(inType); // restore input type
return true; // consume touch event
}
});
Hope this points you in the right direction!

A simpler way, is to set focusable property of EditText to false.
In your xml layout:
<EditText
...
android:focusable="false" />

Another simpler way is adding android:focusableInTouchMode="false" line to your EditText's xml. Hope this helps.

For cursor positioning you can use Selection.setSelection(...), i just tried this and it worked:
final EditText editText = (EditText) findViewById(R.id.edittext);
editText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//change the text here
Selection.setSelection(editText.getText(), editText.length());
return true;
}
});

The best way to do this is by setting the flag textIsSelectable in EditText to true. This will hide the SoftKeyboard permanently for the EditText but also will provide the added bonus of retaining the cursor and you'll be able to select/copy/cut/paste.
You can set it in your xml layout like this:
<EditText
android:textIsSelectable="true"
...
/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
For anyone using API 10 and below, hack is provided here : https://stackoverflow.com/a/20173020/7550472

Related

android EditText imeOption OnClick

With a Button it is simple,
<Button
android:blablabla="blabla"
...
android:onClick="doSomething" />
this will preform the doSomething(View) function.
How can we mimic this with an EditText ?
I have read about this and i read that most people use an imeOptions (which still seems necessary) and then implement a actionListener on that EditText object.
This is were i'm lost.
Is there a way to implement the "Done"-action (or send or...) from our keyboard to a onClick function like we do with a Button, or do we need to explicitly implement the listener ?
Regards !
The below code will perform some action when you press the Done key in the softkeyboard.
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//do your actions here that you like to perform when done is pressed
//Its advised to check for empty edit text and other related
//conditions before preforming required actions
}
return false;
}
});
Hope it helps !!
I am assuming what you are wanting to do is run some code when the EditText is clicked?
If so, I have found a solution from another thread on the site:
EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
then do this code here
}
}
});
via: A better way to OnClick for EditText fields?

Android Hide Soft Keyboard from EditText while not losing cursor

I've come about as far as this which gets me halfway there, but not quite.
I have a dialer Fragment that has all the usual Buttons to enter a number including backspace, so I don't need the soft keyboard. I'd also like to give the user the ability to paste text (long click... works fine per default), as well as to edit what has been entered so I need the cursor.
The easiest way I found to make sure the soft keyboard doesn't pop up if the user clicks inside the EditText is to set the inputType to null - but that kills the cursor as well.
So, how do I declare my EditText and what kind of commands should I launch to have my EditText field never ever show the soft keyboard no matter what the user attempts, but still retain paste functionality and the cursor?
I've also tried android:windowSoftInputMode="stateAlwaysHidden" in my manifest, but to no avail.
This worked for me:
// Update the EditText so it won't popup Android's own keyboard, since I have my own.
EditText editText = (EditText)findViewById(R.id.edit_mine);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
I have finally found a (for me) working solution to this.
First part (in onCreate):
// Set to TYPE_NULL on all Android API versions
mText.setInputType(InputType.TYPE_NULL);
// for later than GB only
if (android.os.Build.VERSION.SDK_INT >= 11) {
// this fakes the TextView (which actually handles cursor drawing)
// into drawing the cursor even though you've disabled soft input
// with TYPE_NULL
mText.setRawInputType(InputType.TYPE_CLASS_TEXT);
}
In addition, android:textIsSelectable needs to be set to true (or set in onCreate) and the EditText must not be focused on initialization. If your EditText is the first focusable View (which it was in my case), you can work around this by putting this just above it:
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" >
<requestFocus />
</LinearLayout>
You can see the results of this in the Grapher application, free and available in Google Play.
Setting the flag textIsSelectable to true disables the soft keyboard.
You can set it in your xml layout like this:
<EditText
android:id="#+id/editText"
...
android:textIsSelectable="true"/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.
Best solution from #Lupsaa here:
Setting the flag textIsSelectable to true disables the soft keyboard.
You can set it in your xml layout like this:
<EditText
android:id="#+id/editText"
...
android:textIsSelectable="true"/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.
If your min SDK is 21, you can this method from java code:
editText.setShowSoftInputOnFocus(false);
Credits to Chen Su article.
use
android:windowSoftInputMode="stateHidden"
in your manifest file instead of android:windowSoftInputMode="stateAlwaysHidden"
This is what I did.
First, in manifest inside activity
android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
Second, in onCreate if inside activity or onActivityCreated if inside fragment
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideSoftKeyboard(v);
}
});
Do not forget to request focus to the editText
editText.requestFocus();
Then add the hideSoftKeyboard(v) method same as the other answer.
private void hideSoftKeyboard(View v){
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
The key here is to requestFocus before clicking the EditText. If without focus, first click will make the keyboard show up(my experience). However, this is applied if you have a single EditText in an activity. With this, you still can type with custom keyboard(if any), can copy and paste, and cursor is still visible.
The exact functionality that you require is provided by setting the flag textIsSelectable in EditText to true. With this, the cursor will still be present, and you'll be able to select/copy/cut/paste, but SoftKeyboard will never show. Requires API 11 and above.
You can set it in your xml layout like this:
<EditText
android:textIsSelectable="true"
...
/>
Or programmatically, like this:
EditText editText = (EditText) findViewById(R.id.editText);
editText.setTextIsSelectable(true);
For anyone using API 10 and below, hack is provided here :
https://stackoverflow.com/a/20173020/7550472
This works perfectly (for me) in 2 steps:
<activity... android:windowSoftInputMode="stateHidden"> in manifest file
Add these properties in your editText XML code
android:focusable="true"
android:focusableInTouchMode="true
You have to put both 1 and 2, only then it will work.
Cheers
EditText text = (EditText) findViewById(R.id.text);
if (Build.VERSION.SDK_INT >= 11) {
text.setRawInputType(InputType.TYPE_CLASS_TEXT);
text.setTextIsSelectable(true);
} else {
text.setRawInputType(InputType.TYPE_NULL);
text.setFocusable(true);
}
First add android:windowSoftInputMode="stateHidden" in your manifest file, under the activity. like this
<activity... android:windowSoftInputMode="stateHidden">
The on your xml add this android:textIsSelectable="true" . This will make the pointer visible.
Then on onCreate method of the activity, add this:
EditText editText = (EditText)findViewById(R.id.edit_text);
edit_text.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager inputMethod = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethod!= null) {
inputMethod.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
i found this very useful code and it work as charm, it head the Keyboard totaly, but keeping cursor and you can copy past, move the cursor...ect
using :
hideSoftKeyboard(editText);
methode :
public void hideSoftKeyboard(EditText edit) {
if (android.os.Build.VERSION.SDK_INT <= 10) {
edit.setInputType(InputType.TYPE_NULL);
} else {
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
try {
Class<EditText> cls = EditText.class;
Method setSoftInputShownOnFocus;
setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
setSoftInputShownOnFocus.setAccessible(true);
setSoftInputShownOnFocus.invoke(edit, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
EditText editText = (EditText)findViewById(R.id.edit_mine);
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
ha... this is the correct way of doing...this job done... this gonna work !
You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this
Works Perfectly for me!! Try this.. :)`
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

OnClick event only works second time on edittext

I have an edittext, and when the user clicks this edittext I want to show an alertdialog.
My code is the following :
edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(true);
edt.requestFocus();
edt.setCursorVisible(false);
edt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.
But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.
How do I fix this?
Simply try to add this to your XML file. Your keyboard pops up when widget gains focus.
So to prevent this behaviour set focusable to false. Then normal use OnClickListener.
<EditText
android:focusable="false"
...
/>
Now, it should works.
You can use onTouch instead of onClick, so it doesn't matter if the EditText has focus or not.
edt.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
return false;
}
});
Nothing much to do you just have to
edt.setFocusable(false);
If focusableInTouchMode is true then touch is triggered in second touch only, so unless you want that case use false for focusableInTouchMode. and if you want to enable the focusability in the view set focusable true
<EditText android:focusable="true" android:focusableInTouchMode="false" ... />
make your alert dialog box appear on
setOnFocusChangedListener()
You should add onFocusChangeListener:
edt.setKeyListener(null);
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
edt.callOnClick();
}
}
});
edt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});
Avoid using a FocusChangeListener since it will behave erratically when you don't really need it (eg. when you enter an activity). Just set an OnTouchListener along with your OnClickListener like this:
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
view.requestFocus();
break;
}
return false;
}
This will cause your EditText to receive focus before your onClick call.
Instead of setting input type use "Editable=false" and "Focus=false" if you don't require keyboard.
It maybe helpful to you.
This was a real problem for me when trying to reproduce a "click" sound from the EditText when the soft keyboard pops up; I was only getting a click every second time. What fixed it for me was the the opposite of what worked for #neaGaze. This worked for me in my_layout.xml :
<EditText android:focusable="true" android:focusableInTouchMode="true" ... />
It allows the click sound/event to happen each time when user enters the EditText, while also allowing the soft keyboard to show. You have to handle the OnClickListener of course for this to happen, even if you do nothing with it, like so :
myEditText = (EditText) findViewById(R.id.myEditText);
...
// implement the onClick listener so we get the click sound and event if needed
myEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//do something or nothing; up to you
}
});
Speaking of that pesky soft keyboard, if I finished from my Dialog style Activity with the soft keyboard up, no matter what I tried the keyboard remained up when I was returned to MainActivity. I had tried all the usual suggestions such as Close/Hide the Android soft keyboard , How to close Android soft keyboard programmatically etc. None of that worked.
In my case I did not need the soft keyboard in MainActivity. What did work was the following in my AndroidManifest.xml file, within the MainActivity section
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>

EditText focus in android

In my application when I click an EditText, I have to perform some logic. I have the code. But it is not going into the click method.
My code:
EditText des=(EditText)findViewById(R.id.desinc);
des.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
java.lang.System.out.println("Inside click");
EditText income=(EditText)findViewById(R.id.editText1);
// TODO Auto-generated method stub
String inc=income.getText().toString();
int indexOFdec = inc.indexOf(".");
java.lang.System.out.println("index="+indexOFdec);
if(indexOFdec==0)
{
java.lang.System.out.println("inside index");
income.setText(inc+".00");
}
}
});
What am I doing wrong? Help me.
Try overriding onTouch by setting up an onTouchListener in the same way as an onClickListener. Use this code as a reference.
EditText dateEdit = (EditText) findViewById(R.id.date);
date.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//anything you want to do if user touches/ taps on the edittext box
}
return false;
}
});
UPDATE(why this behavior):
The first click event focuses the control, while the second click event actually fires the OnClickListener. If you disable touch-mode focus with the android:focusableInTouchMode View attribute, the OnClickListener should fire as expected.
You can also try this: set android:focusableInTouchMode="false" for your EditText box in the xml. See if it works with the existing code.
You should use OnFocusChangeListener()
Try clicking EditText twice because at first instance EditText gets focus and after that EditText's click event executes. So, if you want your code to execute on first click write your code for focus change of EditText using OnFocusChangeListener().

How to hide the soft keyboard on EditText

I want to hide soft keyboard on EditText even on 'click' also. I mean to say there should not be visible soft keyboard in my activity, because I am having own keyboard to enter data.
Please help me... Thanks...
editText_input_field.setOnTouchListener(otl);
private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
source : how to block virtual keyboard while clicking on edittext in android?
Set EditText widget's inputType to null like this,
editTExt.setInputType(TYPE_NULL);
paste below code in your xml file under edittext tag
android:textIsSelectable="true"

Categories

Resources