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"
/>
Related
I'm new to Android Programming so was just making a simple app.
In the app I have an EditText component which slides up as the keyboard pops up and i don't want it to slide up so i searched for it and got an work around of using,
In OnCreate method
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
But the issue was after using this line the EditText was in place after clicking on enter the keyboard didn't went away.So, I search for it and got this method for hiding the keyboard
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
But now the issue is the keyboard is hiding successfully but is not visible after i re click on EditText.So, I was searching on the net but no luck and started started searching methods in Android Studio for making the keyboard visible and figured out some what this
public static void showSoftKeyboard(Activity activity){
InputMethodManager inputMethodManager1 =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager1.showSoftInputFromInputMethod(
activity.getCurrentFocus().getWindowToken(), 0);
But it's not working is throwing NullPointerException.
So please can anyone help me on this.
And also is it there any alternative for keeping the EditText on its position without sliding up. So that there is no need of applying this hide and show method and if not can you please tell me how to bring back the keyboard.
Comment your logic, and please try with below approach,
Write below line in your activity tag in Android Manifest like below
<activity android:name=".yourActivityName"
android:windowSoftInputMode="adjustPan">
</activity>
Then add this line to your edittext xml in your layout
android:imeOptions="actionDone"
like below
<EditText
android:id="#+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edittext"
android:singleLine="true"
android:maxLines="1"
android:imeOptions="actionDone"/>
Or set it from your code
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
On click on done button in soft keyboard, it will be automatically close.
And below is the code of click event of Soft Keyboard done button.
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
// write your code here
}
return false;
}
});
Try this
public void showSoftKeyboard(Context context, View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
or
public void showSoftKeyboard(Context context){
if(context == null) return;
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
So I need an input box for text in my android app.
I have this in my xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inputCodePA"
android:textSize="18sp"
android:inputType="text"
android:hint="#string/inputHint"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/arrowRightPA"
android:layout_toEndOf="#+id/arrowRightPA">
<requestFocus/>
</EditText>
and this:
public void setOnClickListners(){
final EditText inputBox = (EditText)findViewById(R.id.inputCodePA);
inputBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputBox.clearFocus();
inputBox.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(inputBox, InputMethodManager.SHOW_FORCED);
}
});
inputBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
inputBox.setText("");
inputBox.clearFocus();
inputBox.requestFocus();
return false;
}
});
}
For my code in my activity's class (setOnClickListner() is called in my onCreate()).
But, whenever after I type something into the EditText box and press Enter, I can not open the keyboard to enter text again.
I know I'm making a really basic error, but I can't seem to figure out what.
After all, this is the first time an EditText failed me.
HA!
I found out the problem!
It was because I had a ScrollView below the EditText in the code (which made the ScrollView become 'in front'), which did not allow the user to click on the view at all!
Hope this helps other people who makes these stupid mistakes!
I have an Edittext with android:windowSoftInputMode="stateVisible" in Manifest. Now the keyboard will be shown when I start the activity. How to hide it? I cannot use android:windowSoftInputMode="stateHidden because when keyboard is visible then minimize the app and resume it the keyboard should be visible.
I tried with
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
but it did not work.
In the AndroidManifest.xml:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
or try
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Please check this also
Use the following functions to show/hide the keyboard:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
Just add two attributes to the parent view of editText.
android:focusable="true"
android:focusableInTouchMode="true"
Put this in the manifest inside the Activity tag
android:windowSoftInputMode="stateHidden"
Try this:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>
Look at this one for more details.
To hide the softkeyboard at the time of New Activity start or onCreate(),onStart() etc. you can use the code below:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Using AndroidManifest.xml
<activity android:name=".YourActivityName"
android:windowSoftInputMode="stateHidden"
/>
Using Java
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
using the above solution keyboard hide but edittext from taking focus when activiy is created, but grab it when you touch them using:
add in your EditText
<EditText
android:focusable="false" />
also add listener of your EditText
youredittext.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}});
Add the following text to your xml file.
<!--Dummy layout that gain focus -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
</LinearLayout>
If you don't want to use xml, make a Kotlin Extension to hide keyboard
// In onResume, call this
myView.hideKeyboard()
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
Alternatives based on use case:
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
// Calls Context.hideKeyboard
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
view.hideKeyboard()
}
How to show soft keyboard
fun Context.showKeyboard() { // Or View.showKeyboard()
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}
Simpler method when simultaneously requesting focus on an edittext
myEdittext.focus()
fun View.focus() {
requestFocus()
showKeyboard()
}
Bonus simplification:
Remove requirement for ever using getSystemService: Splitties Library
// Simplifies above solution to just
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
I hope this will work, I tried a lot of methods but this one worked for me in fragments. just put this line in onCreateview/init.
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
add in your activity in manifasts
this property
android:windowSoftInputMode="stateHidden"
Put this code your java file and pass the argument for object on edittext,
private void setHideSoftKeyboard(EditText editText){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
To hide the softkeyboard at the time of New Activity start or onCreate(),onStart() method etc. use the code below:
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
To hide softkeyboard at the time of Button is click in activity:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Use SOFT_INPUT_STATE_ALWAYS_HIDDEN instead of SOFT_INPUT_STATE_HIDDEN
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Above answers are also correct. I just want to give a brief that there's two ways to hide the keyboard when starting the activity, from manifest.xml.
eg:
<activity
..........
android:windowSoftInputMode="stateHidden"
..........
/>
The above way always hide it when entering the activity.
or
<activity
..........
android:windowSoftInputMode="stateUnchanged"
..........
/>
This one says don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).
You can set config on AndroidManifest.xml
Example:
<activity
android:name="Activity"
android:configChanges="orientation|keyboardHidden"
android:theme="#*android:style/Theme.NoTitleBar"
android:launchMode="singleTop"
android:windowSoftInputMode="stateHidden"/>
Use the following code to Hide the softkeyboard first time when you start the Activity
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);
This is what I did:
yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup
yourEditText.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event); // handle the event first
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // hide the soft keyboard
yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext
}
return true;
}
});
Try this one also
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);
Ed_Cat_Search.setInputType(InputType.TYPE_NULL);
Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});
If your application is targeting Android API Level 21 or more than there is a default method available.
editTextObj.setShowSoftInputOnFocus(false);
Make sure you have set below code in EditText XML tag.
<EditText
....
android:enabled="true"
android:focusable="true" />
Try this.
First in your searchable xml the fields (name and hint etc) put #string and not literal strings.
Then method onCreateOptionsMenu, it must have a ComponentName object with your package name and your completed class name (with package name) - In case activity which has the SearchView component is the same as the show search results use getComponentName(), as the google android developer says.
I tried a lot of solutions and after much,much work this solution works for me.
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);
Ed_Cat_Search.setInputType(InputType.TYPE_NULL);
Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
Ed_Cat_Search.onTouchEvent(event); // call native handler
return true; // consume touch even
}
});
this one worked for me
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
it will works
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);
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();