Wizard Book


Programming Python, Web, Android

Android: Membuat DatePicker Calendar untuk Memilih Tanggal

Membuat DatePicker untuk Memilih Tanggal Dengan Calendar pada Form Input Android Native

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
 
public class MainActivity extends AppCompatActivity {
 
    private DatePickerDialog datePickerDialog;
    private SimpleDateFormat dateFormatter;
    private EditText editTanggal;
    private Button buttonTanggal;
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
 
        editTanggal = (EditText) findViewById(R.id.editTanggal);
        buttonTanggal = (Button) findViewById(R.id.buttonTanggal);
        buttonTanggal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDateDialog();
            }
        });
 
    }
 
    private void showDateDialog(){
 
        Calendar newCalendar = Calendar.getInstance();
 
        datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
 
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

			Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
 
                editTanggal.setText(dateFormatter.format(newDate.getTime()));
            }
 
        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
 
        datePickerDialog.show();
    }
}

email : contohprogram.com@gmail.com
WA : +6289671400363