Popup DatePicker for EditText

Mark O'Sullivan picture Mark O'Sullivan · Jul 23, 2013 · Viewed 44.4k times · Source

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);
    }


}

Answer

Srinivasan picture Srinivasan · Jul 23, 2013

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