0

I am new to android.

I am using a CalendarPickerView as an activity to select a range of dates.

All I want to do is to is to pass the first and the last day selected to a textview in another activity.

Right now I have a CalendarPickerActivity which you can see below (the code) then I have another activity called EventFiltersActivity where I have a TextView which is supposed to show the first day and the last day selected

Thanks already for your patience and help.

public class CalendarPickerActivity extends AppCompatActivity {
    private CalendarPickerView calendar;
    Button saveButton;
    Button clearButton;

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

        saveButton = (Button) findViewById(R.id.save_button);
        clearButton = (Button) findViewById(R.id.resetta_button);

        final Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.YEAR, 10);

        final Calendar lastYear = Calendar.getInstance();
        lastYear.add(Calendar.YEAR, -10);

        calendar = (CalendarPickerView) findViewById(R.id.calendar_picker);
        final ArrayList<Integer> list = new ArrayList<>();
        list.add(2);

        calendar.getSelectedDates();


        calendar.init(lastYear.getTime(), nextYear.getTime()) //
                .inMode(CalendarPickerView.SelectionMode.RANGE)
                .withSelectedDate(Calendar.getInstance().getTime());

        //.withDeactivateDates(list);
        //.withHighlightedDates(arrayList);

        calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                Toast.makeText(getApplicationContext(), "Selected Date is : " + date.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), EventFiltersActivity.class);
                calendar.getSelectedDates();
            }
        });
    }
    }
  • Read this for more details https://developer.android.com/reference/android/content/Intent – Mayur Patel Aug 28 '19 at 12:42
  • 2
    Possible duplicate of [How to send an object from one Android Activity to another using Intents?](https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – Mayur Patel Aug 28 '19 at 12:44
  • Another possible duplication https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – MrVasilev Aug 28 '19 at 12:48
  • If you have interest you can check MVVM in android and pass data with shared ViewModel, but the Intent putExtra() and both possible duplication above are better for a new Android Developer – MrVasilev Aug 28 '19 at 12:50
  • @MayurPatel thank you for the link, I had checked the documentation earlier but could not find a solution – bdiplastic Aug 28 '19 at 14:02
  • @bdiplastic I think it is not hard to understand – Mayur Patel Aug 28 '19 at 14:35

3 Answers3

1

Make Global Variable Selecteddate

String selectedDate;

Now assign value to Global Variable.

calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
    @Override
    public void onDateSelected(Date date) {
        selectedDate = date.toString();
    }

    @Override
    public void onDateUnselected(Date date) {

    }
});

And pass it to intent.

saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), EventFiltersActivity.class);
                i.putExtra("date", selectedDate);
                startActivity(i);
            }
        });

In EventFiltersActivity :

String getDate = getIntent().getStringExtra("date");
Ashish
  • 6,791
  • 3
  • 26
  • 48
0

You can use sending parameters using an intent

On you calendar activity

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(),EventFiltersActivity.class);
                   i.putExtra("selectedDate",calendar.getSelectedDates().toString());
                   startActivity(i);
        }
    });

On your second Activtiy in oncreateOptionsmenu

  Intent ii = getIntent();
  string getDate = ii.getStringExtra("selectedDate");
  TextView tv = (TextView)findViewBId(R.id.textViewId);
  tv.setText(getDate);
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
0

// set global variables

CalendarPickerView calProfileCalendar;
Button mRequestButton;
Date dateBeginningDate;
String dateBeginningString;
Date dateEndingDate;
String dateEndingString;

onCreate set button and calendar

calProfileCalendar = findViewById(R.id.calendar_view);
mRequestButton = findViewById(R.id.requestCalButtonId);
mRequestButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    dateBeginningDate =  calProfileCalendar.getSelectedDates().get(0);
                    dateBeginningString = formatter.format(dateBeginningDate);
                    dateEndingDate = calProfileCalendar.getSelectedDates().get(calProfileCalendar.getSelectedDates().size() - 1);
                    dateEndingString = formatter.format(dateEndingDate);

                    //set information to pass to new intent
                    Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
                    intent.putExtra("beginningDateString", dateBeginningString);
                    intent.putExtra("dateEndingString", dateEndingString);
                    startActivity(intent);

                }
            });

Nextactivity.java // in onCreate

getIncomingIntent();
private void getIncomingIntent() {

            calDateBeginningString = (String) getIntent().getExtras().get("calBeginningDate");
            calDateEndingString = (String) getIntent().getExtras().get("calEndingDate");

    }
Cflux
  • 1,423
  • 3
  • 19
  • 39