0

I wanted write an application that has a button and when I click on the button I want to display a calender in a Dialog box like Datepicker in Dialog box.

I try to look constructor likes new DatePickerDialog.OnDateSetListener() for calender. Is it not possible to do this?

After api level 11 it supports calender view.

newday
  • 3,842
  • 10
  • 54
  • 79

2 Answers2

1

Hi Please try below code hope it will help you:

  <Button
            android:id="@+id/birthday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

and now call this button into your Activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_register);

         editBirthday = (Button)findViewById(R.id.birthday);


         /*
         * Change Birth day on click of edit box
         */
        editBirthday.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editBirthday.getWindowToken(), 0);
                showDialog(DATE_DIALOG_ID);
                return true;
            }
        });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, year, month,
                    day);
        }
        return null;
    }

    // updates the date we display in the TextView
    private void updateDisplay() {
        /*
         * Hide virtual keyboard
         */
        editBirthday.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(year).append("-").append(month + 1).append("-")
                .append(day).append(""));
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int myear, int monthOfYear,
                int dayOfMonth) {
            year = myear;
            month = monthOfYear;
            day = dayOfMonth;
            updateDisplay();
        }
    };
Ana Llera
  • 1,376
  • 16
  • 32
Manish Srivastava
  • 1,649
  • 2
  • 15
  • 23
  • Thanks for trying to help. I used this http://stackoverflow.com/questions/3712710/android-calendar-view-for-date-picker question doesnt work for my requirment. It shows date picker not a `Calender`. Only difference I see is you have set change click event to touch event. I need to show a Calender. thx. – newday Apr 01 '13 at 10:09
  • try this- if (android.os.Build.VERSION.SDK_INT > 10) { Datepicker.setCalendarViewShown(true); } – Manish Srivastava Apr 01 '13 at 10:16
  • where do I call `Datepicker.setCalendarViewShown(true);` method? There is no Datepicker is declared in the code – newday Apr 01 '13 at 10:28
  • I think you should create a new xml and add a calendar picker inside this xml and call this on click of button using Inflator.. Reason there we are using object of DatePickerDialog.OnDateSetListener so we are unable to create calendar view. My idea use Inflator class on click of button hope it will help you.. – Manish Srivastava Apr 01 '13 at 10:41
  • I didn't try ur answere yet and this is still open for any other answeres. – newday Apr 01 '13 at 11:38
1

//this code shows a calendar in a dialog, hope it helps you

LayoutInflater inflater =(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.calendar, null,  false);
    CalendarView cv = (CalendarView) ll.findViewById(R.id.calendarView);
    cv.setBackgroundColor(Color.BLACK);


    cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

    @Override
    public void onSelectedDayChange(CalendarView view, int year, int month,
            int dayOfMonth) {
        // TODO Auto-generated method stub

        Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth);
        int rows = showMapFragment(year, month, dayOfMonth);

        if (rows == 0){
            Toast.makeText(getApplicationContext(), year + "/" + (month + 1) +"/" + dayOfMonth + " No route to display", Toast.LENGTH_SHORT).show();
        }

        }
    });

    ll.findViewById(R.id.calendarView).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("calendar view", "calendar view clicked");
        }
    });

    Dialog calendarDialog = new Dialog(Main.this);
    calendarDialog.setContentView(ll);




    calendarDialog.setTitle("Pick a date to view your performance and route");
    calendarDialog.show();    

//the following is the calendar layout xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height= "400dp"
android:showWeekNumber="false"
android:clickable="true"
android:weekDayTextAppearance="@style/calendarStyle"
android:dateTextAppearance="@style/calendarStyle" 
/>

</LinearLayout>
ksu
  • 882
  • 1
  • 11
  • 17