Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Please could someone help me?

I have been struggling for days know to get three independently working timepickers to work on a single page on my Android Studio app.

At present I cannot even get these two timepickers to work independently of each other and I just don't understand why.

Any advice would be greatly appreciated.

Sarah

What I have tried:

import java.util.Calendar;

import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity implements OnClickListener {

    private EditText time1Text, time2Text;

    private int mHour, mMinute;

    private TimePickerDialog time1TimePickerDialog;
    private TimePickerDialog time2TimePickerDialog;

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

        time1Text = (EditText) findViewById(R.id.etTime1);
        time1Text.setInputType(InputType.TYPE_NULL);
        time1Text.requestFocus();

        time2Text = (EditText) findViewById(R.id.etTime2);
        time2Text.setInputType(InputType.TYPE_NULL);

        time1Text.setOnClickListener(this);
        time2Text.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        // Get Current Time
        final Calendar c = Calendar.getInstance();
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);

        // Launch Time Picker Dialog
        time1TimePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                time1Text.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        time1TimePickerDialog.show();
    }
}
Posted
Updated 4-Jun-17 16:38pm
Comments
Richard MacCutchan 30-May-17 4:52am    
Your code does not match your question, you declare two dialogs but you only ever use one of them.
Sarah Miller 30-May-17 4:54am    
Ok thanks.

Is that why only one of them is working??

Sarah
Richard MacCutchan 30-May-17 5:08am    
As far as I can see the second one does nothing.

Inside of the onTimeSet() method, you are only using time1Text.

In your current implementation, the View object that is passed to onClick() is the EditText that was clicked. Taking that into consideration, my suggestion would be something like:

@Override
public void onClick(View v) 
{
    // Get Current Time
    Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);
 
    // Launch Time Picker Dialog
    TimePickerDialog tpd = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() 
    {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
        {
            ((EditText) v).setText(hourOfDay + ":" + minute);
        }
    }, mHour, mMinute, false);

    tpd.show();
}
 
Share this answer
 
Comments
Sarah Miller 3-Jun-17 13:53pm    
Thank you so much David Crow.

I like your solution but the only problem is that I have two other edit texts on the page that are used to enter dates.

With your solution, when the user clicks on the date edit texts it allows them to enter a time as well.

How can I differentiate between the three time pickers and the two date pickers.

I have been trying to get this to work for a good few hours now and I am starting to lose hope.

Thanks Again.

Sarah
The View object that is sent to the onClick() method is the EditText widget that was clicked on. Differentiate between them with something like:
EditText et = (EditText) v;
if (et.getId() == R.id.etTime1)
    ;
else if (et.getId() == R.id.etTime2)
    ;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900