Reputation:
I want to use Two TimePicker
in single screen..
I tried but the problem is when i select one timepicker
it will automatic generate the same time in another textview.
here MainActivity.java
package com.example.sachin.timepicker;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends Activity {
private TextView tvDisplayTime, tvDisplayTime1;
private TimePicker timePicker1;
private Button btnChangeTime, btnChangeTime1;
private int hour;
private int minute;
static final int TIME_DIALOG_ID = 999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentTimeOnView();
addListenerOnButton();
}
// display current time
public void setCurrentTimeOnView() {
tvDisplayTime = (TextView) findViewById(R.id.tvTime);
tvDisplayTime1 = (TextView) findViewById(R.id.tvTime1);
// timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));
tvDisplayTime1.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));
// set current time into timepicker
//timePicker1.setCurrentHour(hour);
//timePicker1.setCurrentMinute(minute);
}
public void addListenerOnButton() {
btnChangeTime = (Button) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (Button) findViewById(R.id.btnChangeTime1);
btnChangeTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
btnChangeTime1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(this, timePickerListener, hour, minute,
false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
tvDisplayTime.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
tvDisplayTime1.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
// set current time into timepicker
// timePicker1.setCurrentHour(hour);
// timePicker1.setCurrentMinute(minute);
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
Upvotes: 2
Views: 1255
Reputation: 15336
You need to keep track which time button is clicked isTimePickerFromClicked=True
then in your from
button is clicked set this flag to False
.
Before you setText
check if isTimePickerFromClicked
is either True or False
and setText
accordingly
Update 1: Your code
public class MainActivity extends Activity {
boolean isFromClicked = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentTimeOnView();
addListenerOnButton();
}
// display current time
public void setCurrentTimeOnView() {
tvDisplayTime = (TextView) findViewById(R.id.tvTime);
tvDisplayTime1 = (TextView) findViewById(R.id.tvTime1);
// timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// set current time into textview
tvDisplayTime.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));
tvDisplayTime1.setText(new StringBuilder().append(pad(hour)).append(":")
.append(pad(minute)));
}
public void addListenerOnButton() {
btnChangeTime = (Button) findViewById(R.id.btnChangeTime);
btnChangeTime1 = (Button) findViewById(R.id.btnChangeTime1);
btnChangeTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
isFromClicked = false;
}
});
btnChangeTime1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
isFromClicked = true;
}
});
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
if ( isDateToSelected )
tvDisplayTime1.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
else
tvDisplayTime.setText(new StringBuilder().append(pad(hour))
.append(":").append(pad(minute)));
}
};
}
Note: removed some code to reduce length.
Upvotes: 1