Wednesday, 6 May 2020

Android Date Time Picker Dialog with example

Android Date Time Picker Dialog Project Code


create xml file in android studio 

for example : LoginActivity


<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/btnDate"
android:layout_width="match_parent" 
android:layout_height="wrap_content"    
android:layout_marginTop="@dimen/_10sdp"    
android:background="@drawable/gray_corner"    
android:drawableLeft="@drawable/ic_date"    
android:drawablePadding="@dimen/_10sdp"    
android:focusable="true"    
android:hint="Date of Birth"    
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;
Button selectDate;
TextView date;
private int Year, Month, Day, mHour, mMinute;
DatePickerDialog datePickerDialog;
int year;
int month;
int dayOfMonth;
Calendar calendar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnDate = findViewById(R.id.btnDate);
btnDate.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
final java.util.Calendar c = java.util.Calendar.getInstance();
int mYear = c.get(java.util.Calendar.YEAR); // current year
                int mMonth = c.get(java.util.Calendar.MONTH); // current month                
                int mDay = c.get(java.util.Calendar.DAY_OF_MONTH); // current day 
datePickerDialog = new DatePickerDialog(LoginActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
btnDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
});
}
}

No comments:

Post a Comment