Android: Change Year directly in material-calendarview
Asked Answered
E

2

6

I am using https://github.com/prolificinteractive/material-calendarview

Right now it is changing the month on the click of Right or Left arrow
How can i change the year directly instead of changing month.
Is this possible android default Calendar View. Below is my code for prolificinteractive - calendar

public class ActivityCalendar extends AppCompatActivity implements OnDateSelectedListener, OnMonthChangedListener {

    private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();
    private MaterialCalendarView widget;
    private String zodiacSign = "";
    private HashMap<String,Integer> hashMapMonth =  new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_calendar);
        widget = (MaterialCalendarView)findViewById(R.id.calendarView);
        widget.setOnDateChangedListener(this);
        widget.setOnMonthChangedListener(this);

    }

    @Override
    public void onDateSelected(@NonNull MaterialCalendarView widget, @Nullable CalendarDay date, boolean selected) {
        String strDate = getSelectedDatesString();
        Intent returnIntent = new Intent();
        returnIntent.putExtra(TagClass.BIRTH_DATE, strDate);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }


    @Override
    public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
        //noinspection ConstantConditions

    }

    private String getSelectedDatesString() {
        CalendarDay date = widget.getSelectedDate();
        if (date == null) {
            return "No Selection";
        }
        return FORMATTER.format(date.getDate());
    }
}

Layout.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityCalendar">


    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


</LinearLayout>
Elfredaelfrida answered 31/12, 2015 at 14:48 Comment(1)
Did you find a solution to your problem?Depilatory
S
3

That doesn't seem to be possible with that library as it is right now.

In order order to do it, you would need to download the sources from github, and add it to your project as a library module.

Then, I would expect changes to be needed in at least these components:

Also, my impression is that doing these changes would also cause a mayor refactor in the library.

Spaak answered 29/2, 2016 at 13:30 Comment(0)
F
0

As of now this library doesn't have year selection but you can do something like I did and it'll show calendar with year filter.

1) Prepare some list of years, you can use below method, it'll give you 20 years of list, 10 years from past and 10 years of future.

private fun getListOfYears(): MutableList<Int> {
    val cal = Calendar.getInstance()
    cal.add(Calendar.YEAR, -10)
    val tenYearBack = cal.get(Calendar.YEAR)
    Log.i("yearList", "tenYearBack $tenYearBack")
    val nCal = Calendar.getInstance()
    nCal.add(Calendar.YEAR, 10)
    val tenYearAfter = nCal.get(Calendar.YEAR)
    Log.i("yearList", "tenYearAfter $tenYearAfter")
    val listOfYears = mutableListOf<Int>()
    for (i in tenYearBack..tenYearAfter) {
        listOfYears.add(i)
    }
    listOfYears.forEach {
        Log.i("yearList", "listOfYears $it")

    }
    return listOfYears

}

2) Now add one spinner to your layout and in arrayAdapter of spinner pass this list something like this:

 val yearAdapter = ArrayAdapter<Int>(context, R.layout.year_spinner_item, listOfYears)

3) Lastly, call this method (user defined) with the selected year value(i.e,2019) in onItemSeleted() method to change year of the calendarView. (Note: in below method calendarView refers to CalendarView widget).

 override fun onItemSelected(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) {
     selectedYear=listOfYears?.get(position)
    Utils.log("Selected Year is: $selectedYear")
    showSelectedYearCalendar(selectedYear)
}

private fun showSelectedYearCalendar(selectedYear: Int?) {

    val cal=Calendar.getInstance()
    cal.set(Calendar.YEAR,selectedYear!!)
    cal.set(Calendar.MONTH,0)

    val maxCal=Calendar.getInstance()
    maxCal.set(Calendar.YEAR,selectedYear)
    maxCal.set(Calendar.MONTH,11)

    calendarView.state().edit()
        .setMinimumDate(cal)
        .setMaximumDate(maxCal)
        .commit()
}
Fp answered 5/12, 2019 at 13:55 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.