Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi please any one help me how to use two time picker in a single layout.
Posted
Comments
anamicaa 13-May-14 2:48am    
package com.example.androiddatepicker;
import java.util.Calendar;

import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TimePicker;

public class TimePickerFragment implements android.view.View.OnClickListener
{
private final static int DIALOG_FROM_TIME_PICKER = 0;
private final static int DIALOG_TO_TIME_PICKER = 1;
private static Context context;
private ImageButton bt1;
private ImageButton bt2;

private EditText txtFromTime;
private EditText txtToTime;
private int setFromhour;
private int setFrommin;
private Calendar cal,cal1;
private int setTohour;
private int setTomin;
private int dialog=0;

public static TimePickerFragment newInstance( Context ctx )
{
TimePickerFragment frag = new TimePickerFragment();
Bundle args = new Bundle();
frag.setArguments(args);
context=ctx;
return frag;
}

private void setArguments(Bundle args) {
// TODO Auto-generated method stub

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_main, container, false);
txtFromTime=(EditText)rootView.findViewById(R.id.editText);
txtToTime=(EditText)rootView.findViewById(R.id.editText1);

bt1=(ImageButton)rootView.findViewById(R.id.imageButton1);
bt1.setOnClickListener(this);

bt2=(ImageButton)rootView.findViewById(R.id.imageButton2);
bt2.setOnClickListener(this);

txtFromTime.setOnClickListener(this);
txtToTime.setOnClickListener(this);


// setContentView(R.layout.activity_main);

cal = Calendar.getInstance();
setFromhour = cal.get(Calendar.HOUR_OF_DAY);
setFrommin = cal.get(Calendar.MINUTE);



cal1 = Calendar.getInstance();
setTohour = cal1.get(Calendar.HOUR_OF_DAY);
setTomin = cal1.get(Calendar.MINUTE);


return rootView;
}

private void showTimePickerDialog ( int id )
{
switch ( id )
{
case DIALOG_FROM_TIME_PICKER:

new TimePickerDialog(context,mTimeSetListener, setFromhour, setFrommin, false).show();
break;
case DIALOG_TO_TIME_PICKER:
new TimePickerDialog(context, mTimeSetListener, setTohour,setTomin, false).show();
}
}

@Override
public void onClick( View v )
{
switch (v.getId())
{

case R.id.imageButton1 :
dialog = DIALOG_FROM_TIME_PICKER;
showTimePickerDialog (DIALOG_FROM_TIME_PICKER);
break;
case R.id.imageButton2 :
dialog = DIALOG_TO_TIME_PICKER;
showTimePickerDialog(DIALOG_TO_TIME_PICKER);
break;
default:
break;
}
}

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener()
{

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if ( dialog == DIALOG_FROM_TIME_PICKER )
{
int hour;
String am_pm;
if (hourOfDay > 12) {
hour = hourOfDay - 12;
am_pm = "PM";
}
else {
hour = hourOfDay;
am_pm = "AM";
txtFromTime.setText(hour + " : " + minute + " " + am_pm);
}


}
else if ( dialog == DIALOG_TO_TIME_PICKER )
{
int hour;
String am_pm;
if (hourOfDay > 12) {
hour = hourOfDay - 12;
am_pm = "PM";
}
else {
hour = h
anamicaa 13-May-14 2:48am    
above one was my code?pls any one help

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