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.

Pressable not working in react-native android

See original GitHub issue

Description

When a child of a pressable component is pressed (such as an image), the function passed to the onPress prop does not execute on android. Works as expected on iOS.

React Native version:

0.63.2

Steps To Reproduce

  1. Open a new expo snack
  2. Create a Pressable component that is the parent of some other component (A text or image)
  3. Set the onPress prop to call a function that has a visual effect. (Like an alert)
  4. Switch to the android tab, and click ‘Tap to play’

Expected Results

The function is called and the effect (an Alert) is fired

Snack, code example, screenshot, or link to a repository:

https://snack.expo.io/@razorshnegax/6c7be3

Code example:

import React from 'react';
import { View, Pressable, Image, Alert } from 'react-native';

export default class Index extends React.Component {
  render() {
    // The onPress function fires in iOS, but not android
    return (
      <View>
        <Pressable onPress={() => {Alert.alert("Yeep")}}>
          <Image source={require('./greatknight.png')} style={{
            // So that the image is more centered
            top: 100,
            left: 100
          }}/>
        </Pressable>
      </View>
    );
  }
}


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

4reactions
Razorshnegax018commented, Feb 10, 2021

Yes, @RicardoBrito1938. Turns out that the positioning of the pressable is skewered in android. All you need to do is set the Pressable’s style to whatever position you want the child’s (an Image’s, so to say), and then set the child’s postion to top: 0, left: 0, position: relative. The pressable then should work. Here is the link to my stack overflow post if you need reference. I can add a snack example if you need more help,

1reaction
Thembelanicommented, Oct 26, 2021

Code example would be really helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pressable not working in react-native android - Stack Overflow
Open a new expo snack · Create a Pressable component that is the parent of some other component (A text or image) ·...
Read more >
Please tell me why my Pressable simple code is not working
Please tell me why my Pressable simple code is not working. I've tried even alerting or consoling logging without the handlePress function. What ......
Read more >
Pressable - React Native
Pressable is a Core Component wrapper that can detect various stages of press interactions on any of its defined children.
Read more >
Why We Should Use Pressable In React Native
Every React Native developer is familiar with Touchable components. As a React Native developer, you've probably visited the documentation ...
Read more >
How to use Pressable Component in React Native - YouTube
Facebook's React Native user interface (UI) design which is designed to support IOS and Android OS both platforms. React Native allows ...
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