MEMBUAT APLIKASI KALENDAR DI ANDROID STUDIO

1. Pada Android Studio, buatlah project baru.
 => Jika Anda telah membuka project, dari menu File, pilih New Project.

2. Pada Configure your new project, isi kolom berikut seperti di bawah ini.
 => Untuk Minimum SDK, pilih API 15: Android 4.3.0 (Icecream sandwich). klik Next.
x
     => Jika Anda tidak memiliki sebuah project, di layar Welcome klik New Project.

     => Application name ⇒ TimePicker
     => Company domain ⇒ example.com (Misalnya: com.megadistudio.TimePicker)
     => Project Location ⇒ memilih lokasi untuk project Anda.

     



    3. Pada Select the form factors your app will run oncentang untuk Phone and Tablet.


    4. Pada Add an activity to Mobile, pilih Blank Activity dan klik Next.


    Next ->


    5. Klik tombol Finish untuk membuat project.
    6. Pilih View Anda sebagai Project, Buka file layout untuk kegiatan utama (activity_main.xml) terletak di bawah App  res ⇒ layout.

    activity_main.xml 

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="timepicker.megadistudio.com.timepicker.MainActivity">


        <TextView
            android:text="@string/tk"
            android:textSize="50sp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/textView2" />

        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            app:srcCompat="@drawable/tk"
            android:layout_below="@+id/textView2"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:id="@+id/imageView" />

        <TextView
            android:text=""
            android:textStyle="bold"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView"
            android:layout_alignLeft="@+id/textView2"
            android:layout_alignStart="@+id/textView2"
            android:layout_marginLeft="41dp"
            android:layout_marginStart="41dp"
            android:layout_marginTop="66dp"
            android:id="@+id/txtDate" />

        <Button
            android:text="Waktu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonTime"
            android:layout_alignBaseline="@+id/txtTime"
            android:layout_alignBottom="@+id/txtTime"
            android:layout_alignLeft="@+id/buttonDate"
            android:layout_alignStart="@+id/buttonDate"
            android:textAllCaps="false" />

        <TextView
            android:text=""
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="21dp"
            android:id="@+id/txtTime"
            android:layout_below="@+id/buttonDate"
            android:layout_alignLeft="@+id/txtDate"
            android:layout_alignStart="@+id/txtDate" />

        <Button
            android:text="Tanggal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/buttonDate"
            android:layout_alignBaseline="@+id/txtDate"
            android:layout_alignBottom="@+id/txtDate"
            android:layout_alignRight="@+id/textView2"
            android:layout_alignEnd="@+id/textView2"
            android:layout_marginRight="39dp"
            android:layout_marginEnd="39dp"
            android:textAllCaps="false" />

    </RelativeLayout>

    string.xml 

    <resources>
        <string name="app_name">TimePicker</string>
        <string name="tk">Tahu Koding</string>
    </resources>

    MainActivity.java  

    package timepicker.megadistudio.com.timepicker;

    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.TimePickerDialog;
    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;
    import android.widget.TimePicker;

    import java.util.Calendar;

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

        private Button btnDate, btnTime;
        private TextView txtDate, txtTime;
        private int mTahun, mBulan, mHari, mJam, mMenit;

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

            btnDate = (Button) findViewById(R.id.buttonDate);
            btnTime = (Button) findViewById(R.id.buttonTime);
            txtDate =(TextView) findViewById(R.id.txtDate);
            txtTime =(TextView) findViewById(R.id.txtTime);

            btnDate.setOnClickListener(this);
            btnTime.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v == btnDate) {

                final Calendar c = Calendar.getInstance();
                mTahun = c.get(Calendar.YEAR);
                mBulan = c.get(Calendar.MONTH);
                mHari = c.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener(){

                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                        txtDate.setText(dayOfMonth + "-" + (month + 1) + "-" + year);

                    }
                },mTahun,mBulan,mHari);

                datePickerDialog.show();

            }

            if(v == btnTime){

                final Calendar c = Calendar.getInstance();
                mJam = c.get(Calendar.HOUR_OF_DAY);
                mMenit = c.get(Calendar.MINUTE);

                TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener(){

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        txtTime.setText(hourOfDay + ":" + minute);
                    }
                },mJam,mMenit, false);

                timePickerDialog.show();

            }

        }
    }

    Berikut Hasilnya



    Demikian cara membuat aplikasi Kalendar di Android. Jika ada kekurangan mohon komentarnya. Terima Kasih 
    Wassalamualaikum 

    0 komentar