Android TimePicker is used to select a time. Whenever we want from user to select a time, we use TimePicker.
When user select some time and click on OK button we will show the selected timein textView.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSelectTime"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textSize="23dp"
android:text=" OK " />
<TextView
android:id="@+id/textViewTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Date and Time Selected: "
android:textSize="17dp" />
</LinearLayout>
{
TimePicker timePicker;
TextView textView;
Button btnTime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timePicker=(TimePicker)findViewById(R.id.timePicker1);
textView=(TextView)findViewById(R.id.textViewTime);
btnTime=(Button)findViewById(R.id.buttonSelectTime);
btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int hour=timePicker.getCurrentHour();
int minute=timePicker.getCurrentMinute();
textView.setText("Time Selected "+hour+":"+minute);
}
});
}
}
main.xml
In layout we have a timrpicker, a button, and a textViewWhen user select some time and click on OK button we will show the selected timein textView.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSelectTime"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textSize="23dp"
android:text=" OK " />
<TextView
android:id="@+id/textViewTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Date and Time Selected: "
android:textSize="17dp" />
</LinearLayout>
TimePickerActivity
public class TimePickerActivity extends Activity{
TimePicker timePicker;
TextView textView;
Button btnTime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timePicker=(TimePicker)findViewById(R.id.timePicker1);
textView=(TextView)findViewById(R.id.textViewTime);
btnTime=(Button)findViewById(R.id.buttonSelectTime);
btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int hour=timePicker.getCurrentHour();
int minute=timePicker.getCurrentMinute();
textView.setText("Time Selected "+hour+":"+minute);
}
});
}
}
No comments:
Post a Comment