| 1 | package ro.ieval.unical; |
| 2 | import java.util.Calendar; |
| 3 | import java.util.GregorianCalendar; |
| 4 | |
| 5 | import android.app.ActionBar; |
| 6 | import android.app.Activity; |
| 7 | import android.app.DatePickerDialog.OnDateSetListener; |
| 8 | import android.app.Fragment; |
| 9 | import android.app.FragmentManager; |
| 10 | import android.app.TimePickerDialog.OnTimeSetListener; |
| 11 | import android.os.Bundle; |
| 12 | import android.view.Menu; |
| 13 | import android.view.MenuInflater; |
| 14 | import android.view.MenuItem; |
| 15 | import android.view.View; |
| 16 | import android.widget.CompoundButton; |
| 17 | import android.widget.CompoundButton.OnCheckedChangeListener; |
| 18 | import android.widget.DatePicker; |
| 19 | import android.widget.Spinner; |
| 20 | import android.widget.Switch; |
| 21 | import android.widget.TextView; |
| 22 | import android.widget.TimePicker; |
| 23 | |
| 24 | public final class AddEventActivity extends Activity implements OnDateSetListener, OnTimeSetListener{ |
| 25 | private TextView dateTimeTextView; |
| 26 | |
| 27 | @Override |
| 28 | protected void onCreate(final Bundle savedInstanceState) { |
| 29 | super.onCreate(savedInstanceState); |
| 30 | setContentView(R.layout.add_event); |
| 31 | setTitle(R.string.addeventtitle); |
| 32 | final ActionBar actionBar = getActionBar(); |
| 33 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 34 | final ActionBar.Tab t1 = actionBar.newTab().setText(this.getString(R.string.addevtab1)); |
| 35 | final ActionBar.Tab t2 = actionBar.newTab().setText(this.getString(R.string.addevtab2)); |
| 36 | final ActionBar.Tab t3 = actionBar.newTab().setText(this.getString(R.string.addevtab3)); |
| 37 | |
| 38 | final Fragment f1 = new TrivialFragment(); |
| 39 | final Bundle b1=new Bundle(); |
| 40 | b1.putInt(TrivialFragment.ARGUMENT_LAYOUT, R.layout.add_event_basic_tab); |
| 41 | f1.setArguments(b1); |
| 42 | |
| 43 | final Fragment f2 = new TrivialFragment(); |
| 44 | final Bundle b2=new Bundle(); |
| 45 | b2.putInt(TrivialFragment.ARGUMENT_LAYOUT, R.layout.add_event_details_tab); |
| 46 | f2.setArguments(b2); |
| 47 | |
| 48 | final Fragment f3 = new TrivialFragment(); |
| 49 | final Bundle b3=new Bundle(); |
| 50 | b3.putInt(TrivialFragment.ARGUMENT_LAYOUT, R.layout.add_event_other_tab); |
| 51 | f3.setArguments(b3); |
| 52 | |
| 53 | t1.setTabListener(new AddEventTabListener(f1)); |
| 54 | t2.setTabListener(new AddEventTabListener(f2)); |
| 55 | t3.setTabListener(new AddEventTabListener(f3)); |
| 56 | |
| 57 | actionBar.addTab(t1); |
| 58 | actionBar.addTab(t2); |
| 59 | actionBar.addTab(t3); |
| 60 | |
| 61 | handleViews(f1,f2,f3); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | private void handleViews(Fragment f1,Fragment f2,Fragment f3) { |
| 66 | Switch repeatSwitch = (Switch) this.getFragmentManager().findFragmentById(f1.getId()).getView().findViewById(R.id.repeat_switch); |
| 67 | final Spinner repeatSpinner = (Spinner) this.getFragmentManager().findFragmentById(f1.getId()).getView().findViewById(R.id.repeat_spinner); |
| 68 | repeatSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 69 | |
| 70 | @Override |
| 71 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 72 | if(isChecked) { |
| 73 | repeatSpinner.setEnabled(true); |
| 74 | }else { |
| 75 | repeatSpinner.setEnabled(false); |
| 76 | } |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean onCreateOptionsMenu(final Menu menu) { |
| 83 | final MenuInflater inflater = getMenuInflater(); |
| 84 | inflater.inflate(R.menu.add_event, menu); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean onOptionsItemSelected(final MenuItem item) { |
| 90 | switch (item.getItemId()) { |
| 91 | case R.id.cancel: |
| 92 | finish(); |
| 93 | return true; |
| 94 | default: |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public void showDateTimePickerDialog(final View v) { |
| 100 | dateTimeTextView=(TextView) v; |
| 101 | if(v.getId()==R.id.date_start || v.getId()==R.id.date_end) |
| 102 | new DatePickerFragment().show(getFragmentManager(), "datePicker"); |
| 103 | else |
| 104 | new TimePickerFragment().show(getFragmentManager(), "timePicker"); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onTimeSet(final TimePicker picker, final int hour, final int minute) { |
| 109 | dateTimeTextView.setText(String.format("%2d:%2d",hour,minute)); |
| 110 | if(dateTimeTextView.getId() == R.id.time_start) |
| 111 | ((TextView)findViewById(R.id.time_end)).setText(String.format("%2d:%2d",hour,minute)); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onDateSet(final DatePicker picker, final int year, final int monthOfYear, final int dayOfMonth) { |
| 116 | final Calendar calendar=new GregorianCalendar(year, monthOfYear, dayOfMonth); |
| 117 | dateTimeTextView.setText(String.format("%4d-%2d-%2d", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))); |
| 118 | calendar.add(Calendar.DAY_OF_MONTH, 1); |
| 119 | if(dateTimeTextView.getId() == R.id.date_start) |
| 120 | ((TextView)findViewById(R.id.date_end)).setText(String.format("%4d-%2d-%2d", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))); |
| 121 | } |
| 122 | } |