Android tutorial for beginner part 8, Calendar | অ্যান্ড্রয়েড বাংলা টিউটোরিয়াল,

  • 23 April 20
  • Posted By : Admin
  • 52 Hits
  • 10 Comment

How to create Calendar

}

Java code

Paste below in MainActivity.java file

package com.salam.calendar; import androidx.appcompat.app.AppCompatActivity; import android.app.DatePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; public class MainActivity extends AppCompatActivity { public EditText editText; Calendar mCalender = Calendar.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.edittext); final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { mCalender.set(Calendar.YEAR, i); mCalender.set(Calendar.MONTH,i1); mCalender.set(Calendar.DAY_OF_MONTH,i2); updateedittext(); } }; editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new DatePickerDialog(MainActivity.this, date, mCalender.get(Calendar.YEAR), mCalender.get(Calendar.MONTH), mCalender.get(Calendar.DAY_OF_MONTH)).show(); } }); } private void updateedittext() { String format = "MM/dd/yy"; SimpleDateFormat sdf = new SimpleDateFormat(format , Locale.US); editText.setText(sdf.format(mCalender.getTime())); } }

Create xml file

Paste below code in activity_main.xml file

<?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" tools:context=".MainActivity" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Calendar project" android:layout_marginTop="40dp" android:textSize="40sp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="m/d/y" android:id="@+id/edittext" android:clickable="true" android:editable="false" android:layout_marginTop="30dp" /> </LinearLayout>