Displaypacker Dialog Box in Linear Layout

I am showing the user interface using advanced linear layout, not activity programmatically. For instance,

Public class DisplayDatePicker extends LinearLayout {

So I want to display the date selection by clicking the getDate button; as well as display on a text image. }

Date = Calendar.getInstance();

    btnnTest.setOnClickListener(new Button.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                 showDateDialog(etTest, Date);
                            }

                        });

now extends over funtion showDateDialog, it successfully makes a call, but does not display the datepicker dialog box;

protected void showDateDialog(EditText etLocationTest2, Calendar date2) {
        Log.i("", "Date showDateDialog ");
     ((Activity) getContext()).showDialog(DATE_DIALOG_ID);

    }

In here Could not display Date picker Dialog box, why? i think showDialog box could not call DATE_DIALOG_ID,
so how to call this, any idea?

Deatils of showDialog box here:

protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
                    return new DatePickerDialog(getContext(), datePickerListener, year, month,day);
        return null;
    }



private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;
            // set selected date into textview
            tvDisplayDate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));


        }

I hope you understand my problem and expect an answer.

+4
source share
1 answer

It worked for me. Hope this helps too.

I added all the code to implement this below

, linearlayout

DatePopListener mDatePopListener;
private DatePickerDialog datePickerDialog;
private SimpleDateFormat dateFormatter;

, , . , .

class displayDatePicker extends linearlayout{
DatePopListener mDatePopListener;
EditText edittext;
TextBox label;
private DatePickerDialog datePickerDialog;
private SimpleDateFormat dateFormatter;
public  displayDatePicker(Context context,String labelText){
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
Calendar newCalendar = Calendar.getInstance();
label.setText(labelText);
this.addView(label);
this.addView(txtBox);
txtBox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
          datePickerDialog.show();

        }
    });
    datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            txtBox.setText(dateFormatter.format(newDate.getTime()));
        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


}
//Now you can do your task here whatever as the date will be available in txtBox
//Enter your code as your requirements
}
+2

All Articles