0

This is what my DatePicker looks like in my MainActivity2

public class MainActivity2 extends ActionBarActivity {
    Button btn;
    int year_x, month_x, day_x;
    static final int DIALOG_ID = 0;

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

        final Calendar cal = Calendar.getInstance();
        year_x = cal.get(Calendar.YEAR);
        month_x = cal.get(Calendar.MONTH);
        day_x = cal.get(Calendar.DAY_OF_MONTH);

        showDialogOnButtonClick();
    }

    public void showDialogOnButtonClick(){
        btn = (Button)findViewById(R.id.MyButton2);

        btn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDialog(DIALOG_ID);
                    }
                }
        );
    }

    @Override
    protected Dialog onCreateDialog(int id){
        if (id == DIALOG_ID)
            return new DatePickerDialog(this, dpickerListner ,year_x,month_x,day_x);
        return null;
    }

    private DatePickerDialog.OnDateSetListener dpickerListner
            =new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            year_x = i;
            month_x = i1 +1;
            day_x = i2;
            Toast.makeText(MainActivity2.this,year_x + "/" + month_x + "/" +day_x,Toast.LENGTH_LONG).show();
        }
    };

}

and this is what my MainActivity looks like

    public class MainActivity extends Activity {

        private Button button;

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

            Button next = (Button) findViewById(R.id.MyButton);
            next.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), MainActivity2.class);
                    startActivityForResult(myIntent, 0);
                }
            });

    }
}

the button on the first page sends the user to the second page so they may set the date they want with the date picker. How do I connect the two pages so they can interact with each other?

Huck
  • 1

2 Answers2

0

Seeing that you're using startActivityForResult() to start your second activity I supose that you need the second activity to comunicate the date back to your main activity. To achieve this you could take a look at setResult() and onActivityResult(). This is quite well explained here.

Community
  • 1
  • 1
0

Use Intent for passing date from one activity to another

Ankit Tale
  • 1,924
  • 4
  • 17
  • 30