DatePicker and DatePickerDialog
This DatePicker and DateTimePicker is used to select Date from Android App.
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class Second extends AppCompatActivity {
TextView tv;
int y,m,d;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv = findViewById(R.id.textdata);
Calendar c=Calendar.getInstance();
y=c.get(Calendar.YEAR);
m=c.get(Calendar.MONTH);
d=c.get(Calendar.DATE);
}
public void getDate(View v) {
showDialog(123);
tv.setText(y+"-"+(m+1)+"-"+d);
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == 123) {
return new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
showDate(i,i1,i2);
}
}, y+0, m+0, d+0);
}
return null;
}
private void showDate(int year, int month, int day) {
tv.setText(year + "-" + (month+1) + "-" + day);
}
}
< ?xml version="1.0" encoding="utf-8"?>
< RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set Date Here"
android:padding="20dp"
android:gravity="center"
android:textSize="20sp"
android:id="@+id/textdata"/>
< Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Select Date"
android:onClick="getDate"
/>
< /RelativeLayout>
No comments:
Post a Comment