Date_picker & Time_picker
Step 1:xml & java
Step 1:XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.datepicker_time_picker.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/date"
android:text="select date"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/time"
android:text="select time"/>
</LinearLayout>
STEP 2:JAVA
package demoproject.aalap.com.datepicker_time_picker;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView time1,date1;
int dday,dmonth,dyear,dhour,dmin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time1=(TextView)findViewById(R.id.time);
date1=(TextView)findViewById(R.id.date);
date1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c= Calendar.getInstance();
dday=c.get(Calendar.DAY_OF_MONTH);
dmonth=c.get(Calendar.MONTH);
dyear=c.get(Calendar.YEAR);
DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date1.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
}
}, dyear, dmonth, dday);
dpd.show();
}
});
time1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c=Calendar.getInstance();
dhour=c.get(Calendar.HOUR);
dmin=c.get(Calendar.MINUTE);
TimePickerDialog tpd=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time1.setText(hourOfDay+":"+minute);
}
}, dhour, dmin, false);
tpd.show();
}
});
}
}
Step 1:XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.datepicker_time_picker.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/date"
android:text="select date"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/time"
android:text="select time"/>
</LinearLayout>
STEP 2:JAVA
package demoproject.aalap.com.datepicker_time_picker;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView time1,date1;
int dday,dmonth,dyear,dhour,dmin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time1=(TextView)findViewById(R.id.time);
date1=(TextView)findViewById(R.id.date);
date1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c= Calendar.getInstance();
dday=c.get(Calendar.DAY_OF_MONTH);
dmonth=c.get(Calendar.MONTH);
dyear=c.get(Calendar.YEAR);
DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
date1.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
}
}, dyear, dmonth, dday);
dpd.show();
}
});
time1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar c=Calendar.getInstance();
dhour=c.get(Calendar.HOUR);
dmin=c.get(Calendar.MINUTE);
TimePickerDialog tpd=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time1.setText(hourOfDay+":"+minute);
}
}, dhour, dmin, false);
tpd.show();
}
});
}
}
Comments
Post a Comment