Free Download Software and Android Tutorials

Friday, August 28, 2015

Basic tutorial Android studio: take a date with DatePicker

Android Studio tutorial will discuss about the DatePicker which is one of the important components of an android application to display data date. If the previous articles I have discussed about Spinner components and how to retrieve data and then display it via Toast, so this time we will take the data date using the DatePicker component.

For more details, we just practice to create a new project with the name of the DatePicker then you select Blank Activity. The DatePicker Project will consist of the layout that we give the name activity_main.xml, and a java file that is used to execute commands on the project DatePicker. Java file that we name MainActivity.java. The following source code of each file contained in the project DatePicker.

Source code activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/btnUbahTanggal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ubah Tanggal"/>
    <TextView
        android:id="@+id/lblTanggal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tanggal sekarang yang ada: (Hari-Bulan-Tahun):"
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <TextView
        android:id="@+id/tvTanggal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge"/>
    <DatePicker
        android:id="@+id/dpHasil"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
</LinearLayout>

Source code MainActivity.java

package com.dsoccerupdate.datepicker;

import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tvDisplayDate;
    private DatePicker dpResult;
    private Button btnChangeDate;
    private int tahun;
    private int bulan;
    private int hari;
    static final int DATE_DIALOG_ID = 999;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setCurrentDateOnView();
        tambahkanListenerkeButton();
    }

    //display current date
    public void setCurrentDateOnView() {
        tvDisplayDate = (TextView) findViewById(R.id.tvTanggal);
        dpResult = (DatePicker) findViewById(R.id.dpHasil);
        final Calendar c = Calendar.getInstance();
        tahun = c.get(Calendar.YEAR);
        bulan = c.get(Calendar.MONTH);
        hari = c.get(Calendar.DAY_OF_MONTH);
        //mengeset tanggal sekarang ke textview
        tvDisplayDate.setText(new StringBuilder()
                .append(hari).append("-").append(bulan + 1).append("-").append("-").append(tahun).append(""));
        dpResult.init(tahun, bulan, hari, null);
    }

    public void tambahkanListenerkeButton() {
        btnChangeDate = (Button) findViewById(R.id.btnUbahTanggal);
        btnChangeDate.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this, datePickerListener, tahun, bulan, hari);
        }
        return null;
    }
    private DatePickerDialog.OnDateSetListener datePickerListener
            = new DatePickerDialog.OnDateSetListener(){

    public void onDateSet(DatePicker view, int selectedYear,
                          int selectedMonth, int selectedDay) {
        tahun = selectedYear;
        bulan = selectedMonth;
        hari = selectedDay;
        tvDisplayDate.setText(new StringBuilder().append(hari).append("-").append(bulan + 1).append("-").append("-").append(tahun).append(""));
        dpResult.init(tahun, bulan, hari, null);
    }
    };
}

DatePicker Project Preview

Basic tutorial Android studio: take a date with DatePicker

Basic tutorial Android studio: take a date with DatePicker

DatePicker Project Download

For those of you who want to download the source code project can DatePicker through the link I included below.

DOWNLOAD

Similarly, android studio tutorial take a date with DatePicker, waiting for the next article will discuss about taking the time to use the TimePicker. Hopefully this simple article can provide benefits for all the people who are learning android programming language. 

Basic tutorial Android studio: take a date with DatePicker Rating: 4.5 Diposkan Oleh: Unknown

1 comments:

  1. There's SHOCKING news in the sports betting industry.

    It has been said that every bettor must watch this,

    Watch this or quit placing bets on sports...

    Sports Cash System - Advanced Sports Betting Software

    ReplyDelete