Android Open Source - ProjectStudio Add Task Fragment






From Project

Back to project page ProjectStudio.

License

The source code is released under:

Apache License

If you think the Android project ProjectStudio listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fragments;
//from   ww  w . j  a v  a  2 s.c  o m
import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.uniutilproject.R;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import DB_Provider.CourseDataSource;
import DB_Provider.DB_ABSTRACTS;
import DB_Provider.ProfessorDataSource;
import DB_Provider.TaskDataSource;
import dialog_fragments.DatePickerDialogFragment;
import dialog_fragments.TimePickerDialogFragment;
import model.Professor;
import model.Task;

/**
 * Created by desmond on 1/21/14.
 */
public class AddTaskFragment extends Fragment {
    private static final int TIMEPICKER_FRAGMENT = 0;
    private static final int DATEPICKER_FRAGMENT = 1;
    private DrawerLayout parent_drawer;
    private Spinner task_title_spinner;
    private AutoCompleteTextView task_note;
    private EditText task_location;
    private Spinner task_day;
    private TextView task_time;
    private TextView save_btn;
    private TextView cancel_btn;
    private DatePickerDialogFragment datepicker;
    private ProfessorDataSource prof_dataSource;
    private TaskDataSource task_dataSource;
    private Date time_format;
    private String current_title_position;
    private String current_day_position;
    private Task task;
    private Professor prof;
    private CourseDataSource courseDataSource;
    private LinearLayout acbar_cancel_button;
    private LinearLayout acbar_save_button;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.task_addtask_view, container, false);

        //OPEN DATABASE
        prof_dataSource = new ProfessorDataSource(getActivity());
        task_dataSource = new TaskDataSource(getActivity());
        courseDataSource = new CourseDataSource(getActivity());
        try {
            prof_dataSource.open();
            task_dataSource.open();
            courseDataSource.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        task = new Task();
        prof = new Professor();

        //TO PREVENT VIEWS FROM OVERLAYING EACH OTHER WHEN
        //KEYPAD IS CALLED, CALL THIS METHOD
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        //DRAWER HAS TO BE DISABLED IN OTHER TO PREVENT CHILD FRAGMENTS FROM OVERLAYING PARENT FRAGMENT
        parent_drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
        parent_drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        //INFLATE VIEWS HERE
        task_title_spinner = (Spinner) view.findViewById(R.id.task_title);
        task_location = (EditText) view.findViewById(R.id.task_location_et);
        task_note = (AutoCompleteTextView) view.findViewById(R.id.task_note_et);
        task_day = (Spinner) view.findViewById(R.id.task_dateView);
        task_time = (TextView) view.findViewById(R.id.task_timeView);
        //save_btn = (TextView) view.findViewById(R.id.task_savebtn);
        //cancel_btn = (TextView) view.findViewById(R.id.task_cancelbtn);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.task_spinner, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        task_title_spinner.setAdapter(adapter);

        //DAY SPINNER ADAPER
        ArrayAdapter<CharSequence> day_adapter =
                ArrayAdapter.createFromResource(getActivity(), R.array.day_spinner, android.R.layout.simple_spinner_item);
        day_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        task_day.setAdapter(day_adapter);


        //PUT LISTENERS HERE
        //task_day.setOnClickListener(new DatePickerListener());
        task_time.setOnClickListener(new TimePickerListener());

        //TASK TITLE LISTENER
        task_title_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                current_title_position = parent.getItemAtPosition(position).toString();
                if (current_title_position.equalsIgnoreCase("meeting")) {
                    task_note.setHint("professor");
                    final ArrayList<String> list = new ArrayList<String>();
                    new android.os.Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            final Cursor cursor = prof_dataSource.getAllProfessors();
                            if (cursor.moveToFirst()) {
                                do {
                                    list.add(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBProfessor.NAME_COLUMN)));
                                } while (cursor.moveToNext());
                            }
                            ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
                            task_note.setAdapter(ad);
                        }
                    });

                }
                if (current_title_position.equalsIgnoreCase("lesson")) {
                    task_note.setHint("course");
                    task_location.setHint("room");
                    //ADD AUTOCOMLETE TO THE COURSE TEXTFIELD
                    final ArrayList<String> list = new ArrayList<String>();
                    new android.os.Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            final Cursor cursor = courseDataSource.getAllCourses();
                            if (cursor.moveToFirst()) {
                                do {
                                    list.add(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN)));
                                } while (cursor.moveToNext());
                            }
                            ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
                            task_note.setAdapter(ad);
                        }
                    });
                }
                if (current_title_position.equalsIgnoreCase("todo")) {
                    task_note.setHint("task");
                    task_location.setHint("location");
                    //Restore default empty adapter
                    ArrayList<String> list = new ArrayList<String>();
                    task_note.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list));
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        //TASK DAY LISTENER
        task_day.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                current_day_position = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        /*
        save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //ASSURE THAT TEXTFIELDS ARE NOT EMPTY

                if (task_note.getText().toString().equals("")) {
                    task_note.post(new Runnable() {
                        public void run() {
                            task_note.requestFocusFromTouch();
                            InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            lManager.showSoftInput(task_note, 0);
                            toast_empty_field(task_note.getHint() + " FIELD CANNOT BE EMPTY");
                        }
                    });

                } else if (task_location.getText().toString().equals("")) {
                    task_location.post(new Runnable() {
                        public void run() {
                            task_location.requestFocusFromTouch();
                            InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            lManager.showSoftInput(task_note, 0);
                            toast_empty_field(task_location.getHint() + " FIELD CANNOT BE EMPTY");
                        }
                    });
                } else {

                    if (time_format == null || task_time.getText().toString().equalsIgnoreCase("Now")) {
                        //GET CURRENT TIME
                        getCurrentTime();
                    }

                    if (current_title_position.equalsIgnoreCase("meeting")) {
                        prof.setProfessor_name(task_note.getText().toString());
                        task.setTime(time_format);
                        task.setTitle(current_title_position);
                        task.setNote("Meeting with " + prof.getProfessor_name());
                        task.setDay(current_day_position);
                        task.setLocation(task_location.getText().toString());
                    } else {
                        task.setTime(time_format);
                        task.setTitle(current_title_position);
                        task.setNote(task_note.getText().toString());
                        task.setDay(current_day_position);
                        task.setLocation(task_location.getText().toString());
                        prof.setProfessor_name(" ");
                    }

                    task_dataSource.createTask(task, prof);

                    Log.e("includes", task_dataSource.getTaskCount() + " tasks");

                    //GO BACK TO PARENT FRAGMENT
                    getActivity().onBackPressed();
                }
            }
        });

        */

        /*
        cancel_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //GO BACK TO PARENT FRAGMENT
                getActivity().onBackPressed();
            }
        });
        */

        prof_dataSource.close();
        task_dataSource.close();
        courseDataSource.close();
        return view;
    }

    //PRINT CUSTOM TOAST MESSAGE FOR EDITTEXT
    public void toast_empty_field(String message) {
        Toast toast = Toast.makeText(getActivity(),
                message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION | Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();
    }

    //SET CURRENT TIME
    public void getCurrentTime() {
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        try {
            time_format = timeFormat.parse(hour + ":" + minute);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void showDatePickerFragment(View v) {
        DatePickerDialogFragment datepicker = new DatePickerDialogFragment();
        datepicker.setTargetFragment(this, DATEPICKER_FRAGMENT);
        datepicker.show(getFragmentManager(), "datePicker_professor");

    }

    public void showTimePickerFragment(View v) {
        TimePickerDialogFragment timepicker = new TimePickerDialogFragment();
        timepicker.setTargetFragment(this, TIMEPICKER_FRAGMENT);
        timepicker.show(getFragmentManager().beginTransaction(), "timePicker_professor");

    }

    //THIS METHOD RECIEVES THE DATA FROM THE DIALOG FRAGMENT
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        String time;
        String date;
        switch (requestCode) {
            //MAKE SURE FRAGMENT CODES MATCH UP
            /*
            case DATEPICKER_FRAGMENT:
                Bundle date_bundle = data.getExtras();
                date = date_bundle.getString("date");
                task_day.setText(date);
                break;
                */
            case TIMEPICKER_FRAGMENT:
                Bundle time_bundle = data.getExtras();
                int hourOfday = time_bundle.getInt("hour");
                int minute = time_bundle.getInt("minute");


                SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
                try {
                    time_format = timeFormat.parse(hourOfday + ":" + minute);
                    String actual_time = new SimpleDateFormat("HH : mm").format(time_format);
                    // set the selected time_format to textview
                    if (hourOfday > 12) {
                        // activity_textview.setText(actual_time + "  PM");
                        actual_time = actual_time + " PM";
                    } else {
                        //activity_textview.setText(actual_time + "  AM");
                        actual_time = actual_time + " AM";
                    }
                    task_time.setText(actual_time);
                    break;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
        }
    }

    //delete when done
    private class TimePickerListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            showTimePickerFragment(v);
        }
    }

    private class DatePickerListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            showDatePickerFragment(v);
        }
    }

    //REGSITER THE FRAGMENT AS A CONTRIBUTOR TO THE OPTIONS MENU
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }


    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);//REMOVE THE THREE DOT ICON FROM THE ACTIONBAR, SINCE IT IS NOT NEEDED FOR NOW
        MenuItem item = menu.findItem(R.id.action_settings);
        item.setVisible(false);
        //INFLATE CUSTOM ACTIONBAR
        ActionBar ab = getActivity().getActionBar();
        ab.setDisplayShowTitleEnabled(false);
        ab.setDisplayShowCustomEnabled(true);
        ab.setDisplayShowHomeEnabled(false);
        View customActionView = getActivity().getLayoutInflater().inflate(R.layout.actionbar_custom_layout, null);
        ab.setCustomView(customActionView);

        //ADD A LISTENER TO THE VIEWS IN THE CUSTOM ACTION BAR LAYOUT
        acbar_cancel_button = (LinearLayout) customActionView.findViewById(R.id.custom_ab_cancel);
        acbar_save_button = (LinearLayout) customActionView.findViewById(R.id.custom_ab_done);

        acbar_cancel_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });

        acbar_save_button.setOnClickListener(new SaveTaskListener());

    }

    private class SaveTaskListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            //ASSURE THAT TEXTFIELDS ARE NOT EMPTY

            if (task_note.getText().toString().equals("")) {
                task_note.post(new Runnable() {
                    public void run() {
                        task_note.requestFocusFromTouch();
                        InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        lManager.showSoftInput(task_note, 0);
                        toast_empty_field(task_note.getHint() + " FIELD CANNOT BE EMPTY");
                    }
                });

            } else if (task_location.getText().toString().equals("")) {
                task_location.post(new Runnable() {
                    public void run() {
                        task_location.requestFocusFromTouch();
                        InputMethodManager lManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        lManager.showSoftInput(task_note, 0);
                        toast_empty_field(task_location.getHint() + " FIELD CANNOT BE EMPTY");
                    }
                });
            } else {

                if (time_format == null || task_time.getText().toString().equalsIgnoreCase("Now")) {
                    //GET CURRENT TIME
                    getCurrentTime();
                }

                if (current_title_position.equalsIgnoreCase("meeting")) {
                    prof.setProfessor_name(task_note.getText().toString());
                    task.setTime(time_format);
                    task.setTitle(current_title_position);
                    task.setNote("Meeting with " + prof.getProfessor_name());
                    task.setDay(current_day_position);
                    task.setLocation(task_location.getText().toString());
                } else {
                    task.setTime(time_format);
                    task.setTitle(current_title_position);
                    task.setNote(task_note.getText().toString());
                    task.setDay(current_day_position);
                    task.setLocation(task_location.getText().toString());
                    prof.setProfessor_name(" ");
                }

                task_dataSource.createTask(task, prof);

                Log.e("includes", task_dataSource.getTaskCount() + " tasks");

                //GO BACK TO PARENT FRAGMENT
                getActivity().onBackPressed();
            }
        }
    }
}




