Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TouchableOpacity is working in Android? #578

Closed
akiradeveloper opened this issue Apr 24, 2019 · 23 comments · Fixed by #1567
Closed

TouchableOpacity is working in Android? #578

akiradeveloper opened this issue Apr 24, 2019 · 23 comments · Fixed by #1567
Assignees
Labels
Missing info Platform: Android This issue is specific to Android Platform: Cross platform inconsistency This issue presets the library behavior inconsistency between platforms To verify

Comments

@akiradeveloper
Copy link

I am using RN 0.59.5 with this library and it seems that TouchableOpacity doesn't handle onPress hook in Android whereas it does in iOS. Is this a known issue?

@jkadamczyk
Copy link
Contributor

Hi @akiradeveloper I bumped version of React Native in Example app to 0.59.5 and TouchableOpacity on android records onPress events. You can check this out by yourself on this branch up until it gets merged to master 😊

https://github.com/kmagiera/react-native-gesture-handler/tree/upgrade-react-native

@PierreCapo
Copy link

PierreCapo commented Jun 1, 2019

Hello,

I have the same issue right now with react-native 0.59.8 and react-native-gesture-handler 1.2.2
TouchableOpacity and TouchableHighlight from react-native-gesture-handler aren't working on a basic react-native app on Android (it works well on iOS).

I have changed the import to use the touchables from the react-native library and it works

@mountainWanderer
Copy link

I have the same problem.. but in react native version 0.60.x TouchableOpacity of react-native library seems to work well.

@CyxouD
Copy link

CyxouD commented Nov 26, 2019

To not make mistake described by @PierreCapo again, added ESLint rule to show error when using buttons from react-native-gesture-handler:

"no-restricted-imports": [
      "error",
      {
        "paths": [
          {
            "name": "react-native-gesture-handler",
            "importNames": [
              "TouchableOpacity",
              "TouchableNativeFeedback",
              "TouchableHighlight",
              "TouchableWithoutFeedback"
            ],
            "message": "Please import it from 'react-native' instead."
          }
        ]
      }
    ]

@zingerj
Copy link

zingerj commented Jan 30, 2020

Not able to get react-native-gesture-handler Touchables working at all (trying on iOS simulator) on react-native@0.61.5. Any update on this? Using the default Touchables from react-native for now...

@neiker
Copy link

neiker commented Jan 31, 2020

This is what I do, which is really ugly:

/src/core/touchable/index.ios.ts

// eslint-disable-next-line no-restricted-imports
export {
  TouchableOpacity,
  TouchableNativeFeedback,
  TouchableHighlight,
  TouchableWithoutFeedback,
  FlatList,
  ScrollView,
} from 'react-native-gesture-handler';

/src/core/touchable/index.ts

// eslint-disable-next-line no-restricted-imports
export {
  TouchableOpacity,
  TouchableNativeFeedback,
  TouchableHighlight,
  TouchableWithoutFeedback,
  FlatList,
  ScrollView,
} from 'react-native';

eslint config:

module.exports = {
  extends: ['./eslint-config/native'],
  rules: {
    'no-restricted-imports': [
      'error',
      {
        paths: [
          {
            name: 'react-native-gesture-handler',
            importNames: [
              'TouchableOpacity',
              'TouchableNativeFeedback',
              'TouchableHighlight',
              'TouchableWithoutFeedback',
              'FlatList',
              'ScrollView',
            ],
            message: "Please import it from '/src/core/touchables' instead.",
          },
          {
            name: 'react-native',
            importNames: [
              'TouchableOpacity',
              'TouchableNativeFeedback',
              'TouchableHighlight',
              'TouchableWithoutFeedback',
              'FlatList',
              'ScrollView',
            ],
            message: "Please import it from '/src/core/touchables' instead.",
          },
        ],
      },
    ],
  },
};

@noway
Copy link

noway commented Mar 15, 2020

What if I want to use TouchableNativeFeedback from react-native-gesture-handler? It has a WAY BETTER ripple effect, literally day and night in terms of ripple animation. But ironically onPress doesn't trigger 🤔

@noway
Copy link

noway commented Mar 15, 2020

this helped me: #699

@YahiaBadr
Copy link

@noway did you find any solution? i looked at the link you provided but unfortunately, my project is an expo manged one. So if you got any other solution please let me know.

@noway
Copy link

noway commented Apr 12, 2020

expo doesn't provide as much flexility as standard react native installation for issues exactly like this, so i'd recommend not to use it.

@abcdefghiraj
Copy link

I moved the child component to be a sibling of the touchable component and then gave it absolute positioning such that it draws over the child. It's a stupid hack but suits my use case.

<View>
    <TextInput
       //editable={false}
      />
     <TouchableOpacity 
         style={{position: 'absolute', left:0, top:0 ,width:'100%', height: '100%'}} 
          onPress={()=>{setIsModalVisible(true)}}>
     </TouchableOpacity>
</View>

@mauroduq
Copy link

mauroduq commented Jul 4, 2020

I get back...
I use this
import {TouchableOpacity} from 'react-native';

Instead
import {TouchableOpacity} from 'react-native-gesture-handler';

For some reason, The first one works as well on IOS but not on Android

@ChenhuaWANG22
Copy link

ChenhuaWANG22 commented Aug 14, 2020

for someone has to use touchables from react-native-gesture-handler for iOS:

import { Platform } from 'react-native';

