- How to add Date-picker and Time-picker to your app?
MainActivity:
<Your package name>
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
final int DATE_PIC_DIALOG = 999;
final int TIME_PIC_DIALOG = 998;
Button modifyDateBtn, modifyTimeBtn;
TextView dateLbl, timeLbl;
int year, month, day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
modifyDateBtn = (Button)findViewById(R.id.datePickBtn);
modifyDateBtn.setOnClickListener(this);
dateLbl = (TextView)findViewById(R.id.dateLbl);
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
modifyTimeBtn = (Button)findViewById(R.id.timePickBtn);
modifyTimeBtn.setOnClickListener(this);
timeLbl = (TextView)findViewById(R.id.timeLbl);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.datePickBtn)
{
showDialog(DATE_PIC_DIALOG);
}
else if (v.getId() == R.id.timePickBtn)
{
showDialog(TIME_PIC_DIALOG);
}
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
dateLbl.setText(new StringBuilder().append(arg3).append(“/”)
.append(arg2+1).append(“/”).append(arg1));
day = arg3;
month = arg2 + 1;
year = arg1;
}
};
private TimePickerDialog.OnTimeSetListener myTimeListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeLbl.setText(new StringBuilder().append(String.format(“%02d”, hourOfDay)).append(“:”)
.append(String.format(“%02d”, minute)));
}
};
@Override
protected Dialog onCreateDialog(int id) {
if (id == DATE_PIC_DIALOG) {
return new DatePickerDialog(this, myDateListener, year, month, day);
}
else if (id == TIME_PIC_DIALOG) {
return new TimePickerDialog(this, myTimeListener, 21, 30, true);
}
return null;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:id=”@+id/dateLayout”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<TextView
android:id=”@+id/dateLbl”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”The Date”
/>
<Button
android:id=”@+id/datePickBtn”
android:layout_marginLeft=”100dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Select Date”
/>
</LinearLayout>
<LinearLayout
android:id=”@+id/timeLayout”
android:layout_width=”fill_parent”
android:layout_below=”@id/dateLayout”
android:layout_marginTop=”20dp”
android:layout_height=”wrap_content”>
<TextView
android:id=”@+id/timeLbl”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”The Time”
/>
<Button
android:id=”@+id/timePickBtn”
android:layout_marginLeft=”100dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Select Time”
/>
</LinearLayout>
</RelativeLayout>