Java Source Code List

DB_Provider.CourseDataSource.java
DB_Provider.DBManager.java
DB_Provider.DB_ABSTRACTS.java
DB_Provider.ProfessorDataSource.java
DB_Provider.TaskDataSource.java
Drawables.DrawableAlignedButton.java
FontPackage.TypefaceSpan.java
adapters.CourseCardAdapter.java
adapters.ExamsPassedCardAdapter.java
adapters.ExamsPendingCardAdapter.java
adapters.Exams_TabsPagerAdapter.java
adapters.HomeCardAdapter.java
adapters.MatesCardAdapter.java
adapters.NavDrawerListAdapter.java
adapters.ProfessorCardAdapter.java
adapters.TaskCardAdapter.java
adapters.TasksTabPagerAdapter.java
com.example.uniutilproject.BuildConfig.java
com.example.uniutilproject.UniUtil_MainActivity.java
days_fragments.FridayTaskFragment.java
days_fragments.MondayTaskFragment.java
days_fragments.SaturdayTaskFragment.java
days_fragments.SundayTaskFragment.java
days_fragments.ThursdayTaskFragment.java
days_fragments.TuesdayTaskFragment.java
days_fragments.WednesdayTaskFragment.java
dialog_fragments.DatePickerDialogFragment.java
dialog_fragments.MatesDialogFragment.java
dialog_fragments.ProfessorsDialogFragment.java
dialog_fragments.TimePickerDialogFragment.java
fragments.AddCourseFragment.java
fragments.AddProfFragment.java
fragments.AddTaskFragment.java
fragments.CoursesFragment.java
fragments.CursorLoaderListFragment.java
fragments.ExamsFragment.java
fragments.ExamsPassedFragment.java
fragments.ExamsPendingFragment.java
fragments.HomeFragment.java
fragments.MatesFragment.java
fragments.ProfessorsFragment.java
fragments.StudyPlanFragment.java
fragments.TasksFragment.java
fragments.UpdateCourseFragment.java
fragments.UpdateProfessorFragment.java
interfaces.NoticeDialogListener.java
interfaces.package-info.java
model.Course.java
model.Days.java
model.MyParcelable.java
model.NavDrawerItem.java
model.Professor.java
model.Task.java