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.

TouchableOpacity button is not working with absolute position + transform animation

See original GitHub issue

Description

I am trying to implement a Floating action button using React-Native. I was able to implement this in iOS, But when it comes to android it’s not working. The issue is I cannot press the button. I know that position:'absolute' is not working very well in Android.

React Native version:

Run react-native info in your terminal and copy the results here. System: OS: macOS Mojave 10.14.6 CPU: (4) x64 Intel® Core™ i5-4278U CPU @ 2.60GHz Memory: 26.49 MB / 8.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 10.16.2 - /usr/local/bin/node Yarn: 1.10.1 - /usr/local/bin/yarn npm: 6.9.0 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman Managers: CocoaPods: 1.7.5 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1 Android SDK: Not Found IDEs: Android Studio: Not Found Xcode: 11.2.1/11B500 - /usr/bin/xcodebuild

npmPackages: @react-native-community/cli: Not Found react: 16.11.0 => 16.11.0 react-native: 0.62.1 => 0.62.1

Expected Results

I want to show an Alert message in Android when user clicks on the button

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

snack Link : https://snack.expo.io/z88E3rOGm

import React, { useState } from 'react';
import {
    View,
    Text,
    TouchableWithoutFeedback,
    Animated,
    Image,
    StyleSheet,
    Alert,
    TouchableOpacity,
} from 'react-native';

const DrinksFAB = () => {
    const [animation] = useState(new Animated.Value(0));

    const labelPositionInterploate = animation.interpolate({
        inputRange: [0, 1],
        outputRange: [-30, -70],
    });

    const opacityInterpolate = animation.interpolate({
        inputRange: [0, 0.8, 1],
        outputRange: [0, 0, 8],
    });

    const labelStyle = {
        opacity: opacityInterpolate,
        transform: [
            {
                translateX: labelPositionInterploate,
            },
        ],
    };

    let _open = 0;
    const toggleOpen = () => {
        const toValue = _open ? 0 : 1;

        Animated.timing(animation, {
            useNativeDriver: true,
            toValue: toValue,
            duration: 200,
        }).start();
        _open = !_open;
    };
    let range = 0;
    return (
        <>
            {[1,2].map((i) => {
                range = -70 + range;
                return (
                    <TouchableOpacity
                        style={[styles.button]}
                        onPress={() => {
                            Alert.alert('hey');
                            //Alert.alert('hey');
                        }}
                    >
                        <>
                            <Animated.View
                                style={[
                                    styles.button,
                                    styles.other,
                                    {
                                        transform: [
                                            {
                                                scale: animation,
                                            },
                                            {
                                                translateY: animation.interpolate(
                                                    {
                                                        inputRange: [0, 1],
                                                        outputRange: [0, range],
                                                    },
                                                ),
                                            },
                                        ],
                                    },
                                ]}
                            >
                                <Animated.Text
                                    style={[styles.label, labelStyle]}
                                >
                                    Coffee
                                </Animated.Text>
                            </Animated.View>
                        </>
                    </TouchableOpacity>
                );
            })}

            <TouchableOpacity
                onPress={toggleOpen}
                style={[
                    styles.button,
                    styles.pay,
                ]}
            >
               
                <Text>Pay</Text>
            </TouchableOpacity>
        </>
    );
};

export default DrinksFAB;

const styles = StyleSheet.create({
    button: {
        width: 60,
        height: 60,
        alignItems: 'center',
        justifyContent: 'center',
        shadowColor: '#333',
        shadowOpacity: 0.1,
        shadowOffset: { x: 2, y: 0 },
        shadowRadius: 2,
        borderRadius: 30,
        position: 'absolute',
        bottom: 20,
        right: 20,
        backgroundColor: '#ffff',
    },
    pay: {
        backgroundColor: '#00B15E',
    },
    other: {
        bottom: 5,
        right: 0,
        backgroundColor: '#e2e2e2',
    },
    label: {
        flex: 1,
        flexWrap: 'nowrap',
        color: '#000',
        position: 'absolute',
        fontSize: 14,
    },
});

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:21
  • Comments:11

github_iconTop GitHub Comments

22reactions
niubajr19commented, May 12, 2021

You can just set zIndex:1 in the parent container. Automaticaly, the children containers will be able to be click.

13reactions
Aashu-Dubeycommented, Apr 22, 2020

I’m having exact same problem with react-native version 0.61.5 I had to use TouchableNativeFeedback as a workaround for android.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TouchableOpacity not working inside an absolute positioned ...
In my case the touchable items were inside a FlatList . Using FlatList from 'react-native-gesture-handler' solved this issue for me.
Read more >
TouchableOpacity not working inside an absolute positioned ...
... TouchableOpacity not working inside an absolute positioned View [ Beautify Your Computer : https://www.hows.tech/p/recommended. html ] ...
Read more >
Create a Multi-Step Animated Modal Depending on User ...
Now we need to create our modal. We could use Modal but we will focus on the animations and just setup our modal...
Read more >
RN Screen Transition: create a button that transforms into a ...
import React from 'react'; import { TouchableOpacity, Text, StyleSheet } from 'react-native'; // I put the button in absolute position ...
Read more >
Example to Align a View at the Bottom of Screen in React Native
To do this we will use the Stylesheet element position: 'absolute'. ... global react-native-cli package, please remove it as it may cause unexpected...
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