Jarvis
Jarvis

Reputation: 345

Android: date picker from calendar popdown

In my app user should select a date by clicking on Edit Text, for that currently i'm using a date picker dialog and its working good but its like opening another dialog and all. so i thought of showing it like pop down menu, checkout the pic. please help me to achieve this!

Calendar pic

Upvotes: 5

Views: 10484

Answers (2)

M D
M D

Reputation: 47807

You just create a New Popup window and inflate calender view into that window: follow the below steps:

edit_text.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                showPopup(MainActivity.this);
            }
        });

Now showPopup() method:

// The method that displays the popup.

private void showPopup(Activity context) {

               // Inflate the popup_layout.xml
               LayoutInflater layoutInflater = (LayoutInflater)getBaseContext() 
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               layout = layoutInflater.inflate(R.layout.main3, null,false);
               // Creating the PopupWindow
               final PopupWindow popupWindow = new PopupWindow(
                          layout,400,400);

               popupWindow.setContentView(layout);
               popupWindow.setHeight(500);
               popupWindow.setOutsideTouchable(false);
               // Clear the default translucent background
               popupWindow.setBackgroundDrawable(new BitmapDrawable()); 

                CalendarView cv = (CalendarView) layout.findViewById(R.id.calendarView);
                cv.setBackgroundColor(Color.BLUE);

                cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub
                    popupWindow.dismiss();
                    Log.d("date selected", "date selected " + year + " " + month + " " + dayOfMonth);

                    }
                });
              popupWindow.showAtLocation(layout, Gravity.TOP,5,170);
            }

Now, main3.xml file:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layout"
  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:clickable="true"
    android:showWeekNumber="false" />

 </LinearLayout>

and just below snap have a look and you'll customized this popup window by your self ok. snap

Upvotes: 3

M D
M D

Reputation: 47807

Try this @Jarvis

 public class MainActivity extends Activity {

 EditText date;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    date=(EditText)findViewById(R.id.date);

    date.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(ProximityActivity.this, dateD, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
}
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener dateD = new DatePickerDialog.OnDateSetListener(){

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
         // TODO Auto-generated method stub
        myCalendar.set(Calendar.YEAR, year);
        myCalendar.set(Calendar.MONTH, monthOfYear);
        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        updateLabel();
    }

};

  private void updateLabel() {

        String myFormat = "MM/dd/yy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.ENGLISH);

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

}

Upvotes: 2

Related Questions