Edittext imeOptions actionDone not working with digits attribute? - android

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

Related

EditText: how to display the number while you are typing it?

I have three EditText widgets in my app and the XML for one of them is below. They work, mostly, but the problem is that the actual widget is at the bottom of my devices screen. When I tap the widget, and the numeric keypad is displayed, it completely covers the text entry window and I can't see what number I'm typing. Can I modify TextView so that when the Keypad is displayed on my device, it also displays the number that I'm currently typing?
<EditText
android:id="#+id/EditTextPrices" android:layout_width="100dip"
android:layout_height="wrap_content" android:inputType="number|numberDecimal"
android:singleLine="true" android.imeOptions="actionDone"/>
Another problem is that when I finish typing one number, instead of returning me to the main activity screen, it brings up the numeric keypad for the next TextEdit widget. I don't want this to happen. I had thought that setting android.imeOptions="actionDone" would cause the keypad to go away and be done when I finished typing the number, but not so. How can I stop the 'next' window from appearing?
Look into adding android:windowSoftInputMode to your manifest file. "adjustPan" as it's value is probably what you want though I've found that it sometimes fails to account for the space taken just above a softkeyboard by the strip of word-guesses while you're typing things in.
Add android:windowSoftInputMode=”adjustPan” to your manifest for the activity. That will cause the activity to scroll up so that the focused edit text is always on screen.
The other advice given to use adjustPan is good, and another idea is to put your layout in a scroll view.
Regarding your second problem, how are you handling actionDone?
For example this is how I hide the keyboard (or do whatever you want when they press done).
final EditText editTextPrices = (EditText) findViewById(R.id.editTextPrices);
editTextPrices.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionID, KeyEvent event) {
if (actionID == EditorInfo.IME_ACTION_DONE) {
InputMethodManager imm = (InputMethodManager) MyClass.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(bugemailaddress.getWindowToken(), 0);
// DO OTHER HANDLING OF INPUT HERE
return true;
}
return false;
}
});

Android How to show numeric softkeyboard with EditText of InputType="text"

I need allow to user input numeric information in a EditText, but the output needs to be formatted like "##.###,##" (# in [0..9]). The formatting I was made in a TextWatcher... this is good, the TextWatcher does the job... bute, when user selects the EditText, as it marked as text, the AlphaKeyboard is shown, if I select the EditText as numeric the keyboard I need is shown and the TextWatcher stop working.
put the following in your xml at the required edittext
<EditText android:inputType="number" ... />
refer to
How do I show the number keyboard on an EditText in android?
You need to change edittext entry like this.
<EditText android:inputType="phone" android:numeric="decimal"></EditText>
Use this code to show soft keypad.
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(v.getApplicationWindowToken(), 1);
As you are doing formatting in TextWatcher (##,####....) i.e. you are adding "," in EditText and you have made the EditText as numeric. If its numeric then "," char is invalid. Thus its not allowing TextWatcher to update the formatted text that contains ',".
You have 3 options to deal with this problem :
Make the EditText as normal instead of numeric. In TextWatcher look out for any other invalid chars other than 0-9. You can show IME for Mumber keypad when the focus is on the EditText.
Use Mask for EditText.
Let it be numeric. Trap FocusGain & FocusLost for the EditText. In focusGain, remove the formatting & set the inputType to Numeric. & In focusLost, first make the input type to Normal and then update the entered value to formmated value. You can set the input type to normal at runtime using setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL); & for numeric use : setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
I believe 1st option will be best to handle and manage out.

Android software keyboard switch between numeric and alphabetic programmatically

I have an EditText with inputMode = text. By default software keyboard is shown as alphabetical and user have to switch it to numeric by pressing specific key (like "123").
Having text inputMode is it possible to show numeric keyboard by default instead of alphabetic?
I need both alphabetic and numeric. But numeric is used more often then alphabetic so i search for way to switch mode programmatically.
I find the answer a day, finally I found this and its work.
android:inputType="textVisiblePassword"
source
Just set it with the normal setter:
EditText editText = (EditText) findViewById(R.id.edittext);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
// or
editText.setInputType(InputType.TYPE_CLASS_TEXT);
you can achieve it by setting the below attribute to EditText on xml has follows android:inputType="number"

How can I get a "done" button in softkeyboard?

how can I have a "done" button in my softkeyboard (Samsung Galaxy 10.1, Android 3.1) when writing in an EditText?
Using
<EditText
android:id="#+id/comment"
android:layout_width="772dp"
android:layout_height="200dp"/>
I get
If possible, I'd also like to remove this "attachment" button.
Anybody can help?
EDIT
I managed to get a "Done" button using
android:inputType="textImeMultiLine",
but the "return" button disappeared...
How can I have both? (I asked this new question here).
add this to your EditText xml:
android:imeOptions="actionDone"
or, to set it from code:
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
for more, read this
Using my Galaxy S2 phone
For the code below, each EditText will have a Return button that adds a new line:
EditText editText = new EditText(this);
For the code below, each EditText will have a Next button that navigates to the next field and the last one will have Done button that will dismiss the keyboard:
EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
For the code below, no change, each EditText has a Return button:
EditText editText = new EditText(this);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
For the code below, all EditText will have a Done button and all will dismiss the keyboard.
EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
For layouts use code below:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:imeOptions="actionDone"/>
In my Intel x86 Emulator at least, the "Done" key appears only if you specify the input type: "phone", "number", "text", "textPassword", ... with android:inputType. If you don't specify any or you set "textMultiLine", "Done" does not appear.
android:imeOptions="actionDone"
and
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
seem useless, since they don't change anything either in the first case (where "Done" appears anyway) or in the in the second case (since "Done" keeps not appearing) !
Add the next code to your EditText in xml
android:imeOptions="actionDone"
android:imeActionLabel="#string/done"
android:singleLine="true"
The android:inputType="text" field is optional
Use TextView.setImeOptions and pass it actionDone.

Disabling of EditText in Android

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"

Categories

Resources