Android NumberPicker not saving EditText changes - android

I have copied Android's NumberPicker widget to my own application, but I'm having one problem...
When someone manually clicks the EditText and changes it via the keyboard, the selection is not saved. Is there some listener that I can implement to check and see if the user manually changes the EditText to set it as current? Or something?

I found this solution lost somewhere in android-developers google group:
For android SDK NumberPicker widget, simply use:
myNumberPicker.clearFocus();
before you try to get its value.
When you have an activity with only the NumberPicker and maybe a button or a spinner, and you edit it and try to click elsewhere, its onFocusChangedListener is not handled properly. So you just need to force it to lost focus before using getValue(). Worked like a charm to me.

I used the Michael Novak number picker. Although onEditorAction was already implemented it didn't properly set the input. The easy fix for me was to call validateInput on the textView in getCurrent.
/**
* #return the current value.
*/
public int getCurrent() {
validateInput(mText);
return mCurrent;
}

I has same problem using numberpicker http://www.quietlycoding.com/?p=5 and I solved it by adding OnKeyListener (instead OnEditorActionListener which was already suggested by Junzi) to NumberPicker class. Other steps are same so:
Let NumberPicker.java extend OnKeyListener
add mText.setOnKeyListener(this); to NumberPicker constructor
implement OnKey:
public boolean onKey(View v, int keyCode, KeyEvent event) {
validateInput(v);
return false;
}

If you are using Android NumberPicker from Android4.0.x means you can get the Value of NumberPicker using the getValue().
http://developer.android.com/reference/android/widget/NumberPicker.html#getValue()
It's an Easy way to get the Numberpicker in android 4.0.x...
final NumberPicker np = new NumberPicker(CustomizedListView.this);
np.setMinValue(0);
np.setMaxValue(100);
np.setWrapSelectorWheel(true);
And you can get the value by using np.getValue() method.

You can try to set a TextWatcher to your EditText to do the same or do not allow an user to enter a value manually there...
Or keep an OK button there and take the values out from the EditText only when an user clicks the OK button
Did you try this sample ?
http://www.quietlycoding.com/?p=5

Not sure if you are using the same Numberpicker as mine: http://www.quietlycoding.com/?p=5. I have tried to add a OnEditorActionListener to the NumberClass, seems it solved the problem for me.
modify the NumberPicker.java let it extends OnEditorActionListener.
add mText.setOnEditorActionListener(this); to NumberPicker constructor
implement onEditorAction:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
validateInput(v);
return false;
}
Hope it can be helpful.

Related

Android using MaterialDatePicker and TextInputLayout

I am new to android development and currently trying to integrate material design into my app.
I would like to evaluate a simple form, for this purpose I used the components com.google.android.material.textfield.TextInputLayout and com.google.android.material.textfield.TextInputEditText for user input. Besides the text input, I need a date, which I want to read with a MaterialDatePicker.
I tried to display the MaterialDatePicker with OnFocusChangeListener, this works too, but I have two problems.
the display is a little bit delayed because first a keyboard is opened which is closed immediately after calling the MaterialDatePicker.
when the display is closed with the Back button, the focus is still on TextInputLayout. So I would have to change the focus first to open a MaterialDatePicker again.
This is how I implemented the OnFocusChangeListener
#Override
public void onFocusChange(View view, boolean selected) {
if( view.getId() == R.id.myId&& selected ){
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
MaterialDatePicker picker = builder.build();
picker.show( this.getParentFragmentManager(), "DATE_PICKER" );
}
}
Are there alternative components of Material Design that are better suited for the presentation? I would like to keep the behavior within the form, so as soon as the date is entered by the user, a small label should be displayed above, like this:
Thank you for your help.
I recently encountered the same problem.
The first issue concerning the keyboard, is solved by calling:
mTextInputEditText.setInputType(InputType.TYPE_NULL);
By setting the InputType to TYPE_NULL the keyboard won't open by clicking on the text field.
In addition, if you no longer want the user to be able to input any text, you can add:
mTextInputEditText.setKeyListener(null);
The second issue, to show the DatePicker again while it is already in focus, you can set an extra onClickListener:
mTextInputEditText.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
openDatePicker();
}
});
The OnClickListener is called as soon as the user clicks the text field again. Sadly it will not work with the first click.
You can look at this answer https://stackoverflow.com/a/11799891/9612595 for more information. Unfortunately, making the text field unfocusable resolves into weird behavior with the hint from Material.
I hope that helps!
Adding to luk321 answer. Instead of OnClickListener you can use OnTouchListener. For ex -
editText.setOnTouchListener((view, motionEvent) -> {
if(motionEvent.getAction() == MotionEvent.ACTION_UP){
//your code
}
return false;
});
It will work on first touch. Be sure to use ACTION.UP otherwise event will occur while scrolling also.
deliverDatePicker.editText?.setOnClickListener {
viewModel.onDatePickerClick()
}
deliverDatePicker.editText?.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
viewModel.onDatePickerClick()
}
}
Overriding setOnFocusChangeListener as well as setOnClickListener solves the first unregistered click event of #luk321 answer

