Set current hour and minute for TimePicker
package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView dateDefault = (TextView)findViewById(R.id.dateDefault);
TextView timeDefault = (TextView)findViewById(R.id.timeDefault);
DatePicker dp = (DatePicker)this.findViewById(R.id.datePicker);
dateDefault.setText("Date defaulted to " + (dp.getMonth() + 1) + "/" +
dp.getDayOfMonth() + "/" + dp.getYear());
dp.init(2008, 11, 10, null);
TimePicker tp = (TimePicker)this.findViewById(R.id.timePicker);
java.util.Formatter timeF = new java.util.Formatter();
timeF.format("Time defaulted to %d:%02d", tp.getCurrentHour(),
tp.getCurrentMinute());
timeDefault.setText(timeF.toString());
tp.setIs24HourView(true);
tp.setCurrentHour(new Integer(10));
tp.setCurrentMinute(new Integer(10));
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/dateDefault"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<DatePicker android:id="@+id/datePicker"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/timeDefault"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<DigitalClock
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<AnalogClock
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
Related examples in the same category