Does anyone know how to disable the blinking cursor in an EditText view?
You can use either the xml attribute android:cursorVisible="false" or programatically:
java: view.setCursorVisible(false)
kotlin: view.isCursorVisible = false
Perfect Solution that goes further to the goal
Goal: Disable the blinking curser when EditText is not in focus, and enable the blinking curser when EditText is in focus. Below also opens keyboard when EditText is clicked, and hides it when you press done in the keyboard.
1) Set in your xml under your EditText:
android:cursorVisible="false"
2) Set onClickListener:
iEditText.setOnClickListener(editTextClickListener);
OnClickListener editTextClickListener = new OnClickListener()
{
public void onClick(View v)
{
if (v.getId() == iEditText.getId())
{
iEditText.setCursorVisible(true);
}
}
};
3) then onCreate, capture the event when done is pressed using OnEditorActionListener to your EditText, and then setCursorVisible(false).
//onCreate...
iEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
iEditText.setCursorVisible(false);
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(iEditText.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
You can use following code for enabling and disabling edit text cursor by programmatically.
To Enable cursor
editText.requestFocus();
editText.setCursorVisible(true);
To Disable cursor
editText.setCursorVisible(false);
Using XML enable disable cursor
android:cursorVisible="false/true"
android:focusable="false/true"
To make edit_text Selectable to (copy/cut/paste/select/select all)
editText.setTextIsSelectable(true);
To focus on touch mode write following lines in XML
android:focusableInTouchMode="true"
android:clickable="true"
android:focusable="true"
programmatically
editText.requestFocusFromTouch();
To clear focus on touch mode
editText.clearFocus()
simple add this line into your parent layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
The problem with setting cursor visibility to true and false may be a problem since it removes the cursor until you again set it again and at the same time field is editable which is not good user experience.
so instead of using
setCursorVisible(false)
just do it like this
editText2.setFocusableInTouchMode(false)
editText2.clearFocus()
editText2.setFocusableInTouchMode(true)
The above code removes the focus which in turn removes the cursor. And enables it again so that you can again touch it and able to edit it. Just like normal user experience.
In my case, I wanted to enable/disable the cursor when the edit is focused.
In your Activity:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (v instanceof EditText) {
EditText edit = ((EditText) v);
Rect outR = new Rect();
edit.getGlobalVisibleRect(outR);
Boolean isKeyboardOpen = !outR.contains((int)ev.getRawX(), (int)ev.getRawY());
System.out.print("Is Keyboard? " + isKeyboardOpen);
if (isKeyboardOpen) {
System.out.print("Entro al IF");
edit.clearFocus();
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
edit.setCursorVisible(!isKeyboardOpen);
}
}
return super.dispatchTouchEvent(ev);
}
add android:focusableInTouchMode="true" in root layout, when edit text will be clicked at that time cursor will be shown.
If you want to ignore the Edittext from the starting of activity, android:focusable and android:focusableInTouchMode will help you.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout7" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true">
This LinearLayout with your Edittext.
Change focus to another view (ex: Any textview or Linearlayout in the XML) using
android:focusableInTouchMode="true"
android:focusable="true"
set addTextChangedListener to edittext in Activity.
and then on aftertextchanged of Edittext put edittext.clearFocus();
This will enable the cursor when keyboard is open and disable when keyboard is closed.
In kotlin your_edittext.isCursorVisible = false
rootLayout.findFocus().clearFocus();
Related
I have 2 EditText with id (et_1 and et_2) in a layout, and I set
for et_1
android:imeOptions="actionNext",
android:nextFocusForward="#+id/et_2"
when et_1 gets focus and the the software keyboard shows "Next", and when I click "Next", the focus doesn't go to et_2, but to another EditText in another fragment layout. Why is it this way?
How to make the focus go to et_2?
Thanks.
Try changing,
android:nextFocusForward="#+id/et_2"
to
android:nextFocusDown="#+id/et_2"
I hope it helps!
I had the same problem in one of my apps. What I did instead was have a listener in my activity for when "Next" was pressed and then just manually changed the focus. Like this:
final EditText et_1 = (EditText)findViewById(R.id.et_1);
final EditText et_2 = (EditText)findViewById(R.id.et_2);
// listen on the et_1 EditText
et_1.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_NEXT) { // "Next was selected"
et_2.requestFocus(); // focus on et_2
}
return true;
}
});
I realised referencing does not hide the keyboard
android:nextFocusDown="#+id/et_2"
To Hide the Keyboard add actionDone
<EditText
android:id="#+id/editTextEnterAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Amount"
android:imeOptions="actionDone"
/>
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);
I'd like for my enter key to complete an action without hiding the keyboard afterwards. Currently every time I enter something and press the enter button the EditText input is cleared to accept the next input, but as the keyboard disappears I have to click the EditText again so that the keyboard would appear again...
Right now this is on my layout:
<EditText
android:id="#+id/etCommand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/commandHint"
android:inputType="textNoSuggestions"
android:imeOptions="actionSend" >
</EditText>
And the code:
etCommand = (EditText) findViewById(R.id.etCommand);
etCommand.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendCommand();
}
return false;
}
});
EDIT
As swayam suggested I had to return true inside onEditorAction so that the OS would think the action was handled and leave it alone.
Have a go at this code snippet :
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Add the KeyListener for EditText for Enter key press and after the task completed requestfocus on edittext again would be solution
I'm using 4 EditText fields and 2 spinners in an activity. The order of these components are 2 EditText, then 2 spinners and then 2 EditText fields.
The problem occurs when I transfer focus (with the help of soft keyboard next button) from EditText to spinner, spinner does not get the focus and the focus is transferred to the next EditText field that was placed after the spinners.
I have used requestfocus() on spinner, but it did not work.
How do I make sure the spinner gets focus?
I managed to find a solution other then repositioning my Spinner. In the EditText before the spinner, add this listener:
editTextBefore.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
hideKeyboard();
textView.clearFocus();
spinner.requestFocus();
spinner.performClick();
}
return true;
}
});
You also need to add these line to able spinner to get focus:
spinner.setFocusable(true); // can be done in XML preferrable
My hideKeyboard function was just a visual detail that I wanted to add so the keyboard get hidden:
private void hideKeyboard() {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
Hope I have helped in this tricky question.
The flag InputMethodManager.HIDE_NOT_ALWAYS can be found in the documentation.
Thanks, I have solved by doing the following:
I set the Spinner object on top (within the onCreate method) just to make sure that my code gets executed first
I used the following:
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
s1.setFocusable(true);
s1.setFocusableInTouchMode(true);
s1.requestFocus();
This is a shot in the dark, but try setting the focusable property (in XML or in code; whatever way you are doing it) to true on the spinner.
http://developer.android.com/reference/android/view/View.html#attr_android:focusable
EDIT: Also, see this question: Can't manage to requestFocus a Spinner
I just had the same problem. I solved it using the nextFocusDown/Up/Left/Right properties.
<EditText
android:id="#+id/tPhone"
...
android:focusableInTouchMode="true"
android:focusable="true"
android:nextFocusDown="#+id/sCountry"/>
<Spinner
android:id="#+id/sCountry"
....
android:focusableInTouchMode="true"
android:focusable="true"
android:nextFocusUp = "#+id/tPhone"
android:nextFocusDown="#+id/tStreet"/>
<EditText
android:id="#+id/tStreet"
...
android:visibility="gone"
android:focusableInTouchMode="true"
android:focusable="true"
android:nextFocusUp = "#+id/sCountry"/>
But why this is even neccessary... beats me.
Whenever I click in the EditText the Android keyboard popup window appears, but I don't want the keyboard to pop up.
I want to permanently hide the android keyboard popup for my current application.
How can I do this?
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.
You may try to fake your EditText with a Button like this:
<Button
android:id="#+id/edit_birthday"
style="#android:style/Widget.EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_birthday"/>
In the manifest:
<activity
android:name=".YourActivity"
.
.
.
android:windowSoftInputMode="stateAlwaysHidden" >
</activity>
Works for all Android versions.
In your XML use the property android:textIsSelectable="true"
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
.
.
.
android:textIsSelectable="true"/>
And, in your Java class:
editText1.setShowSoftInputOnFocus(false);
In Kotlin:
editText1.showSoftInputOnFocus = false
Solution can be found here :
public void onCreate(Bundle savedInstanceState) {
edittext = (EditText) findViewById(R.id.EditText01);
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
}
These two line should do what you want:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Try This
public void disableSoftKeyboard(final EditText v) {
if (Build.VERSION.SDK_INT >= 11) {
v.setRawInputType(InputType.TYPE_CLASS_TEXT);
v.setTextIsSelectable(true);
} else {
v.setRawInputType(InputType.TYPE_NULL);
v.setFocusable(true);
}
}
Try to add this in yout onCreate() method.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Not tested but It Should work!!
In your xml file you can use this:
android:editable="false"
In your xml set attribute editable to false.
it won't let you edit the text and the keyboard will not be opened.
<EditText
android:editable="false"
/>