Monodroid - EditText input method won't accept numbers - android

I am having some very strange problems with the EditText control in Mono for Android. My solution is targeting 2.3 and I am debugging on a T Mobile VivaCity. Here is my AXML for the EditText
<EditText
android:inputType="text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ctl_searchText" />
When I show the View containing the EditText the keyboard automatically appears, this is not a problem. The problem is I can't enter any numbers by tapping the numbers on the keyboard, the only way I can get a number to show in the text field is if I hold the key down and chose the number from a context menu. Although once I've entered a number in using this method I'm then not able to delete it. I've tried all sorts of input methods and had a look for similar issues in SO to no avail. Does this sound like an issue with the device? Or is there something glaringly obvious I'm not doing in the code/AXML?
== EDIT ==
I think I've narrowed the problem down, it has something to do with the KeyPress event handler used on the EditText. As the EditText represents a search field I have added the attribute android:singleLine="true" to stop the return key from adding an extra line and instead say "Done". When I add a KeyPress event handler to the control this is when it stops me from entering numbers, but without the handler it begins to function normally again. Here is what I have:
<EditText
android:id="#+id/ctl_searchText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
EditText ctl_searchText = FindViewById<EditText>(Resource.Id.ctl_searchText);
ctl_searchText.KeyPress += (object sender, View.KeyEventArgs e) =>
{
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
Toast.MakeText (this, ctl_searchText.Text, ToastLength.Short).Show ();
e.Handled = true;
}
};
With this code I cannot enter numbers into the text field, but I can enter letters. When I remove the event handler it works again, allowing me to enter all characters. I'm going to carry on investigating, this is very strange.

Please cheack that you doesn't use OnKeyListener. If yes just check that onKey (View v, int keyCode, KeyEvent event) method return True if the listener has consumed the event, false otherwise. In your case it something like this:
ctl_searchText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_ENTER){
//do smth
return true;
}
return fasle;
}
});

Try adding an else stating that e.Handled = false;
Your code will then look like:
ctl_searchText.KeyPress += (object sender, View.KeyEventArgs e) =>
{
if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
{
Toast.MakeText (this, ctl_searchText.Text, ToastLength.Short).Show ();
e.Handled = true;
}
else
{
e.Handled = false;
}
};

Related

EditText not accepting numbers

A client is testing an Android app I made for them and they are using a tablet. They say that they can't enter numbers in an EditText, but it seems to work on my phone. What could be a reason or this?
Here is the code for a particular EditText:
pWord = (EditText)findViewById(R.id.passwordsignin);
y = false;
signIn = (Button) findViewById(R.id.signin);
pWord.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER){
y = true;
new RegisterDeviceAsynctask().execute();
}
return true;
}
});
and the XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordsignin"
android:layout_below="#+id/emailsignin"
android:layout_centerHorizontal="true"
android:hint="Password"
android:layout_marginTop="10dip"/>
Edit: The issue ended when I ended the setOnKeyListener code. Any idea why this could be?
tell your client to use two types of text, for example: android:inputType= "typeOne|typeTwo" ... change typeOne and Two and use Log to print real output.
Some suggestions:
1. Your android:inputType="textPassword" will not let anyone see what is being entered. Try changing the android:inputType="text" so you can see if the expected character is being entered. That will let you debug the issue. It is possible the user is mistyping.
2. Try logging the password or putting it into a Toast so the user can see what they typed.
The above suggestions are for the debug period only. For release you don't want to have those in place. Your users should probably change their password after the debug period (or before, to a temporary password, and then change it back to the real password).
Try this
android:inputType="TYPE_TEXT_VARIATION_PASSWORD|TYPE_NUMBER_VARIATION_PASSWORD"
This will ensure that an appropriate keyboard/input method is selected for it (this can be dependent on the actual hardware manufacturer).
If that doesn't work, ask that they separate the physical keyboard from the tablet, to make the software keyboard come up.
If that doesn't work, see if they don't have a third party custom keyboard installed. Not only we have different hardware manufacturers, but since people can install their own keyboards from Google Play, that's another possibility.
If that doesn't work, ask them to take a screenshot of the keyboard they're getting on it, anonymize the screenshot the best way you can, and then post it on here in your question.
Just change youe code a bit:
pWord = (EditText)findViewById(R.id.passwordsignin);
y = false;
signIn = (Button) findViewById(R.id.signin);
pWord.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER){
y = true;
new RegisterDeviceAsynctask().execute();
return true;
}
return false;
}
});
just use this code as your SetOnKeyListener
if (keycode==keyevent.keyback)
{
...
}
else
{
return false;
}

Text watcher in android works once only

I have written a key listener on a edit text in android.
Following is my code:
textview.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter"
// button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on Enter key press
if (textview.getText().toString().length() == 15) {
textvalue = textview.getText().toString();
textview.setText(replacecardformat());
textview.clearFocus();
Log.e(""TAG, "Executed");
return true;
} else {
return false;
}
}
return false;
}
});
However the log statement is executed only once.Is some problem in my return statement.
Two observations:
if you need listener on each text change use view.addTextChangedListener(TextWatcher). Text Watcher has three methods: one fired before, one after, and one on text change. I suppose that this is what you are looking for. More details and tutorial you can find here
is your textview an TextView or EditText? I am asking since only EditText can receive keyboard input. however TextView can have such listener too. Because its text can also change (see documentation here).

