Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset datepicker

I am developing a taskmanager on Android 2.1. I want to reset date and time on clicking the reset button to current date and time. Help me with the code..

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText next = (EditText) findViewById(R.id.editText1);
final Button sub = (Button) findViewById(R.id.button1);
final Button res = (Button) findViewById(R.id.button2);
final DatePicker dp= (DatePicker) findViewById(R.id.datePicker1);
final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);

res.setOnClickListener(new View.OnClickListener() {     
 public void onClick(final View view) {                  
     next.setText("");
     dp.refreshDrawableState();
        }       
    });    
}}
like image 497
prakash_d22 Avatar asked Mar 15 '26 14:03

prakash_d22


2 Answers

//get current time
Time now = new Time();
now.setToNow();

//update the TimePicker
tp.setHour(now.hour);
tp.setMinute(now.minute);

//update the DatePicker
dp.updateDate(now.year, now.month, now.monthDay);
like image 81
Gabriel Negut Avatar answered Mar 17 '26 05:03

Gabriel Negut


Time is now deprecated. Use Calendar instead:

Calendar calendar = Calendar.getInstance();

tp.setHour(calendar.get(Calendar.HOUR_OF_DAY));
tp.setMinute(calendar.get(Calendar.MINUTE));      

dp.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
like image 31
The Berga Avatar answered Mar 17 '26 04:03

The Berga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!