Wednesday, 6 May 2020

Android Time Picker Dialog with example

IN XML FILE

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LoginActivity">

<TextView
android:id="@+id/pickuptime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp"
android:background="@drawable/gray_corner"
android:drawableLeft="@drawable/ic_access_time_black_24dp"
android:drawablePadding="@dimen/_10sdp"
android:focusable="true"
android:hint="time"
android:inputType="textEmailAddress"
android:isScrollContainer="true"
android:overScrollMode="always"
android:padding="@dimen/_10sdp"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:singleLine="false"
android:windowSoftInputMode="stateAlwaysVisible">
</TextView>
</LinearLayout>

In JAVA FILE


public class LoginActivity extends AppCompatActivity {
    TextView btn_login;
    TextView btnDate;
    TextView pickuptime;
    private int Year, Month, Day, mHour, mMinute;
    DatePickerDialog datePickerDialog;
    Calendar calendar;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        btn_login= findViewById(R.id.btn_login);
        pickuptime = findViewById(R.id.pickuptime);


pickuptime.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {

        final java.util.Calendar c = java.util.Calendar.getInstance();
        mHour = c.get(java.util.Calendar.HOUR_OF_DAY);
        mMinute = c.get(java.util.Calendar.MINUTE);
 TimePickerDialog timePickerDialog = new TimePickerDialog(LoginActivity.this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override                    
public void onTimeSet(TimePicker view, int hourOfDay,
                                          int minute) {
                        //mStr_pickup_time= hourOfDay + ":" + minute;
pickuptime.setText(hourOfDay + ":" + minute);
                    }
                }, mHour, mMinute, false);
        timePickerDialog.show();

    }
});

No comments:

Post a Comment