0

I am an amateur android app developer. Can someone please send me the code in which, when I click on a button, it detects what day it is (Sunday, Monday, Tuesday, etc) and takes me to different activities based on the day.

eg. If it is Tuesday, and I click on my button1, It takes me to Activity 3. If it is Wednesday, and I click button1, It takes me to Activity 5. If it is Friday, and I click button1, It takes me to Activity 6.

Thank you, Your help will be really appreciated.

2 Answers2

0
  Button btn=(Button) findViewById(R.id.alert_btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar calendar=Calendar.getInstance();
            SimpleDateFormat sdf=new SimpleDateFormat("EEEE");

            switch (sdf.format(calendar.getTime())){
                case "Saturday":
                    startActivity(new Intent(MainActivity.this,Activity0.class));
                    break;
                case "Sunday":
                    startActivity(new Intent(MainActivity.this,Activity1.class));
                    break;
                case "Monday":
                    startActivity(new Intent(MainActivity.this,Activity2.class));
                    break;
                case "Tuesday":
                    startActivity(new Intent(MainActivity.this,Activity3.class));
                    break;
            }

        }
    });
amir
  • 16
  • 3
0

You can use a Calendar class to check the day_of_week and use the Intent for moving to next Activity in android. Calendar.DAY_OF_WEEK will give values between 1-7 i.e(Sunday to Saturday). Check this code to solve your problem

 b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Calendar cal = new GregorianCalendar();
                int day_of_week=cal.get(Calendar.DAY_OF_WEEK);
                 Intent in;
                //Use switch case to check day
                switch (day_of_week)
                {
                    case 1:
                         in=new Intent(YourCurrentActivity.this,NextActivity.class);
                        startActivity(in);
                        break;
                    case 2:
                        //make such cases and change your next Activity as you want
                }

            }
        });
  • Thank You So Much, Really Big Help! – Denver Saldanha May 11 '20 at 15:40
  • @DenverSaldanha Welcome Denver !! . Please mark my answer as accepted if it solved the problem you asked. – Jasvir Singh May 11 '20 at 15:47
  • In the answer above Yours, the guy has set cases such as Saturday, Monday, Tuesday, etc hence I know which activity goes where. How do I know where to put which activity in your answer, please mention!! I am a real noob so please guide me by editing your answer with "days" instead of "cases" – Denver Saldanha May 11 '20 at 16:01
  • In this '1' is for Sunday and similarly 2-Monday, 3-Tuesday, 4-Wednesday, 5-Thursday, 6-Friday, 7-Saturday. @DenverSaldanha – Jasvir Singh May 11 '20 at 16:11
  • Cool I checked it out but, I am getting an error in case 2 the word ''in'' it says the variable ''in'' is already defined in the scope. – Denver Saldanha May 11 '20 at 16:15
  • okay.. I edited the code now. Check it. I moved the declaration of 'in' outside switch. It will solve your problem @DenverSaldanha – Jasvir Singh May 11 '20 at 16:24
  • I changed the code and now the app is crashing, I put the code that I implemented in the question, Please let me know where the error is. – Denver Saldanha May 11 '20 at 18:28
  • Share your code so that i can know the error @DenverSaldanha – Jasvir Singh May 12 '20 at 11:07