How to enter a "Negative Decimal Number" in EditText? - android

I am making a calculator and I want to make sure that user is able to enter a "Negative Decimal Number" I see the option in the xml file separately for example in order to put the Decimal Number, the EditText's inputType must be numberDecimal and for negative number the inputType must be "numberSigned" but I need both. If I choose inputType as "text" then user is able to enter anything which throws an exception if it's not a number. I am not sure how to fix that either.

You can use both with the "|" operator. Just specify "numberSigned|numberDecimal".

Add this in the layout of the EditText:
android:digits="0123456789-."
You can specify more there, for example if you want the user to input sin() functions.

As #a11n says, you can use the pipe operator to specify both:
<EditText
...
android:inputType="numberSigned|numberDecimal"
... />
Valid numbers: positive and negative decimal and whole numbers.

Some phones have the decimal and negative in the same button and makes it unable to do negatives. I figured out a way you could separate the buttons by simply adding:
android:inputType="numberSigned|numberDecimal|textPersonName"
android:digits="0123456789.-"
That way you still have the number keyboard layout but the negative and decimal buttons will be separate and you cannot use any other digit that messes up the app.

Related

EditText height for fit a certain numbers of character

There's a way to set height of an EditText for contains at least n character ?
There are some number of ways that you can limit the editText as number of characters within multiple line as well.
Firstly, set this in your .xml:
android:inputType="textMultiline"
android:maxLength="n"
Secondly, you can also use a way around to reach your goal. Here it is. Implement TextWatcher to let user enter just 'n' characters. Whenever user enters 'n' characters, set the EditText to non-editable. And also set OnFocusChangeListener to it.
EditText editText = (EditText)findViewById(R.id.entry);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
For more details and also explanation of textWatcher, you can have a look number of input characters.
For more manipulation on multiple lines. You can set the number of lines this way, after you have set textMultiline, you can use any or some of below:
android:lines="8"
android:minLines="6"
android:maxLines="10"
android:scrollbars="vertical/horizontal"
Use android:layout_height="wrap_content" in your EditText.
If you want a single row of text only, use android:singleLine="true"
If you want it to have a minimum height set, use android:minHeight="x dp"
If you want it to be exactly n lines tall, use android:lines

How to allow Multiline Text in Android EditText while restricting input using android:digits?

With an EditText it is possible to add an entry to support alphanumeric digits like the following:
android:digits="#string/alphanumeric_allowed_chars_free_textentry"
Where the string is defined as
<string name="alphanumeric_allowed_chars_free_textentry">
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=[];,./~!##$%^</string>
However, as others have noted, when a user sets the digits attribute, it can cause the "enter" button to show a return arrow instead of a next or done button, as described in this related question
The typical solution to get done to show up is to set android:singleLine="true" and android:imeOptions="actionDone", but this will only work for an input box which is not a multi-line box. I have a multi-line edit text and would like to be able to add a done button and also fliter using the digits attribute.
Does anyone know how to allow an EditText to use digits while also being multiline, while also allowing the return button to show "done"? Android 4.4
What if you modify alphanumeric_allowed_chars_free_textentry to include carriage return (\r, \n)?
i was unable to reply / comment to your comment
So added new answer
in multiline edittext
after adding \r\n to my digits property
android:digits="1234567890#$%^&*()_+-=qwertyuiopasdfghjklzxcvbnm:;,.?/|~`{}[]QWERTYUIOPASDFGHJKLZXCVBNM+x÷x€£¥₩×\r\n ●■□⊙☆¤•°《》¿¡\"
i am able to goto nextline on enter as well using the digits property.
below is my axml code "
i was unable to reply / comment to your comment
in multiline edittext after adding \r\n to my digits property android:digits="1234567890#$%^&*()_+-=qwertyuiopasdfghjklzxcvbnm:;,.?/|~`{}[]QWERTYUIOPASDFGHJKLZXCVBNM+x÷x€£¥₩×\r\n ●■□⊙☆¤•°《》¿¡\"
i am able to goto nextline as well using the digits property .
"
<EditText
android:id="#+id/NewTask_TaskDescription"
style="#style/MultiLineTextboxStyle"
android:layout_marginBottom="10dp"
android:layout_height="151dp"
android:layout_width="match_parent"
android:inputType="textCapSentences|textMultiLine"
android:singleLine="false"
android:digits="1234567890#$%^&*()_+-=qwertyuiopasdfghjklzxcvbnm:;,.?/|~`{}[]QWERTYUIOPASDFGHJKLZXCVBNM+x÷x€£¥₩×\r\n ●■□⊙☆¤•°《》¿¡\\" />
"

Android emulator : insert negative number?

I suppose it must be easy but how do I insert a negative number in an EditText?
I am running the emulator on a Mac.
Thank you.
Just press '-' before you enter the number?
If that's not possible, there is probably something wrong with the inputType of the editText.
For example, android:inputType="numberDecimal" doesn't allow negative numbers. In that case you should add: android:inputType="numberDecimal|numberSigned".
In resource file .axml, EditBox change type
android:inputType="numberDecimal"
to
android:inputType="numberDecimal|numberSigned"
It works for me
EditText yourEditText = (EditText)findViewById(R.id.yourEditText);
yourEditText.setText("whatever");
editText.setText(String.valueOf(-1));

android EditText inputType for StreetNumber field

I'm trying to choose correct inputType in my adress dialog streetNumber field.
I want to show numeric keyboard first, but then let user also to input alphabetic characters
for some very special cases. Closer to this is inputType datetime,
but this doesn't allow to enter alphabetic characters. So how to set my streetNumber field correctly?
Use android:inputType="textPostalAddress"
The EditText inherits from TextView and shares its input type attributes with it. They can be found here in the official documentation.
Maybe the input type textPostalAddress would be suitable for your need. If not, plenty of other types are available. The XML attribute that allows setting this type is android:inputType="the type you have chosen".
See if this can help you
myEditText.setRawInputType(Configuration.KEYBOARD_QWERTY)

Android numberDecimal, considering locale in xml layout

For e.g. an EditText it is normal to specify android:inputType="numberDecimal" for a text field supposed to contain a decimal number. But this assumes that '.' is used as the decimal separator and in some countries ',' is used instead. Is it possible to specify in xml that the users locale needs to be considered or do I have to do it manually in my code?
This is known bug, see here for more information.
A possible workaround is to set android:inputType="numberDecimal" (so the digit keyboard will be opend) and android:digits="0123456789.," (so the comma can be entered into the EditText). Then add a TextChangedListener and do something like Double.parseDouble(editable.toString().replace(',', '.')); in afterTextChanged.

Categories

Resources