Search code examples
javaandroidtimepicker

get total hours from two timepicker


Can someone tell me how can I get the total hours from two TimePicker ?It is possible to get since the two timepicker are actually getting from the same function.

WorkDetails.java

        EditText TimeIn = (EditText) findViewById(R.id.editText6);
        EditText TimeOut = (EditText) findViewById(R.id.editText7);
        TimeIn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    TimePick time = new TimePick(v);
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    time.show(ft, "TimePicker");
                }
            }
        });


        TimeOut.setOnFocusChangeListener(new View.OnFocusChangeListener(){
            @Override
            public void onFocusChange(View v,boolean hasFocus) {
                if (hasFocus) {
                    TimePick time = new TimePick(v);
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    time.show(ft, "TimePicker");

                }
            }
        });

TimePick.java

public class TimePick extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
   EditText time;

    public TimePick(View view)
    {
       time=(EditText)view;
    }

    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        final Calendar c= Calendar.getInstance();
        int hour=c.get(Calendar.HOUR_OF_DAY);
        int minute=c.get(Calendar.MINUTE);
        return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view,int hourofDay, int minute)
    {

        time.setText(Integer.toString(hourofDay) + ":" + Integer.toString(minute));
    }


}

workdetails.xml

<EditText
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText6"
        android:layout_x="147dp"
        android:layout_y="170dp"
        android:hint="Select Time"
        android:clickable="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Time In"
        android:id="@+id/textView7"
        android:layout_x="23dp"
        android:layout_y="184dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Time out"
        android:id="@+id/textView8"
        android:layout_x="23dp"
        android:layout_y="247dp" />

    <EditText
        android:layout_width="143dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText7"
        android:layout_x="147dp"
        android:layout_y="241dp"
        android:hint="Select Time"/>

Solution

  • you can calculate the hours of the day to do a concat your time hours and min with (.) dot >> later parse it as a float >> and make a differance. here is a working code snippet.

            float end = Float.parseFloat("21.00");
            float start = Float.parseFloat("10.00");
    
            System.out.println(end-start);
    

    change you method like:

    public void onTimeSet(TimePicker view,int hourofDay, int minute)
        {
    
            time.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
        }
    

    and get the value later from editText.gettext();

    Edit:

    Make your TimeIn editText variable static and get the value of it while setting timeout like this..

        public void onTimeSet(TimePicker view,int hourofDay, int minute)
            {
    
                time.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
    
              if(time.getId==R.id.editText7){
                if(YourActivity.TimeIn !=null){
                String timeIn = YourActivity.TimeIn.getText().toString();
    
                float end = Float.parseFloat(time.getText().toString);
                float start = Float.parseFloat(timeIn);
    
                System.out.println(end-start);
               // do what you need with your calculated hours
             }
            }
            }