I'm doing a simple calculator.
How do i make a filter for a Edittext to only show numbers and symbols +-*/?
you use digits, for example:
<EditText
android:id="#+id/et_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:digits="1234567890-+*/. "
android:maxLength="10" />
now only "1234567890-+*/. " will be allowed in the edittext
this will filter numbers only, though I would suggest that you make buttons that add the number to a plain text view so the user never has the option to edit the text unless they are using the buttons you make, if all you are doing is making an add,subtract,multiply,divide calculator then it would be a much easier way of doing it
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_marginBottom="79dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
Related
I have a strange problem. Whatever in Edittext , all letters are autocapitalize. I don't want that. I want text to appear same as user types which is lower and uppercase both.
Here is my code
<EditText
android:layout_width="match_parent"
android:textAppearance="? android:attr/textAppearanceSmall"
android:layout_height="150dp"
android:cursorVisible="true"
android:layout_marginBottom="15dp"
android:hint="Feedback"
android:background="#drawable/edit_text"
android:textCursorDrawable="#null"
android:gravity="top"
android:inputType="textMultiLine"
android:padding="8dp"
android:id="#+id/feedback_input" />
Make android:textAllCaps="false" in your edit text.
I have an EditText with inputType = text, because a math expression can be entered.
I would like to have a keyboard that shows numbers along with +, %, - and *.
Not sure what that kind of keyboard is called.
Is there a way for me to set it up without having to change inputType to number.
Thanks!
Use
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal|numberSigned"
android:digits="0123456789.+-*/%()"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:digits="0123456789.+-*/"
android:id="#+id/inputs"
/>
Try setting input type to your EditText
<EditText android:inputType="number" ... />
EDIT
There's another option for better customization here
AndroidDoc Use the inputType numberDecimal|numberSigned for calculator
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal|numberSigned" >
</EditText>
While declaring EditText in xml file, I was warned exactly as given below
No label views point to this text field with an android:labelFor="#+id/#+id/start" attribute
EditText code is
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/start"
android:hint="#string/edit_message" />
I also had no idea what this error message was trying to inform me to do!
Thankfully I found an explanation: http://thecodeiscompiling.blogspot.com/2013/12/android-labelfor-waning-fix.html
the warning is telling you the EditText field does not have a TextView that can be read to a user when accessibility is turned on, since you haven't specified one with the android:labelFor attribute.
Solution for this warning:
No label views point to this text field with an android:labelFor="#+id/#+id/editText1" attribute
When you drag and drop and Text Fields into Graphical Layout you get this above error .
The generated code looks like this:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="31dp"
android:layout_marginTop="154dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
Solution :
add this label "android:labelFor="#+id/editText1" as shown below .
<EditText
android:id="#+id/editText1"
android:labelFor="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="31dp"
android:layout_marginTop="154dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
Use id like following in your editText.
android:id="#+id/editText1"
And then if you want to set labelFor then use
android:labelFor="#id/editText1"
Use android:labelFor="#+id/start" instead of android:id="#+id/start".
I want to make a multi line edit text but the problem is that whenever i enter new line height of edit text increases. how to fix its height. My code is
<EditText
android:id="#+id/editAdditionalInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editPhoneCutomerDetails"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_below="#+id/editPhoneCutomerDetails"
android:layout_marginTop="10dp"
android:background="#drawable/message_additional_box1x"
android:ems="10"
android:padding="10dp"
android:gravity="top">
<requestFocus />
</EditText>
To fix the size of editText you can use
android:singleLine="true"
but it can limit your editText line to 1 .
if you want more lines in editText then use the following property.
Use android:lines="3" .
Use android:lines="2" for fixing multilines and for fixing single line use android:singleLine="true"
for example use like this as shown for multilines
<EditText
android:id="#+id/.."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Name"
android:maxLength="20"
android:lines="2"// fix the multiline limit
android:textColor="#000000" />
for example use like this as shown for single line
<EditText
android:id="#+id/.."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Name"
android:maxLength="20"
android:singleLine="true"// fix the single line limit
android:textColor="#000000" />
try this;
android:inputType="textMultiLine"
<EditText
android:inputType="textMultiLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
As singleline is now deprecated use inputType:multiline and set the number of lines you want with lines tag
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="4"/>
I had a similar requirement but ended up not changing the size of the box. i.e it grows as text is typed into it but stops short of blocking the other widgets in view. I read this post during my quest and had found a solution. Decided to post it back here. Restricting size to a value may possibly have different effects on different screens. So I think it is better to let it be.
The thing is to put the edit text inside a linearlayout with the rest of the widgets. The code and snapshot are here
Multi line edit box with ok cancel buttons
I need help with my app. This is my xml file with Relative layout, and I need to jump to the next edittext after pressing Enter key.
But there is a problem, if I'm writing to the editTextKm and press Enter key, it jumps to the editTextFullTank, but I need to jump to the editTextManufactYear. How to do it?
<EditText
android:id="#+id/editTextSPZ"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="11"
android:inputType="textCapCharacters"/>
<EditText
android:id="#+id/editTextKm"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editTextSPZ"
android:inputType="number"
android:maxLength="7"/>
<EditText
android:id="#+id/editTextManufactYear"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editTextKm"
android:layout_alignBottom="#+id/editTextKm"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_toRightOf="#+id/editTextKm"
android:inputType="number"
android:maxLength="4" />
<EditText
android:id="#+id/editTextFullTank"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editTextKm"
android:inputType="number"
android:maxLength="3"/>
<EditText
android:id="#+id/editTextReserveTank"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/editTextManufactYear"
android:layout_toRightOf="#+id/editTextFullTank"
android:inputType="number"
android:maxLength="3" />
<EditText
android:id="#+id/editTextNote"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/editTextFullTank"
android:capitalize="sentences"
android:singleLine="false"/>
For further details you can consult Anddroid Developer blog.
Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides by using these XML attributes in the layout file:
android:nextFocusLeft="reference"
android:nextFocusRight="reference"
android:nextFocusUp="reference"
android:nextFocusDown="reference"
add the attributes:
android:nextFocusDown="#id/your_next_edit_text_to_focus" to each EditText according to what you need.
You can also use :
android:nextFocusUp="#id/your_next_edit_text_to_focus"
android:nextFocusLeft="#id/your_next_edit_text_to_focus"
android:nextFocusRight="#id/your_next_edit_text_to_focus"