I am not able to read the date after selecting from calendar.Actually i didt show the calendar in normal way,i displayed calendar view in popup window its working fine.
when i tried to handle the click event of calendar view application is crashed.i am not understanding what to do.please help me to display selected date into EdiText1.
xml file activity_home1 is
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:removed="#CCC"
android:orientation="vertical" >
<Button
android:id="@+id/show_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Popup" />
<edittext>
android:id="@+id/editText1fordate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestfocus />
</edittext>
</linearlayout>
Myactivity class is:
package com.annomap.datepicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.annomap.datepicker.R;
import com.annomap.datepicker.R.string;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class Home1Activity extends Activity {
CalendarView cal;
Point p;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.show_popup);
button.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home1);
cal = (CalendarView) findViewById(R.id.calendarView1);
cal.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year,
int month, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, month, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd");
String formatteddate = sdf.format(c.getTime());
EditText editext1 = (EditText) findViewById(R.id.calendarView1);
editext1.setText(formatteddate);
}
});
Button btn_show = (Button) findViewById(R.id.show_popup);
btn_show.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (p != null)
showPopup(Home1Activity.this, p);
}
private void showPopup(Activity context, Point p) {
int popupWidth = 200;
int popupHeight = 150;
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup1);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout,
viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
int OFFSET_X = 110;
int OFFSET_Y = 30;
popup.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popup.showAtLocation(layout, Gravity.NO_GRAVITY,
p.x + OFFSET_X, p.y + OFFSET_Y);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home1, menu);
return true
}
}