Programming | Android | Tips N Tricks

Post Page Advertisement [Top]


    

Xml Layout for this Example:
(main.xml)
--------------------------------->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="datepicker.com.datepicker.MainActivity">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="30dp"
        tools:layout_editor_absoluteY="16dp"
        android:datePickerMode="spinner"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/select"
        android:onClick="click_select"
        android:text="select date"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/showdate"/>

</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java (THe Java File)

package dateselspiner.com.dateselectionspinner;

import android.content.Intent;
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 {

    DatePicker datep;
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        datep = (DatePicker)findViewById(R.id.datePicker2);
        text = (TextView)findViewById(R.id.textView);
        text.setText("");
    }


    public void showDate(View V)
    {
        text.setText(getdate());
    }

    public String getdate()
    {
        String date_text= "";
        date_text = "" + datep.getDayOfMonth() +""+(datep.getMonth()+1)+ "" + datep.getYear() + "" ;
        return date_text;
    }

    public void next_page(View view)
    {
       Intent in = new Intent(this,secondPage.class);
       startActivity(in);
    }
}

Copy Above Code And Run your application. 
Enjoy Codding.

No comments:

Post a Comment

Comments

Bottom Ad [Post Page]

| Designed by Colorlib