0

I am using Eclipse to build an Android Application, I have one Activity which has 2 EditTexts and when I click on them, they will redirect me to the other activity which I will then select a date from CalendarView and time from TimePicker. Afterwards, I will click on Done and it will redirect me back to the previous Activity with the EditText filled.

Now, my question is, how do I populate the EditText with the date and time selected from the CalendarView and TimePicker in dd/MMM/yyyy and 24hr format?? I tried using Intent but I don't know how to code in.

Here's my partial code:

public class RentStartActivity extends Activity {
CalendarView calendar1;
TimePicker time1;
Button btnCancel1, btnDone1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rent_start);
    btnCancel1 = (Button) findViewById(R.id.btnCancel1);
    btnCancel1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent cancel = new Intent(RentStartActivity.this, SearchActivity.class);
            startActivity(cancel);
        }
    });

    btnDone1 = (Button) findViewById(R.id.btnDone1);
    btnDone1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent done = new Intent(RentStartActivity.this, SearchActivity.class);
            done.putExtra("Date1", calendar1.getDate());
            done.putExtra("Time", time1.getDrawingTime());
            startActivity(done);
        }
    });
}
Justin
  • 45
  • 6
  • Firstly confirm that Date and Time values getting correctly. After that in your SearchActivity get data using intent. String date= getIntent().getStringExtra("Date1"); String time= getIntent().getStringExtra("Time"); – Anu Roy May 22 '17 at 08:37

2 Answers2

0

You can use startActivityForResult() and onActivityResult() to send data between activities.

Check out this links: How to manage `startActivityForResult` on Android?

https://developer.android.com/training/basics/intents/result.html

Community
  • 1
  • 1
vanna
  • 949
  • 2
  • 9
  • 16
0

Use startActivityForResult() in your SearchActivity:

    Intent i = new Intent(this, RentStartActivity.class);
    startActivityForResult(i, 1);

in your RentStartActivity:

btnDone1 = (Button) findViewById(R.id.btnDone1);
btnDone1.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
        Intent done = new Intent();
        done.putExtra("Date1", calendar1.getDate());
        done.putExtra("Time", time1.getDrawingTime());
        setResult(Activity.RESULT_OK,done);
        startActivity(done);
        finish();
    }
});

and get the data in SearchActivity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String date = data.getStringExtra("Date1");
            String time = data.getStringExtra("Time");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}

To format date time You can visit here.

Community
  • 1
  • 1
Junaid Fahad
  • 149
  • 7