Prashanth
Prashanth

Reputation: 1011

How to make a custom Data Picker like Date Picker in Android

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--

Like This..

This is the example of Date Picker in Android:-

Android Picker example

Upvotes: 0

Views: 683

Answers (4)

Prashanth
Prashanth

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:

  1. First by using NumberPicker
  2. Secondly by adding TextChangedListener to the EditText

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

Distwo
Distwo

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

Nobody
Nobody

Reputation: 420

then try this link

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

Mrinmoy
Mrinmoy

Reputation: 1368

  • Design a layout as you want with two buttons and one edittext in middle
  • Then give one button addition funtionality and other substraction functionality

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

Related Questions