0

I did everything when it comes to creating notification on selected time in timepicker but I cannot pass it to textview in another activity. Can someone help me with this?

I want to pass time value from SecondActivity to textView named textviewAlarm in HomeActivity when clicking button Done.

public class Second extends AppCompatActivity implements View.OnClickListener{

    private int notificationId = 1;


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

        // Set Onclick Listener.
        findViewById(R.id.setBtn).setOnClickListener(this);
        findViewById(R.id.cancelBtn).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        EditText editText = findViewById(R.id.editText);
        TimePicker timePicker = findViewById(R.id.timePicker);

        // Set notificationId & text.
        Intent intent = new Intent(Second.this, AlarmReceiver.class);
        intent.putExtra("notificationId", notificationId);
        intent.putExtra("todo", editText.getText().toString());

        // getBroadcast(context, requestCode, intent, flags)
        PendingIntent alarmIntent = PendingIntent.getBroadcast(Second.this, 0,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);

        switch (view.getId()) {
            case R.id.setBtn:
                int hour = timePicker.getCurrentHour();
                int minute = timePicker.getCurrentMinute();

                // Create time.
                Calendar startTime = Calendar.getInstance();
                startTime.set(Calendar.HOUR_OF_DAY, hour);
                startTime.set(Calendar.MINUTE, minute);
                startTime.set(Calendar.SECOND, 0);
                long alarmStartTime = startTime.getTimeInMillis();

                // Set alarm.
                // set(type, milliseconds, intent)
                alarm.set(AlarmManager.RTC_WAKEUP, alarmStartTime, alarmIntent);


                // Get TextView object which is used to show user pick date and time.
                TextView textView = (TextView)findViewById(R.id.textViewAlarm);

                StringBuffer strBuffer = new StringBuffer();
                strBuffer.append("You selected date time : ");
                strBuffer.append(this.notificationId);

                textView.setText(strBuffer.toString());

                break;

            case R.id.cancelBtn:
                alarm.cancel(alarmIntent);
                Toast.makeText(this, "Canceled.", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

I want my selected time to be shown in textViewAlarm in HomeActivity.

  • 4
    Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – jseashell Jun 11 '19 at 20:31

1 Answers1

0

You can get the time from the time picker by adding a

timePicker.setOnTimeChangedListener( new TimePicker.onTimeChangedListener() {

            @Override
                public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                    time = String.valueOf(hourOfDay).toString() + ":" + String.valueOf(minute).toString();
                }

}

When the user changes the time it will assign the time to a variable and pass that argument through intent as follow

Intent intent = new Intent(Second.this, AlarmReceiver.class);
intent.putExtra("notificationId", notificationId);
intent.putExtra("time", time);

Take that passed argument from the HomeActivity and set it to the TextView as follow

textviewAlarm.setText(time.toSring());
Udara Abeythilake
  • 1,215
  • 1
  • 20
  • 31