In my application, I have an EditText that the user only has Read access not Write access.
In code I set android:enabled="false".
Although the background of EditText changed to dark, when I click on it the keyboard pops up and I can change the text.
What should I set to disable EditText?
I believe the correct would be to set android:editable="false".
And if you wonder why my link point to the attributes of TextView, you the answer is because EditText inherits from TextView:
EditText is a thin veneer over
TextView that configures itself to be
editable.
Update:
As mentioned in the comments below, editable is deprecated (since API level 3). You should instead be using inputType (with the value none).
use EditText.setFocusable(false) to disable editing
EditText.setFocusableInTouchMode(true) to enable editing;
You can try the following method :
private void disableEditText(EditText editText) {
editText.setFocusable(false);
editText.setEnabled(false);
editText.setCursorVisible(false);
editText.setKeyListener(null);
editText.setBackgroundColor(Color.TRANSPARENT);
}
Enabled EditText :
Disabled EditText :
It works for me and hope it helps you.
Use this to disable user input
android:focusable="false"
android:editable="false" This method is deprecated one.
For disable edit EditText, I think we can use focusable OR enable but
Using android:enabled=... or editText.setEnabled(...)
It also changes the text color in EditText to gray.
When clicked it have no effect
Using android:focusable=... or editText.setFocusable(false) - editText.setFocusableInTouchMode(true)
It doesn't change text color of EditText
When clicked it highlights the EditText bottom line for about few millisecond
Output
As android:editable="false" deprecated
In xml
Use android:enabled="false" it's simple. Why use more code?
If you want in java class you can also use this programmatically
editText.setEnabled(false);
As android:editable="false" is depricated.You can use InputType TYPE_NULL on EditText
use like this :
editText.setInputType(InputType.TYPE_NULL);
Simply:
editText.setEnabled(false);
To disable the functionality of an EditText, just use:
EditText.setInputType(InputType.TYPE_NULL);
in case you want to enable it some way, then you can use:
EditText.setInputType(InputType.TYPE_CLASS_TEXT);
I use google newly released Material Design Library. In my case, it works when I use
android:focusable="false" and
android:cursorVisible="false"
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/to_time_input_layout"
app:endIconMode="custom"
app:endIconDrawable="#drawable/ic_clock"
app:endIconContentDescription="ToTime"
app:endIconTint="#color/colorAccent"
style="#style/OutlinedEditTextStyle"
android:hint="To Time">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/to_time_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false" />
</com.google.android.material.textfield.TextInputLayout>
Set below properties in class:
editText.setFocusable(false);
editText.setEnabled(false);
It will work smoothly as you required.
This will make your edittext disabled.
editText.setEnabled(false);
And by using this
editText.setInputType(InputType.TYPE_NULL);
Will just make your Edittext not show your softkeyboard, but if it is connected to a physical keyboard, it will let you type.
Disable = FOCUS+CLICK+CURSOR
Disabling focus, click, and cursor visibility does the trick for me.
Here is the code in XML
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:clickable="false"
/>
if you use android:editable="false", eclipse will remind you this message "android:editable is deprecated: Use inputType instead".
So, I use android:focusable="false" instead, it worked well for me.
android:editable="false"
is now deprecated and use
YourEditText.setInputType(InputType.TYPE_NULL);
Using android:editable="false" is Depracted. Instead you'll need to Use android:focusable="false"
Use TextView instead.
In my case I needed my EditText to scroll text if no. of lines exceed maxLines when its disabled. This implementation worked perfectly for me.
private void setIsChatEditTextEditable(boolean value)
{
if(value)
{
mEdittext.setCursorVisible(true);
mEdittext.setSelection(chat_edittext.length());
// use new EditText(getApplicationContext()).getKeyListener()) if required below
mEdittext.setKeyListener(new AppCompatEditText(getApplicationContext()).getKeyListener());
}
else
{
mEdittext.setCursorVisible(false);
mEdittext.setKeyListener(null);
}
}
Try this one, works fine for me:
public class CustomEdittext extends EditText {
Boolean mIsTextEditor=true;
public CustomEdittext(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
#Override
public boolean onCheckIsTextEditor() {
// TODO Auto-generated method stub
return mIsTextEditor;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
mIsTextEditor=false;
Boolean mOnTouchEvent=super.onTouchEvent(event);
mIsTextEditor=true;
return mOnTouchEvent;
} }
Note: You need to add this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
on your activity or else keyboard will popup at first time.
As some answer mention it, if you disable the editText he become gray and if you set focusable false the cursor is displaying.
If you would like to do it only with xml this did the trick
<YourFloatLabel
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/view_ads_search_select"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"/>
</YourFloatLabel>
I simply add a FrameLayout appear above the editText and set it focusable and clickable so the editText can't be click.
This works for me:
android:focusable="false"
From #Asymptote's comment on the accepted answer, use:
myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);
...and Bob's your uncle.
Today I still use editable="false", but also with focusable="false".
I think the case we need to make an EditText un-editable, is because we want to keep its EditText style (with that underline, with hint, etc), but it accepts other inputs instead of text. For example a dropdown list.
In such use case, we need to have the EditText clickable (thus enabled="false" is not suitable). Setting focusable="false" do this trick, however, I can still long hold on the EditText and paste my own text onto it from clipboard. Depending on your code and handling this can even crash your app.
So I also used editable="false" and now everything is great, except the warning.
you can use android:focusable="false" but also need to disable cursor otherwise
copy/paste function would still work.
so, use
android:focusable="false"
android:cursorVisible="false"
There are multiple was how to achieve multiple levels of disabled.
editText.setShowSoftInputOnFocus(false); and editText.setFocusable
prevents the EditText from showing keyboard - writing in some text. But cursor is still visible and user can paste in some text.
editText.setCursorVisible(false)
hides the cursor. Not sure why would you want to do that tho. User can input text & paste.
editText.setKeyListener(null)
I find this way most convenient. There is no way how user can input text, but widget still works with OnClickListener if you want to trigger action when user touches it
editText.setEnabled(false);
completely disables EditText. It is literally 'read-only', user cannot input any text in it and (for example) OnClickListener doesn't work with it.
TextEdit documentation
Set this in your XML code, It works.
android:focusableInTouchMode="false"
Related
How can I disable spelling corrections in an EditText's soft-keyboard programmatically in Android? The user can disable it from settings, but I need to disable it in my application.
Is there any way to do this?
Set this in your layout's xml for your EditText:
android:inputType="textNoSuggestions"
Or call setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) in your Activity'
If you need to support API 4 and below, use android:inputType="textFilter"
If setting:
android:inputType="textNoSuggestions"
still doesn't work (and your text field is small), a quick work-around is to use:
android:inputType="textVisiblePassword"
I have an Editext . It contains attribute digits and imeOptions (actionDone) together.
<android.support.v7.widget.AppCompatEditText
android:id="#+id/edit_text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890abcdefghijklmnopqrstuvwxyz....."
android:hint="#string/item_name"
android:imeOptions="actionDone"
android:maxLines="1" />
The actionDone (Done button in Softkeyword) not found while using digit && imeOptions attributes together . We can only find enter button which doesn't make any focus change. I have tried it by skipping digit attribute , then imeOptions working correctly.
Thanks in advance
Just add singleLine="true" to your edittext
android:singleLine = "true"
Use setRawInputType() on your EditText View
view.setRawInputType(view.getInputType() & ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE)
It is important to call setRawInputType() and not setInputType(), since the latter will set the keylistener based on the inputmethod and your android:digits attribute will be discarded. setRawInputType() will only change the inputmethod and it won't touch the KeyListener, furthermore & ~EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE will disable the multi line mode, so no return key will be visible, instead your chosen imeOption should be visible.
Basically, there is a different behavior of singleLine and maxLines.
My testing with "android:digits" seems to cause problems in edittext fields and when setting imeOptions to android:imeOptions="actionDone" I could not get the "Done" button to appear on the keyboard.
Once I used
android:inputType="text"
without digits setting, the keyboard then presented "Done" (or a tick depending on your device's keyboard), and I could then capture the key stroke using:
editextField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch(result) {
case EditorInfo.IME_ACTION_DONE:
// put your code here.
break;
}
return false;
}
});
Hi you can programmatically set :
EditText edit = view.findViewById(R.id.memo_edit_text);
edit.setRawInputType(InputType.TYPE_CLASS_TEXT);
edit.setImeActionLabel("DONE", EditorInfo.IME_ACTION_DONE);
edit.setImeOptions(EditorInfo.IME_ACTION_DONE);
On the EditText that you want to associate with an IME Action
It works for textMultiLine and with any digits, just chose your action
credits : https://stackoverflow.com/a/52503760/11858207
In Kotlin you can also set
isSingleLine = true
I have an EditText in Android configured for the number keyboard, and I would like the keyboard's button to say "next", while mine is saying "done".
How can I change that?
I already tried:
<com.innovattic.font.FontEditText
style="#style/CadastroTextBoxStyle"
android:hint="CEP"
android:id="#+id/etCEP"
android:inputType="number"
android:singleLine="true"
android.imeOptions="actionNext" />
And also this:
etCEP.setImeActionLabel("Next", KeyEvent.KEYCODE_ENTER);
But it still says done.
What else can I do?
Thanks
As can be seen in Specifying the Input Method Type, you do not need to call TextView.setImeActionLabel(CharSequence, int) and you have to instead just provide a android:imeOptions value such as actionSend or actionNext in XML attributes to change the label accordingly.
This is not working for you because you have mistyped : as . in your attributes. Switching those out should fix your issue in no time.
Just wondered, am I the only one to encounter this "strange" behavior.
When placing an EditText inside my activity and setting its
inputType="textPassword" as follow:
<EditText android:text="" android:id="#+id/EditText01"
android:hint="This is a hint" android:inputType="textPassword"
android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
The hint is displayed with bigger/double spaces between the words.
If I remove the inputType attribute it all goes back to normal.
I couldn't find a known issue regarding this behavior.
BTW- If you wonder why this is important (it isn't that much) try
putting two EditText widgets one below the other and set the inputType
of one of them to "textpassword" it doesn't look good.
Any idea on how to change the password or the other edittexts to use the same format ?
Thanks
PS. The question was added here first: http://groups.google.com/group/android-developers/browse_thread/thread/88738bb8d8046f6f but I didn't find an answer.
It happens because typeface automatically setted to monospace in case of password field. Setting android:typeface="normal" on password field doesn't helps.
Here code from TextView sources:
if (password) {
setTransformationMethod(PasswordTransformationMethod.getInstance());
typefaceIndex = MONOSPACE;
} else if ((mInputType&(EditorInfo.TYPE_MASK_CLASS
|EditorInfo.TYPE_MASK_VARIATION))
== (EditorInfo.TYPE_CLASS_TEXT
|EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) {
typefaceIndex = MONOSPACE;
}
I can't find solution without implementing custom control with overriden typeface for hint.
P.S.: There is one solution, but it is not always acceptable - to set typeface to monospace on other EditText's.
Doing mEditText.setTypeface(Typeface.DEFAULT); fixes the problem for me.
I am trying to create an EditText which toggles its state between read only and write mode.
My XML is as below:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="4"
android:inputType="textMultiLine">
</EditText>
In my code i do the following :
textArea = (EditText) convertView.findViewById(com.pravaa.mobile.R.id.textArea);
//isEditable decides if the EditText is editable or not
if(!isEditable){
textArea.setInputType(InputType.TYPE_NULL);
}
//the view is added to a linear layout.
addView(textArea);
My issue is that the text does not get wrapped. Am i missing out on something? Kindly help me with this. I have also attached an image of my output.
The text set in the view is "12345678901234567 90123456789012345678901234567890 Nationwide Campaign New"
I was able to solve this issue by removing
android:inputType="textMultiLine"
To achieve the non-editable feature I used
setFocusable(false);
I guess that by calling this ...
textArea.setInputType(InputType.TYPE_NULL);
you override the flag InputType.TYPE_TEXT_FLAG_MULTI_LINE. Try calling this instead...
textArea.setInputType(InputType.TYPE_NULL|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
This line will make the text multi-line and will wrap the text
textArea.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
You'd probably also want to call setLines(n) to set the height of the textArea.
Try to replace:
editText.setInputType(InputType.TYPE_NULL);
with:
editText.setRawInputType(InputType.TYPE_NULL);
I would say that by using textArea.setInputType(InputType.TYPE_NULL); you remove the textMultiLine from your xml.
Just set textMultiLine isn't enough. Try this:
textArea.setHorizontallyScrolling(false);
textArea.setEllipsize(null);
textArea.setInputType(InputType.TYPE_NULL|InputType.TYPE_TEXT_FLAG_MULTI_LINE);
What me helped was:
edtText.setInputType(InputType.TYPE_NULL);
edtText.setSingleLine(false);
It seems that the setSingleline Attribute in the XML file is ignored.