Click here to Skip to main content
15,913,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

HTML
<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:

Java
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);

		// Get the x, y location and store it in the location[] array
		// location[0] = x, location[1] = y.
		button.getLocationOnScreen(location);

		// Initialize the Point with x, and y positions
		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) {
				// TODO Auto-generated method stub
				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) {

				// Open popup window
				if (p != null)
					showPopup(Home1Activity.this, p);
			}

			private void showPopup(Activity context, Point p) {
				// TODO Auto-generated method stub
				int popupWidth = 200;
				int popupHeight = 150;

				// Inflate the popup_layout.xml
				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);

				// Creating the PopupWindow
				final PopupWindow popup = new PopupWindow(context);
				popup.setContentView(layout);
				popup.setWidth(popupWidth);
				popup.setHeight(popupHeight);
				popup.setFocusable(true);

				// Some offset to align the popup a bit to the right, and a bit
				// down, relative to button's position.
				int OFFSET_X = 110;
				int OFFSET_Y = 30;

				// Clear the default translucent background
				popup.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

				// Displaying the popup at the specified location, + offsets.
				popup.showAtLocation(layout, Gravity.NO_GRAVITY,
						p.x + OFFSET_X, p.y + OFFSET_Y);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.home1, menu);
		return true
	}
}
Posted
Updated 17-Dec-15 1:34am
v3
Comments
Richard MacCutchan 17-Dec-15 7:37am    
Use your debugger to capture the error output.

1 solution

Its hard to help you without seeing your LogCat exception message, better you can this github project which will suit your need:
CalendarDialogBuilder[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900