Navigate back to the homepage

how to add datePickerAndroid in react native with color changes?

Infinitbility
React Native
October 30th, 2020 · 1 min read
how to add datePickerAndroid in react native with color changes?

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 picker
2async openDatePicker(){
3
4 try {
5 const {
6 action,
7 year,
8 month,
9 day
10 } = 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 date
14 });
15 if (action !== DatePickerAndroid.dismissedAction) {
16 // Selected year, month (0-11), day
17
18 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 here
3}

write code after press ok on datepicker

1if (action == DatePickerAndroid.dateSetAction) {
2 // code goes here
3}

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>
2
3 <!-- 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>
8
9 <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 day
8 } = 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);
17
18 if(dateType == 'start'){
19 this.setState({selectedStartDate: date});
20 }else{
21 this.setState({selectedEndDate: date});
22 }
23
24 }
25 } catch ({ code, message }) {
26 console.warn('Cannot open date picker', message);
27 }
28 }

Thanks for reading…

More From React Native Tutorial

Basics

  1. Introduction To React Native
  2. React Native Environment Setup using expo
  3. React Native Environment Setup for windows
  4. React Native Environment setup on Mac OS
  5. React Native Environment setup on linux
  6. React Native Project Structure
  7. React Native State
  8. React Native Props
  9. React Native Styling
  10. React Native Flexbox
  11. React Native Text
  12. React Native Textinput
  13. React Native Commands
  14. React Native ScrollView

Advances

  1. React Native Dark Mode
  2. React Native Fonts
  3. React Native SQLite
  4. React Native DatepickerAndroid
  5. React native ScrollView scroll to position
  6. How to align icon with text in react native
  7. React Native Image
  8. React Native Firebase Crashlytics
  9. React Native Async Storage
  10. React Native Share

Error & Issue Solution

  1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
  2. Expiring Daemon because JVM heap space is exhausted In React Native
  3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
  4. Unable to determine the current character, it is not a string, number, array, or object in react native
  5. App crashed immediately after install react native video or track player
  6. how to delete SQLite database in android react native
  7. React native material dropdown twice click issue
  8. How to get the current route in react-navigation?
  9. how to disable drawer on the drawer navigation screen?
  10. Image not showing in ios 14 react native
  11. React Native image picker launchimagelibrary on second time issue
  12. how to open any link from react native render Html
  13. Network request failed in react native fetch
  14. React Native upload video example

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo

Tutorials

Categories

More articles from Infinitbility

How to get the current route in react-navigation?

How to get the current route in react-navigation?

the simplest way to get current route name from react navigation.

October 25th, 2020 · 1 min read
Why python is slower then other low level programming languages?

Why python is slower then other low level programming languages?

the reason behind python is slower than other low-level programming languages with proper example.

October 15th, 2020 · 2 min read
© 2020–2021 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]