let TouchableHighlight,TouchableOpacity;
if (Platform.OS === 'ios') {
    ({ TouchableHighlight,TouchableOpacity } = require('react-native-gesture-handler'));
} else {
    ({ TouchableHighlight,TouchableOpacity } = require('react-native'));
}

@jakub-gonet
Copy link
Member

jakub-gonet commented Aug 25, 2020

I wasn't able to repro that, TouchableOpacity is kinda working on android (doesn't have opacity though). Used code from the master branch (1d23e20).

Could you guys please share some small reproduction example with RNGH version, so I can test and fix that?

Example code used in GIF:

import React from 'react';
import { SafeAreaView, Text, Platform } from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';

export default () => {
  return (
    <SafeAreaView style={styles.container}>
      <TouchableOpacity
        style={[styles.rectangle]}
        onPress={() => console.log('Touch', Platform.OS)}>
        <Text>Press me!</Text>
      </TouchableOpacity>
    </SafeAreaView>
  );
};

const styles = {
  container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
  rectangle: {
    backgroundColor: 'pink',
    width: 100,
    height: 100,
    alignItems: 'center',
    justifyContent: 'center',
  },
};

Screen Recording 2020-08-25 at 15 30 12

@jakub-gonet jakub-gonet added Platform: Android This issue is specific to Android Platform: Cross platform inconsistency This issue presets the library behavior inconsistency between platforms Missing info To verify labels Aug 25, 2020
@tandat2209
Copy link

@jakub-gonet Seems like it does not work inside a Modal

Please take a look at this snack

https://snack.expo.io/@dattannguyen/touchableopacity-does-not-work-on-android

@jakub-gonet
Copy link
Member

@tandat2209, this may be related to #139.

@bluenex
Copy link

bluenex commented Sep 7, 2020

I confirm Touchableopacity doesn't have opacity when it is being tapped. I am upgrading RN version to the latest (0.63.2) and having RNGH 1.7.0 as a dependency of React-Navigation.

I used TouchableOpacity imported from react-native before and just change to import from RNGH after upgrading RN version as the one from RN doesn't have opacity on both iOS and Android. However, the one imported from RNGH only works properly on iOS. It doesn't change opacity on click on Android.

It is not on Modal either. In my case, I found it on storybook (image below).

(justify-content seems to have weird behavior after upgrading as well 😅 )

@jakub-gonet
Copy link
Member

@bluenex, could you provide some small example of code?

@bluenex
Copy link

bluenex commented Sep 8, 2020

@bluenex, could you provide some small example of code?

Here it is:

import { TouchableOpacity } from 'react-native-gesture-handler';
import styled from styled-components;

const ButtonContainer = styled(TouchableOpacity)`...`;

const StyledView = styled.View`...`;

const Button = ({ children, ...buttonProps }) => (
  <ButtonContainer {...buttonProps}>
    <StyledView>
      <Text>
        { children }
      </Text>
    </StyledView>
  <ButtonContainer>
);

So this is my custom button component which is styled using styled-components. This button is working perfectly on iOS but not Android.

More information, I use react-navigation and follow its docs here https://reactnavigation.org/docs/getting-started/. One thing I'm very curious is that it doesn't suggest adding anything to either MainActivity.java or MainApplication.java, just import 'react-native-gesture-handler'; in an entry file. Is that enough?

Update

So I end up trying to add to MainActivity.java as described in RNGH docs and it works. Probably not related to this issue anymore. Will check if react-navigation should update their docs to include this. Sorry for the confusion and thank you for a quick reply!

@SUMITIOS
Copy link

Hello,

I have the same issue right now with react-native 0.59.8 and react-native-gesture-handler 1.2.2
TouchableOpacity and TouchableHighlight from react-native-gesture-handler aren't working on a basic react-native app on Android (it works well on iOS).

I have changed the import to use the touchables from the react-native library and it works

It's not working in React native 0.62.2, 0.63.x and react-native-gesture-handler "^1.8.0" too. In fact in most versions of RN the problem is there. The onPress does not work at all. One must import it from "react-native" always for android

@flexbox
Copy link

flexbox commented Oct 29, 2020

Confirmed as well on the last version of the expo SDK

"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.3.tar.gz",
"react-native-gesture-handler": "~1.7.0",

@anastasiia-tripuz
Copy link

anastasiia-tripuz commented Jul 3, 2021

@abcdefghiraj

I moved the child component to be a sibling of the touchable component and then gave it absolute positioning such that it draws over the child. It's a stupid hack but suits my use case.

<View>
    <TextInput
       //editable={false}
      />
     <TouchableOpacity 
         style={{position: 'absolute', left:0, top:0 ,width:'100%', height: '100%'}} 
          onPress={()=>{setIsModalVisible(true)}}>
     </TouchableOpacity>
</View>

If you want to use TouchableOpacity from the react-native-gesture-handler and use position: "absolute" you should provide containerStyle prop instead of the style.

import { TouchableOpacity } from 'react-native-gesture-handler';

...

<TouchableOpacity 
    onPress={() => {}} 
    containerStyle={{ 
        position: 'absolute',
        top: 0,
        left: 0
    }}
>
   <Text>Press</Text>
</TouchableOpacity>

@jakub-gonet
Copy link
Member

I submitted PR fixing opacity effect but I wasn't able to repro this issue. If you still experience it, make sure you set up RNGH correctly and provide a working repro example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Missing info Platform: Android This issue is specific to Android Platform: Cross platform inconsistency This issue presets the library behavior inconsistency between platforms To verify
Projects
None yet
Development

Successfully merging a pull request may close this issue.