info@thistimebd.com

Sunday 24th of November 05:14:56am

Date Picker Android app Studio Source Code

package com.thistimebd.web.datepicker;


import android.annotation.SuppressLint;

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;


public class MainActivity extends AppCompatActivity {

    private TextView textView;

    private DatePicker datePicker;

    private Button selectButton;



    @SuppressLint("WrongViewCast")

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        textView=(TextView) findViewById(R.id.texViewId);

        datePicker= (DatePicker) findViewById(R.id.datePickerId);

        selectButton=(Button) findViewById(R.id.buttonId);


        textView.setText(currentDate());


        selectButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                textView.setText(currentDate());

            }

        });

    }


    String currentDate(){

        StringBuilder stringBuilder=new StringBuilder();


        stringBuilder.append("Current Date: ");

        stringBuilder.append(datePicker.getDayOfMonth()+"/");

        stringBuilder.append((datePicker.getMonth()+1)+"/");

        stringBuilder.append(datePicker.getYear());

        return stringBuilder.toString();


    }

}

===============================================

activity_main.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"

    android:layout_margin="10dp"

    android:gravity="center"

    tools:context="com.thistimebd.web.datepicker.MainActivity">


    <TextView

        android:id="@+id/texViewId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Current Date"

        android:textSize="20sp"

        android:textStyle="bold"

        android:layout_marginBottom="20dp"

        />

<Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="select date"

    android:id="@+id/buttonId"

    android:textStyle="bold"

    android:textSize="20sp"

    android:layout_marginBottom="10dp"


    />


    <DatePicker

        android:id="@+id/datePickerId"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">



    </DatePicker>



</LinearLayout>