Implement date picker and time picker in button click and store in edit text boxes
Posted
by
user3597791
on Stack Overflow
See other posts from Stack Overflow
or by user3597791
Published on 2014-08-25T16:16:19Z
Indexed on
2014/08/25
16:19 UTC
Read the original article
Hit count: 390
android
Hi I am trying to implement a date picker and time picker in button click and store in edit text boxes. I have tried numerous things but since i suck at coding I cant get any of them to work. Please find my class and xml below and i would be grateful for any help
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class NewEvent extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private EventHandler handler;
private String picturePath = "";
private String name;
private String place;
private String date;
private String time;
private String photograph;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_event);
handler = new EventHandler(getApplicationContext());
ImageView iv_user_photo = (ImageView) findViewById(R.id.iv_user_photo);
iv_user_photo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
Button btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EditText et_name = (EditText) findViewById(R.id.et_name);
name = et_name.getText().toString();
EditText et_place = (EditText) findViewById(R.id.et_place);
place = et_place.getText().toString();
EditText et_date = (EditText) findViewById(R.id.et_date);
date = et_date.getText().toString();
EditText et_time = (EditText) findViewById(R.id.et_time);
time = et_time.getText().toString();
ImageView iv_photograph = (ImageView) findViewById(R.id.iv_user_photo);
photograph = picturePath;
Event event = new Event();
event.setName(name);
event.setPlace(place);
event.setDate(date);
event.setTime(time);
event.setPhotograph(photograph);
Boolean added = handler.addEventDetails(event);
if(added){
Intent intent = new Intent(NewEvent.this, MainEvent.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "Event data not added. Please try again", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri imageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(imageUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_new_event_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New Event"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Event"
android:layout_alignParentBottom="true" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tv_new_event_title"
android:layout_above="@id/btn_add">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_user_photo"
android:src="@drawable/add_user_icon"
android:layout_width="100dp"
android:layout_height="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event:" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place:" />
<EditText
android:id="@+id/et_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date:" />
<EditText
android:id="@+id/et_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
<Button
android:id="@+id/button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<requestFocus />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time:" />
<EditText
android:id="@+id/et_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="time" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<requestFocus />
</LinearLayout>
</ScrollView>
</RelativeLayout>
© Stack Overflow or respective owner