question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toggle not working in Android.

See original GitHub issue

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Use TouchableWithoutFeedback
  2. onPress I am toggling with this.pickerRef.current.togglePicker(false)

Expected behavior
Should open the picker

Screenshots
null

Additional details

  • Device: Android
  • OS: 10
  • react-native-picker-select version: 8.0.4
  • react-native version: 0.63.3
  • expo sdk version: [ n/a]

Reproduction and/or code sample

<RNPickerSelect
          disabled={!editable}
          onValueChange={(v) => this.handleValueChange(v)}
          value={selectedValue}
          style={{ maxHeight: 0 }}
          ref={this.pickerRef}
          onUpArrow={this.onArrowUp}
          InputAccessoryView={this.InputAccessoryView}
          onDownArrow={this.onDownArrow}
          style={{
            chevronUp: {
              fontSize: 0,
              color: 'white',
            },
          }}
          items={values}>
          <View style={{ height: 0 }}></View>
        </RNPickerSelect>

        <TouchableWithoutFeedback
          onPress={() => {
            console.log('toogle');
            this.pickerRef.current.togglePicker(false);
          }}>
          <View style={[styles.inputContainer, style]}>
            {selectedValue.trim().length > 0 ? (
              <>
                <Text style={styles.labelStyle}>{placeholder}</Text>
                <Text style={styles.textInputStyle}>{label}</Text>
              </>
            ) : (
              <>
                <Text style={styles.labelStyle}>{placeholder}</Text>
              </>
            )}
          </View>
        </TouchableWithoutFeedback>
      </>

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:6

github_iconTop GitHub Comments

28reactions
bacarybrunocommented, May 30, 2021

@hasnaina-entertainer this should work. As per this commit https://github.com/react-native-picker/picker/pull/258 (thanks @mateusz1913)

     <>
        <RNPickerSelect
          disabled={!editable}
          onValueChange={(v) => this.handleValueChange(v)}
          value={selectedValue}
          style={{ maxHeight: 0 }}
          pickerProps={{ ref: this.pickerRef }}
          ref={this.pickerRef}
          onUpArrow={this.onArrowUp}
          InputAccessoryView={this.InputAccessoryView}
          onDownArrow={this.onDownArrow}
          style={{
            chevronUp: {
              fontSize: 0,
              color: 'white',
            },
          }}
          items={values}>
          <View style={{ height: 0 }}></View>
        </RNPickerSelect>

        <TouchableWithoutFeedback onPress={() => { this.pickerRef.current.focus(); }}>
          {content}
        </TouchableWithoutFeedback>
     </>

Short answer (Android only)

<RNPickerSelect
   {...otherProps}
   pickerProps={{ ref: this.pickerRef }}
/>

and then

<TouchableWithoutFeedback onPress={this.pickerRef.current.focus}>
  {content}
</TouchableWithoutFeedback>

To make this work for Android & iOS you can do something like this

class PickerSelect extends React.Component {
  pickerRef = React.createRef()

  openPicker() {
    if (Platform.OS === 'android') {
      this.pickerRef.current.focus()
    } else {
      this.pickerRef.current.togglePicker(true)
    }
  }

  render() {
    const { onValueChange, items, value } = this.props
    return (
      <RNPickerSelect
        onValueChange={onValueChange}
        items={items}
        style={{ viewContainer: { height: 0 } }}
        ref={Platform.OS === 'ios' ? this.pickerRef : null}
        pickerProps={{ ref: Platform.OS === 'android' ? this.pickerRef : null }}
        value={value}
      />
    )
  }
}

and then

const pickerRef = useRef();

<PickerSelect
  ref={pickerRef}
  items={values}
/>

<TouchableWithoutFeedback onPress={pickerRef.current.openPicker}>
  {content}
</TouchableWithoutFeedback>
1reaction
valeriashpinercommented, Aug 16, 2021

I got message for pickerProps={{ ref: this.pickerRef }}: Type ‘{ ref: React.MutableRefObject<undefined>; }’ is not assignable to type ‘CustomPickerProps’. Also, I tried @bacarybruno solution and I always get Cannot read properly "openPicker" of null.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Toggle button not toggling on Android - Stack Overflow
ToggleButton s, like all CompoundButton s, take care of their own checked state upon clicking. Your OnClickListener is counteracting that ...
Read more >
Shell.Current.FlyoutIsPresented = true does not toggle the first ...
Run the attached example. Tap on the "Toggle Flyout" button. Nothing happens. Swipe flyout manually. Tap on the "Toggle Flyout" button again.
Read more >
Known Tasker Issues And Workarounds - joaoapps.com
Google has changed the way Android works for apps that target API 29, so Tasker can't toggle wifi anymore. Check here for more...
Read more >
Mouse Toggle not working on Chromecast after update to ...
The mouse toggle functionality stopped working after i updated the Chromecast with Google Tv to the new Android 12. The mouse pointer pops...
Read more >
navbar dropdown toggling not working
*Expected behavior*When I click the outside of dropdown menu, it should be disappeared. *Actual behavior*Still show.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found