Hello Friends,
Welcome To Infinitbility!
DatePickerAndroid provide by react native you don’t need to install packages for it.
for import DatePickerAndroid from react native
1import { DatePickerAndroid } from 'react-native';
DatePickerAndroid
Deprecated. Use @react-native-community/datetimepicker instead.
To show date picker in react native call below function on your code
1// call function for show date picker2async openDatePicker(){34 try {5 const {6 action,7 year,8 month,9 day10 } = await DatePickerAndroid.open({11 // Use `new Date()` for current date.12 // May 25 2020. Month 0 is January.13 date: new Date(2020, 4, 25) // show selected date14 });15 if (action !== DatePickerAndroid.dismissedAction) {16 // Selected year, month (0-11), day1718 this.setState({date: new Date(year, month, day)});19 }20 } catch ({ code, message }) {21 console.warn('Cannot open date picker', message);22 }23}
props
show selected current date on datepicker you want
1await DatePickerAndroid.open({2 date : new Date()3});
For show disabled previous date on datepicker
1await DatePickerAndroid.open({2 minDate : new Date()3});
for show disabled upcoming date on datepicker
1await DatePickerAndroid.open({2 maxDate : new Date()3});
change datepicker layout like calendar, spinner, and default.
1await DatePickerAndroid.open({2 mode: 'spinner' // enum('calendar', 'spinner', 'default')3});
write code after press cancel on datepicker
1if (action == DatePickerAndroid.dismissedAction) {2 // code goes here3}
write code after press ok on datepicker
1if (action == DatePickerAndroid.dateSetAction) {2 // code goes here3}
chage datepicker theme style
DatePickerAndroid is an API for the native DatePicker Android module and thus cannot be directly styled through JS props like real React Native components. However, you can style the native android modules by changing the styles.xml in your android folder. It is located at “android/app/src/main/res/values/styles.xml” relative to your react-native project. To get a red background for the DatePicker, you edit the contents of styles.xml as follows:
Make Sure your datePickerAndroid mode is default else style not working.
1<resources>23 <!-- Base application theme. -->4 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">5 <!-- Customize your theme here. -->6 <item name="android:datePickerDialogTheme">@style/Dialog.Theme</item>7 </style>89 <style name="Dialog.Theme" parent="Theme.AppCompat.Light.Dialog">10 <item name="colorAccent">#FF0000</item>11 <item name="android:textColorPrimary">#0000FF</item>12 </style>13</resources>
Multipe Fields for datepicker
when you have Multipe input Fields of date then pass flag on openDatePicker function. like below example.
1async openDatePicker(dateType){2 try {3 const {4 action,5 year,6 month,7 day8 } = await DatePickerAndroid.open({9 // Use `new Date()` for current date.10 // May 25 2020. Month 0 is January.11 minDate: new Date(this.state.selectedStartDate),12 date: new Date(),13 mode: 'default'14 });15 if (action !== DatePickerAndroid.dismissedAction) {16 let date = new Date(year, month, day);1718 if(dateType == 'start'){19 this.setState({selectedStartDate: date});20 }else{21 this.setState({selectedEndDate: date});22 }2324 }25 } catch ({ code, message }) {26 console.warn('Cannot open date picker', message);27 }28 }
if you get help, please share post on your social network
More From React Native Tutorial
Basics
- Introduction To React Native
- React Native Environment Setup using expo
- React Native Environment Setup for windows
- React Native Environment setup on Mac OS
- React Native Environment setup on linux
- React Native Project Structure
- React Native State
- React Native Props
- React Native Styling
- React Native Flexbox
- React Native Text
- React Native Textinput
- React Native Commands
Advances
- React Native Dark Mode
- React Native Fonts
- React Native SQLite
- React Native DatepickerAndroid
- React native ScrollView scroll to position
- How to align icon with text in react native
- React Native Image
- React Native Firebase Crashlytics
- React Native Async Storage
- React Native Share
Error & Issue Solution
- Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
- Expiring Daemon because JVM heap space is exhausted In React Native
- Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
- Unable to determine the current character, it is not a string, number, array, or object in react native
- App crashed immediately after install react native video or track player
- how to delete SQLite database in android react native
- React native material dropdown twice click issue
- How to get the current route in react-navigation?
- how to disable drawer on the drawer navigation screen?
- Image not showing in ios 14 react native