package com.example.mypickerviews;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {
    DateFormat form=DateFormat.getDateTimeInstance();
//	DateFormat form = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");

    Calendar calnd=Calendar.getInstance();
    TextView window;
    Button btn;
    
    DatePickerDialog.OnDateSetListener dat=new DatePickerDialog.OnDateSetListener() {
    	public void onDateSet(DatePicker view, int year,int month, int day) {
    		calnd.set(Calendar.YEAR, year);
    		calnd.set(Calendar.MONTH,month);
    		calnd.set(Calendar.DAY_OF_MONTH,day);
    		updateWindow();
    	}
    };
    
    TimePickerDialog.OnTimeSetListener tim=new TimePickerDialog.OnTimeSetListener() {
    	public void onTimeSet(TimePicker view, int hour,int minute) {
    		calnd.set(Calendar.HOUR_OF_DAY, hour);
    		calnd.set(Calendar.MINUTE,minute);
    		updateWindow();
    	}
    };	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btn=(Button)findViewById(R.id.btn1);
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				new DatePickerDialog(MainActivity.this, dat, calnd.get(Calendar.YEAR), calnd.get(Calendar.MONTH), calnd.get(Calendar.DAY_OF_MONTH)).show();
			}
		});
		
		btn=(Button) findViewById(R.id.btn2);
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				new TimePickerDialog(MainActivity.this, tim, calnd.get(Calendar.HOUR_OF_DAY), calnd.get(Calendar.MINUTE), true).show();
			}
		});
		window = (TextView)findViewById(R.id.textView1);
		updateWindow();
	}
	
	private void updateWindow() {
		window.setText(form.format(calnd.getTime()));
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
