Geetanjali
Geetanjali

Reputation: 1043

Runtime Textview Problem in Datepicker dialog

Is it possible to create a single run time Textview and upon OnClick show a DatePicker Dialog and when date is set show the result in the same TextView?

Upvotes: 1

Views: 430

Answers (2)

Geetanjali
Geetanjali

Reputation: 1043

I got answer of this... we have to pass the Text View where we want to display like if we have set "Pick date" before clicking so after showing dialog box the date that we want to set in the text View should be passed in the same Text View by referencing same Text View object....

Upvotes: 0

Kishore
Kishore

Reputation: 2051

 private int mYear;
 private int mMonth;
 private int mDay;
 private TextView mDateDisplay;
 static final int DATE_DIALOG_ID = 0;

 public void onCreate(Bundle savedInstaneState){
        super.onCreate(savedInstaneState);

 setContentView(R.layout.main);  

 mDateDisplay = (TextView) findViewById(R.id.datepicker); 

 mDateDisplay.setOnClickListener(this);


    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);


    }

public void onClick(View v) {

     showDialog(DATE_DIALOG_ID);         
}   
private void updateDisplay() {
    this.mDateDisplay.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" "));
}
 private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };
        @Override
        protected Dialog onCreateDialog(int id) {
           switch (id) {
           case DATE_DIALOG_ID:
              return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
           }
           return null;
        }

xml is

<TextView android:text="Select date"
  android:id="@+id/datepicker" 
  android:layout_width="wrap_content" 
  android:textStyle="bold"
  android:textSize="28dip"
  android:editable = "true"
  android:clickable="true"
  android:layout_height="wrap_content"/>

Upvotes: 2

Related Questions