How to set MultiLine and imeOptions="actionDone" for an EditText on Android?

How to set these options at the same time :
android:minLines="3"
android:inputType="textMultiLine"
android:imeOptions="actionDone"
It seems as soon as I put android:inputType="textMultiLine", the keyboard automatically replace the key OK by the key Enter. Does anyone know if it is possible to have both keys ?
NB : this answer is not what I am looking for. I would like both keys.
Hi i am also face the same issue finally i got solution for this.
change
android:inputType="textMultiLine"
to
android:inputType="text"
AND
Inside the .java file access EditText using id
editText.setHorizontallyScrolling(false);
editText.setMaxLines(3);
And Now implement the OnEditorAction over the editText.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == 4) { //actionId 4 for actionDone And 6 for actionSend
//perform action what you want
return true;
} else
return false;
}
});
The only thing guaranteed is that Android will pass the inputType and imeOptions to the IME. What the IME does with them is implementation-dependent. Where some IMEs may decide there's sufficient screen real estate to display both keys when in multi-line mode, that behavior shouldn't be relied upon.
Have written an answer here for similar question : https://stackoverflow.com/a/42236407/7550472 and it turned out to be the saving grace for me as nothing else worked. For easier access, i'll paste the code here too for lazy people like me ;).
In your Java code :
////////////Code to Hide SoftKeyboard on Enter (DONE) Press///////////////
editText.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE); //Set Return Carriage as "DONE"
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (event == null) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Capture soft enters in a singleLine EditText that is the last EditText
// This one is useful for the new list case, when there are no existing ListItems
EditText.clearFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
else if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Capture soft enters in other singleLine EditTexts
} else if (actionId == EditorInfo.IME_ACTION_GO) {
} else {
// Let the system handle all other null KeyEvents
return false;
}
}
else if (actionId == EditorInfo.IME_NULL) {
// Capture most soft enters in multi-line EditTexts and all hard enters;
// They supply a zero actionId and a valid keyEvent rather than
// a non-zero actionId and a null event like the previous cases.
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// We capture the event when the key is first pressed.
} else {
// We consume the event when the key is released.
return true;
}
}
else {
// We let the system handle it when the listener is triggered by something that
// wasn't an enter.
return false;
}
return true;
}
});
The minLines defined in XML will remain the same while the other two attributes are not required as its handled in the Java code.
If you create a subclass of EditText and insert this function, it should solve your problem.
This question was answered at https://stackoverflow.com/a/5037488/7403656 however I made a little alteration which gets the imeOption from the xml instead of just setting it to the Done option.
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeOptions = getImeOptions();
int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION;
if ((imeActions & imeOptions) != 0) {
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= imeOptions;
}
if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}

EditText Minimum Length & Launch New Activity

I have a couple of queries regarding the EditText function in Android.
First of all, is it possible to set a minimum number of characters in the EditText field? I'm aware that there is an
android:maxLength="*"
however for some reason you can't have
android:minLength="*"
Also, I was wondering if it is possible to launch a new activity after pressing the enter key on the keyboard that pops us when inputing data into the EditText field? And if so, could someone show me how?
Thanks for any help you could offer regarding either question :)
To respond to an enter key in your edit field and notify the user if they haven't entered enough text:
EditText myEdit = (EditText) findViewById(R.id.myedittext);
myEdit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if (myEdit.getText().length() < minLength) {
Toast.makeText(CurrentActivity.this, "Not enough characters", Toast.LENGTH_SHORT);
} else {
startActivity(new Intent(CurrentActivity.this, ActivityToLaunch.class);
}
return true;
}
return false;
}
});
There's no simple way to force a minimum length as the field is edited. You'd check the length on every character entered and then throw out keystrokes when the user attempt to delete past the minimum. It's pretty messy which is why there's no built-in way to do it.

Android: Remove Enter Key from softkeyboard

In my login form when user clicks on an EditText and presses the enter key, this inserts a new line, therefore increasing the EditText's size. Next moment, it returns to its previous place and prints a dot in the password field (which is the next field).
I want to remove this enter key from the softkeyboard. Is it possible?
Use :
android:singleLine = "true"
or
edittext.setSingleLine();
And your ENTER key is gone
add this tag to textView in xml
android:singleLine = "true"
I am afraid you can't do this. But one thing is you can handle the softkeyboard keyevents like this,
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Log.i("event", "captured");
return false;
}
else if(event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK){
Log.i("Back event Trigered","Back event");
}
}
}
return false;
}
});
Apart from this, you have to note that providing the attribute android:singleLine=true will make your edittext from growing in size when the soft keyborad ENTER is pressed
Inside the tag EditText you only have to do:
android:singleLine="true"
this remove the enter key in the keyboard
UPDATE
Inasmuch as android:singleLine="true" is deprecated I use android:maxLines="1" to avoid the enter in a EditText. How the name of the method says only N lines is permitted.
New update :
android:maxLines="1"
If you want something more generic in your .java:
boolean state = true;
yourTextInputEditText.setSingleLine(state);

Categories

Resources