EditText is not taking numeric input - android

I have an EditText view in my android application. My EditText is not taking integer values as an input however it is taking all the other ones. the following is my EditText View.
<EditText
android:id="#+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true">
</EditText>

On the possible issue that i faced once and suspects is: The EditText might be contained in an XML layout file and the layout file will be used for a Dialog instance. The problem occurs when the onKeyListener of the Dialog instance is returning some invalid default value i.e. 'return true' for irrelevant keys. In such case your EditText will be behaving strangely. The sample code for the scenario is
Dialog d=new Dialog(context);
d.setContentView(R.layout.mylayout);
d.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
Log.i("onkey", keyCode + " == Onkey");
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return true; // !!! this is wrong it should be return false;
}
});
}

Try this?
android:inputType="number"
android:digits="0123456789"
If it works, you can add letters in with the numbers in digits that you want to allow. Here's a handy list of all the inputTypes you could try. They mostly change what appears on the soft keyboard.

You need to add android:inputType="number" in your xml just like follows,
<EditText
android:id="#+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:inputType="number"> <------------- this line added
</EditText>

Related

For setting limit in multiline edittext 's each line

I want to set limits in edittext each line and then click enter key cursor move to the next line in android studio, please answer how can I do this..my codes are
<EditText
android:inputType="textMultiLine|number"
android:singleLine="false"
android:digits= "0,1,2,3,4,5,6,7,8,9"
android:id="#+id/number"
android:hint="Enter Number Here..."
android:paddingLeft="10dp"
android:textSize="15sp"
android:fontFamily="sans-serif"
android:layout_margin="5dp"
android:gravity="top|left"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="130dp" />
when I click enter key it did not work and when I remove inputType from XML code enter key works please solve
Follow steps below:
Step 1: add these lines in your XML
android:maxLines="1"
android:imeOptions="actionNext"
Step 2(optional): use following code in your Activity
If you want to do some action when user clicks next
editText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
//your code here
return true;
}
return false;
});
I m using AndroidX with Java 1_8 enabled
Hope this will help!
May be you need to use android:minEms or android:maxEms. Read more in following link:
https://developer.android.com/reference/android/widget/EditText

EditText keeps writing in single line Android

I know this question has been asked before and I tried several answers but none of them worked. I have an EditText where the user gives caption of a picture. The editText keeps writing the text in a single line. It doesn't go to the next line. I want the text to go to next line automatically when it reaches the screen's border/end. Here's my code. Please let me know if I have made any mistakes in my code.
<EditText
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="start"
android:inputType="textMultiLine|textPersonName"
android:hint="Write some thing about the photo"
android:maxLength="160"
android:paddingLeft="8dp"
android:textSize="14sp"
android:layout_below="#+id/privacyHolder"
android:lines="3"
android:layout_alignParentEnd="true"
android:ems="20"
android:paddingEnd="4dp"
android:maxLines="3"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
android:paddingTop="4dp"
android:background="#drawable/roundedbutton"
android:id="#+id/description"
/>
In the Resource, design your edit text as follow:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textMultiLine|textCapSentences"
android:gravity="left"
/>
In the activity you can use TextWatcher to listen when text exceeds few character and automatically move down.
Here is the snippet :
private boolean isReach = false;
// put this in onCreate method
edittext.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable s) {
// if edittext has 20 character reached add a new line, you can use either 20 or more here
if(yourtextEd.getText().length() == 20 && !isReach) {
yourtextEd.append("\n");
isReach = true;
}
// if edittext has less than 20 characters and isReach has set , change
if(yourtextEd.getText().length() < 20 && isReach) isReach = false;
}
});
Hope this will help you.

Execute function at soft key enter

I'm trying to add a way to execute a function on a soft key enter (or whatever the bottom right hand key would be, I assume its usually a enter/done key) once a edit text field has been filled in with numbers. I also have a calculate button that I would like to keep as a back up in an attempt to 'idiot proof' the app a little bit. below is a snippet of my code thus far; this part is working:
onCalculate refers to a button I have.
I have error checking for null values and whatever else.
EditText something
public void onCalculate (View v){
do stuff....
}
I want to add something down here to 'do stuff' in the event the user presses the done/enter/bottom right hand soft key instead of pressing the button. Below is a snippet from my layout XML:
<EditText
android:id="#+id/editText1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="numberDecimal"
android:textSize="15sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_margin/>
I know I probably need to create some kind of key listener for the enter key, but I'm not sure how to go about it.
You can achieve this by associating an OnEditorActionListener to the EditText. For example:
editText1.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText1.setOnEditorActionListener(new new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE)
{
onCalculate(editText1);
return true;
}
}
});

