Calender.getInstance() gives error in Android Studio
Asked Answered
H

3

39

I'm trying to pop-open a DatePicker dialogue box when my EditText is clicked. The problem is, when I try to instantiate my Calender object with Calender.getInstance() the creepy-red-underline appears below getInstance().

Calender myCalender = Calender.getInstance();

Hover over it and it says

Call requires API level 24(current minimum is 15).

Obviously I expect my app to run on devices at the minimum API level 15. Please help me with this, I'm new to Android. Thank you.

I tried to follow this method to add a DatePicker to my EditText

Hassett answered 10/7, 2016 at 10:51 Comment(2)
Which Calendar do you use?Pentheus
The Internationalization guide explains what ICU is and how to use it for <= API 23 (6.0) vs API 24 (7.0) onwardsKatheleenkatherin
S
132

You have the wrong import statement for Calendar. It needs to be java.util.Calendar. My guess is that you have an import for android.icu.util.Calendar.

Stoltzfus answered 10/7, 2016 at 10:55 Comment(0)
R
0
This code will help  you to solve your problem..
It will pop-open a DatePicker dialogue box when onClick EditText box:

1) MainActivity.java

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.icu.text.DateFormat;
import android.icu.util.Calendar; 
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity
{
    EditText editText1;
    private int nYear,nMonth,nDay,sYear,sMonth,sDay;
    static final int DATE_ID = 0;
    Calendar c = Calendar.getInstance();

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

        editText1 = (EditText) findViewById(R.id.editText1);
        sYear = c.get(Calendar.YEAR);
        sMonth = c.get(Calendar.MONTH);
        sDay = c.get(Calendar.DAY_OF_MONTH);
        //editText
        editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DATE_ID);
            }
        });

    /***************calender code*****************/
    private void calender_fetch()
    {
        editText1.setText((nMonth+1)+"/"+nDay+"/"+nYear+"");
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            nYear = year;
            nMonth = monthOfYear;
            nDay = dayOfMonth;
            calender_fetch();
        }
    };
    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
            case DATE_ID:
                return new DatePickerDialog(this, mDateSetListener, sYear, sMonth, sDay);
        }
        return null;
    }
    /*************************calender close*********************************/
Resign answered 6/12, 2017 at 6:56 Comment(0)
E
-2

use: import java.util.Calendar;

This is the package we need to use to get Calendar on the dialog box, instead of,

instead: import android.icu.util.Calendar;

this works fine.

Eczema answered 4/12, 2016 at 10:35 Comment(1)
Welcome to StackOverflow. Do not copy other members' posts, instead come up with original answers.Revere

© 2022 - 2024 — McMap. All rights reserved.