Reputation: 1011
How to make a custom Data Picker like Date Picker in Android so that I can get data (int data) by custom input or by using the buttons (- & +) so that it become easy for user to enter larger value !! Has anyone any idea how to implement this? please provide example, code snippet if possible.
I want to make like this
left - Button
middle - EditText
right - Button
I want Like this image--
This is the example of Date Picker in Android:-
Upvotes: 0
Views: 683
Reputation: 1011
After reading through various posts, examples, articles and Android documentation I found solution to my own question, It can be achieved by two ways:
- First by using
NumberPicker
- Secondly by adding
TextChangedListener
to theEditText
Here is the code how I achieved it :
int noOfCups = 2;
EditText editTextNoOfCups;
editTextNoOfCups = (EditText) findViewById(R.id.edit_text_no_of_cups);
editTextNoOfCups.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != 0) {
Log.e("pss", String.valueOf(s));
noOfCups = Integer.parseInt(String.valueOf(s));
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Here is the link to official Android documentation :
Upvotes: 0
Reputation: 11749
You might want to use the NumberPicker
which will allow you to select any value.
It's not the same look but it might work for you: https://android--examples.blogspot.com/2015/05/how-to-use-numberpicker-in-android.html
Upvotes: 1
Reputation: 420
try on text changed listener as described in the link to update the variable whenever user changes the edit text, then variable will hold the changed value and so you can use your method to change values of that variable
Upvotes: 2
Reputation: 1368
Follow little code snippet, provided below
addButton.setOnClickListener(new OnClickListener(){
public void onClick(){
int valueInEditText = Integer.parseInt(etReference.getText().toString());
etReference.setText(String.valueOf(valueInEditText + 1));
}
});
Similaryly, do it for substration button.
Note: Do validation if you don't want negative values.
Upvotes: 1