How to add "Next" on android keyboard

I've seen in some applications that in the keyboard appears a Button called next that puts the focus on the next Edit Text. I want to add this in my application, Do you know how I can do it? Or it's only on the application's keyboard? Thanks a lot.
Sorry, I haven't more information about this.
In the layout of edittext add android:imeOptions attribute. Here you can add different action buttons like Go, Search , Send , Next, Done etc. But to use this the EditText has to be a singleLine Else there will be an enter button there. So also use the android:singleLine="true".
So here is your solution
<EditText
android:id=//put ur id
android:layout_width=//according to need
android:layout_height=//accordintoneed
android:imeOptions="actionNext"
android:singleLine="true"/>
EDIT
for addition i should add some more for future use or for the help of others..
You can handle the imeoption actions from your code. for that you have to set setOnEditorActionListener to the Edittext and when a action is pressed you can get it on public boolean onEditorAction(TextView v, int actionId, KeyEvent event) method. and if you not handle the code by yourself the imeoptions action will do the default action (usually going to next control).
Here is an example code for handling the action
EditText e = (EditText)findViewById(R.id.editText1);
e.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_NEXT ) {
// handle next button
return true;
}
return false;
}
});
You just have to use the imeOptions tag in your layout. See imeOptions in docu.
<EditText
android:id="#+id/someField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"/>
You have to add these two line given below in your EditText
android:imeOptions="actionNext"
android:singleLine="true"
i tried all those answers posted here but none actually work. gladly I found myself a solution for this.
<EditText
android:id="#+id/etRegisterFirstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Firstname"
android:imeOptions="actionNext"
android:inputType="textCapWords"
android:maxLines="1" />
<EditText
android:id="#+id/etRegisterLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Lastname"
android:imeOptions="actionNext"
android:inputType="textCapWords"
android:maxLines="1" />
Please do notice that I ADD android:inputType. Thats it .
Cheers / Happy coding

AutocompleteTextView: on "NEXT" highlight next TextView, on "DONE", make keyboard disappear

I have two AutocompleTextViews and I want to switch to the next if the user presses "NEXT", and make the virtual keyboard disappear when he hits "DONE" at the second AutocompleTextView. So far, the buttons "NEXT"/"DONE" do nothing at all.... Unfortunately I found no resources addressing this problem.
Any suggestions?
thx
EDIT: Just want to add that this was asked when Android was on version 2.3 or something like that.
I ran into this problem and fixed it by setting the imeOptions on the AutocompleteTextView to actionNext.
Example:
<AutoCompleteTextView
android:id="#+id/dialog_product_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:completionThreshold="1"
android:imeOptions="actionNext"
/>
I found this solution for the "NEXT" problem: in your View source code write something like this
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (firstAutoComplete.hasFocus()) {
// sends focus to another field (user pressed "Next")
otherText.requestFocus();
return true;
}
else if (secondAutoComplete.hasFocus()) {
// sends focus to another field (user pressed "Next")
anotherText.requestFocus();
return true;
}
}
return false;
}
It seems to be an old Android http://code.google.com/p/android/issues/detail?id=4208.
Here I found my solution: http://groups.google.com/group/android-developers/browse_thread/thread/e53e40bfe255ecaf.
Just add these two lines into AutoCompleteTextView xml Code:
android:completionThreshold="1"
android:imeOptions="actionNext"
Don't forget to add inputType="text" otherwise you'll have enter button instead of next/done one:
<AutoCompleteTextView
android:id="#+id/suppliers"
android:layout_width="#dimen/summary_input_width"
android:layout_height="wrap_content"
android:hint="#string/current_supplier"
android:imeOptions="actionNext"
android:inputType="text"
android:lines="1" />

Categories

Resources