TimePicker and TimePickerDialog




TimePicker and TimePickerDialog

In Android app Time Picker allows you to select the Hours and Minutes in your custom user interface. For this functionality android provides TimePicker and TimePickerDialog widget.

TimePickerDialog is the better choice for android Ui code.






package example.com.uidemo;

import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class First extends AppCompatActivity {
TimePicker tp;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
tp = findViewById(R.id.time);
tv = findViewById(R.id.text);

}

public void selectDate(View v) {

Calendar c=Calendar.getInstance();

TimePickerDialog td=new TimePickerDialog(First.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {

}
}, c.get(Calendar.HOUR), c.get(Calendar.MINUTE), true);//Yes 24 hour time
td.setTitle("Select Time");
td.show();

}
}









< ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

< RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello Time Picker"
android:textColor="#db0000"
android:background="#ffff24"
android:textSize="30sp"
android:textStyle="bold"
android:id="@+id/text"
android:typeface="serif"
android:gravity="center"/>

< TimePicker
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:id="@+id/time"
android:layout_centerInParent="true">

< /TimePicker>

< Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Date"
android:layout_below="@id/time"
android:onClick="selectDate"
android:layout_alignParentBottom="true"/>


< /RelativeLayout>

< /ScrollView>

No comments: