Mark O'Sullivan
Mark O'Sullivan

Reputation: 10798

Popup DatePicker for EditText

Been trying to look at other questions on here regarding DatePickers popping up for EditText but I've had problems getting it working.

I've seen examples where you can use setOnClickListener or setOnTouchListener, which one would be the best?

Also I've seen a couple of different designs to the DatePicker, how do you change the design?

Below is my code so far, tried using code from the other examples but couldn't get it working.

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;


/**
 * Created by MOS182 on 7/21/13.
 */
public class AddReminder extends Activity {

    TextView Title, Amount, PaymentDate, ReminderDate, ReminderTime;
    EditText eTitle, eAmount, ePaymentDate, eReminderDate, eReminderTime;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminders_dialog);
        initializeVariables();
    }



    private void initializeVariables()
    {
        Title = (TextView) findViewById(R.id.tvTitle);
        Amount = (TextView) findViewById(R.id.tvAmount);
        PaymentDate = (TextView) findViewById(R.id.tvPaymentDate);
        ReminderDate = (TextView) findViewById(R.id.tvReminderDate);
        ReminderTime = (TextView) findViewById(R.id.tvReminderTime);
        eTitle = (EditText) findViewById(R.id.etTitle);
        eAmount = (EditText) findViewById(R.id.etAmount);
        ePaymentDate = (EditText) findViewById(R.id.etPaymentDate);
        eReminderDate = (EditText) findViewById(R.id.etReminderDate);
        eReminderTime = (EditText) findViewById(R.id.etReminderTime);
    }


}

Upvotes: 14

Views: 46094

Answers (3)

CrandellWS
CrandellWS

Reputation: 2804

For older APIs ie I still try to support 16 ... so:

From http://javapapers.com/android/android-datepicker/

You can use a DialogFragment that implements DatePickerDialog as exampled:

public static class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    // Thanks to -> http://javapapers.com/android/android-datepicker/
    EditText mEditText;
    public SelectDateFragment(EditText mEditText){
        this.mEditText = mEditText;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, yy, mm, dd);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(mEditText, yy, mm+1, dd);
    }
}

public void selectDate() {
    DialogFragment newFragment = new SelectDateFragment(editText);
    newFragment.show(getFragmentManager(), "DatePicker");
}
public static void populateSetDate(EditText mEditText, int year, int month, int day) {
    mEditText.setText(month+"/"+day+"/"+year);
}

In your activity set you editText value as required:

private EditText editText; 
//...

Attach the onclick listener:

    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectDate();

        }
    });

Upvotes: 0

Inderjeet Singh
Inderjeet Singh

Reputation: 111

/* create calndar object */  
  Calendar myCalendar = Calendar.getInstance();
/* find textview*/
date = (TextView) findViewById(R.id.stdate);
/* and copy the fallowing code*/
final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
              @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd/MMM/yyyy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.UK);

                date.setText(sdf.format(myCalendar.getTime()));

            }

        };


        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(MainActivity.this, datePickerListener, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();

            }
        });

Upvotes: 1

Srinivasan
Srinivasan

Reputation: 4661

I have used setOnClickListener EditText method to show a DatePicker.

And then in xml file set EditText property android:focusable="false".So that you can avoid focus and virtual keyboards.

yourEditText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //To show current date in the datepicker
            Calendar mcurrentDate=Calendar.getInstance();
            mYear=mcurrentDate.get(Calendar.YEAR);
            mMonth=mcurrentDate.get(Calendar.MONTH);
            mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog mDatePicker=new DatePickerDialog(**YourActivityName**.this, new OnDateSetListener() {                  
                public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                    // TODO Auto-generated method stub                      
                    /*      Your code   to get date and time    */
                }
            },mYear, mMonth, mDay);
            mDatePicker.setTitle("Select date");                
            mDatePicker.show();  }
    });

try this one...comment your thoughts...thanks..

Upvotes: 50

Related Questions