Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize TouchableOpacity according to the component present inside it?

I'm using TouchableOpacity to make a button as I'm learning react-native.

But the problem is, TouchableOpacity takes 100% width of the screen. But, I want to take the size/grow with the component present inside it.

How can I do it?

import React, { Component } from "react";
import { Text, TouchableOpacity } from "react-native";
export default class App extends Component {
    render(){
        return(
        <TouchableOpacity
            onPress={() => console.log("Pressed!")}
            style={{ backgroundColor: "red" }}
        >

            <Text>Press me!</Text>

        </TouchableOpacity>
        );
    }

When I decrease the width of the TouchableOpacity like 10 or 20, it automatically increases its height to fit the Text.

So, it possible for TouchableOpacity to grow with the size of component present inside it?

like image 698
ssh Avatar asked Apr 09 '18 00:04

ssh


People also ask

Can you style TouchableOpacity?

This component fades out when pressed, and fades back in when released. We can style it however we want, just like a View . We can configure the pressed opacity with the activeOpacity prop.

How do I change the size of a button in React Native?

How do I change the size of the button text in React Native? Unfortunately, according to the documentation (https://reactnative.dev/docs/button) you can't change a font-size of a button. The only style prop you can change is color .

How do you change opacity of TouchableOpacity in React Native?

On press down, the opacity of the wrapped view is decreased, dimming it. Opacity is controlled by wrapping the children in an Animated.View , which is added to the view hierarchy. Be aware that this can affect layout.

How do you increase touchable area in React Native?

You can use the View#hitSlop property to increase the touchable area. This can be useful in scenarios where you know that the increased touch area won't overlap with other touchables. A more robust solution is to use the padding style property.


1 Answers

You can actually use alignSelf: 'flex-start' or 'flex-end' depending on which side you want to pin

like image 145
The1993 Avatar answered Oct 10 '22 06:10

The1993