EditText methods in java android

Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});

Setting time in edittext

I would like to implement an EditText that operates similarly to the android alarm app. In this app, there are two EditText fields that serve as the HH and mm. Typing in these fields overwrites the number that was previously there. When you have typed the second number into the HH field, focus automatically switches to the mm field.
I have tried to put this logic in manually using a TextWatcher in the afterTextChanged() method, but unfortunately modifying the text whilst in this method causes a recursive loop.
What's the correct way to implement this?
See below for alarm app example:
add listener to your hh text view like this
hh.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (hh.getText().toString().length() == 2){
mm.requestFocus();
}
return false;
}
});
hope this will work for you.
It turns out editing the text from the onTextChanged method doesn't actually cause a recursive loop, as long as you temporarily disable the logic with a boolean switch. Painstakingly wrote in all the logic into the TextWatcher to mimic the alarm-style EditText fields.

Android. Backspace/delete key not functioning.

When onKeyListener is set Backspace/delete key is not functioning.
I set an OnKeyListener on my EditText. Then default actions of some keys became not functioning. Like DELETE/Backspace. Then I changed to use my own text-deleting function by manipulating the string inside. But it seems to be pretty complex.
I have to get selection, make substring, and so on. Are there other solutions to get the key functioning normally?
It depends on the IME you are using.
Some IME implements delete function without sending KEYCODE_DEL.
Try other IME than the default.
For example, if you press DEL button long enough, some IME deletes all text in the edit box.
This cannot be done through KEYCODE_DEL.
I had this problem too, I solved it by returning false in onKeyListener function. This should execute normal operations on other keys.
.setOnKeyListener(new DialogInterface.OnKeyListener()
{
#Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
//your workarounds;
return false;
}
})
There are two known issues affecting KEYCODE_DEL for the default (LatinIME) Google Keyboard that ships with Android: Issues 42904 and 62306.
I have researched this and have devised a workaround, with code, that seems to get around both of these issues. That workaround can be found here:
Android - cannot capture backspace/delete press in soft. keyboard
I have similar problems that you are facing and I somehow managed to stumble on the solution. Apparently, I had setOnKeyListener to 'return true'. After I changed it to 'return false', the phone keyboard works perfect with backspace functioning properly once again on edittext. Hope this helps:
Solution: One of your existing onkeylistener codes contain 'return true'. Rectify it by setting existing code from 'return true' to 'return false'
.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
...
return false;
}
});

apply general onkeylistener

New to programming, now to android. So I hope I dont annoy you to much.
How would I go about setting an onkeylistener at the top level of the app that captured the keyevent no matter what.
Basically what i have is a linear layout with dynamically added edittexts.
I want to capture the Enter key event and have it get the current edittext, perform some tests then create a new edittext and add it to the layout.
I know I can (and have) implement an onkeylistener to individual child views, but not being a programmer, the logic seems weird to create an edittext that listens for input to create another edittext that listens for input to create another.... (you see where this goes)
Can anyone point me in the right direction?
I have lots more info about what Im trying to do, I just dont know what is pertinent and what is not, so let me know if you need more.
Thanks for your time in advance,
Chris
Take a look at http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29
What you want is to intercept all the events before they are processed by any View in the window. Return true if the event was handled or false if you want the childs to process the event further.
Like Ben said your activity can implement OnKeyListener then for each EditText you create, set the OnKeyListener to be the activity.
editText1.setOnKeyListener(this);
And then in your implementation of onKey you can handle the key press event.
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(v == editText1) {
// do something
} else if( v == editText2 ) {
// do something
}
return true; // return true if you handled the keypress
}
Your activity can implement OnKeyListener.

Categories

Resources