MainActivity.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="com.example.bhaumik.datepicker.MainActivity">
<TextView
android:id="@+id/tv_your_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Date"
android:textSize="24dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:textColor="@android:color/background_dark"/>
<Button
android:id="@+id/btn_select_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Select Date"
android:layout_marginTop="20dp"
android:textAllCaps="false"
/>
</LinearLayout>
MainActivity.java
package com.example.bhaumik.datepicker;
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_your_date);
button = (Button) findViewById(R.id.btn_select_date);
Calendar calendar = Calendar.getInstance();
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int years, int months, int days) {
String dates = String.valueOf(days + "/" + (months + 1) + "/" + years);
textView.setText(dates);
}
},year,month,day);
datePickerDialog.show();
}
});
}
}
0 comments:
Post a Comment