1

I can't find how to reach the datepicker in a popup from the main Activity. When I call findIdByView() to get the datepicker, the program looks in the layout of main. But the datepicker is defined in the popuplayout. How do I get there?

I just have to be able to get the value of the datepicker when the popup is closed again.

This is my method making and closing the popup:

public void onSelectDayClick(View view)
{
    if(popup.isShowing())
    {
        //DatePicker datepicker = (DatePicker) this.findViewById(R.id.datePicker); (doesn't work)
        int year = datepicker.getYear();
        int month = datepicker.getMonth();
        int day = datepicker.getDayOfMonth();
        ;displayday = new DateTime(year, month, day, 12, 00);
        popup.dismiss();
        ;newDay();
        LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); 
        popup = new PopupWindow(inflater.inflate(R.layout.dateselect, null, false),LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    }

    else
    {
        popup.showAtLocation(this.findViewById(R.id.scrollView), Gravity.BOTTOM, 0, 150);
    }
}

My xml file: (dateselect.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:padding="10dip" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" >
<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" />
</LinearLayout> 

Thankx

Kat
  • 668
  • 2
  • 11
  • 24

2 Answers2

1

Simply use layout.findViewById instead of this.findViewById, also popup needs to be defined prior to checking if it's showing, i suggest putting it into onCreate

private Object datepicker;
private View layout;


protected void onCreate(Bundle savedInstanceState) {
    ...
    LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    layout = inflater.inflate(R.layout.dateselect, null, false);
    popup = new PopupWindow(layout,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
}
public void onSelectDayClick(View view)
{
    if(popup.isShowing())
    {
        DatePicker datepicker = (DatePicker) layout.findViewById(R.id.datePicker); 
        int year = datepicker.getYear();
        int month = datepicker.getMonth();
        int day = datepicker.getDayOfMonth();
     //   ;displayday = new DateTime(year, month, day, 12, 00);*/
        popup.dismiss();
     //   ;newDay();


    }

    else
    {
        popup.showAtLocation(this.findViewById(R.id.myTextView), Gravity.BOTTOM, 0, 150);
    }
}
adax2000
  • 424
  • 1
  • 3
  • 17
0

The answer by adax2000 did the job for me. Popup works perfectly.

Not sure what the purpose of the Object datepicker; declaration is at the top, as this is never used, and leaving it out works just fine.

Lars
  • 15
  • 1
  • 4