Latest News

Date Picker Obrolan - Android Studio

How to use Date Picker Dialog?

In this tutorial you will learn how to use Date Picker Dialog to pick date and use it anywhere you want. We will show Date Picker Dialog on a Button click and save the picked date to a TextView.

Step 1: Create a new project OR Open your project

Step 2: Code

activity_main.xml
<LinearLayout 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:orientation="vertical"android:padding="10dp"tools:context=".MainActivity" > <Button android:id="@+id/btnPickDate" android:text="Pick Date" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvdate" android:text="Pick Date" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>

MainActivity.java
package com.blogspot.devofandroid.myapplication;import android.app.DatePickerDialog;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;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 {Calendar mClndr;DatePickerDialog dpd;Button mBtnPickDate;TextView mTvDate;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTvDate = findViewById(R.id.tvdate);mBtnPickDate = findViewById(R.id.btnPickDate);mBtnPickDate.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {mClndr = Calendar.getInstance();int day = mClndr.get(Calendar.DAY_OF_MONTH);int month = mClndr.get(Calendar.MONTH);int year = mClndr.get(Calendar.YEAR);dpd = new DatePickerDialog(MainActivity.this , new DatePickerDialog.OnDateSetListener() {@Overridepublic void onDateSet(DatePicker datePicker , int mYear , int mMonth , int mDay) {mTvDate.setText(mDay + "/" + (mMonth+1) + "/" + mYear);}} , year , month , day);dpd.show();}});}}

Step 3: Output

  


0 Response to "Date Picker Obrolan - Android Studio"