Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – resizeMode ‘contain’ not working in react native

Posted on November 11, 2022 By

Solution 1 :

Resize mode contain

Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

You can read more about that here resizeMode

In Your case you give height * 0.3 it take total screen heigth of 30%.

Solution 2 :

considering your image does not have padding around, I think this should make it look like your expected result

pass resizeMode in style instead as a prop

    <Image 
        source={Logo} 
        styles={{ width: width * 0.6, height: width * 0.6, resizeMode: 'contain' }} 
      />


const styles = StyleSheet.create({
  root: {
    alignItems: 'center',
    justifyContent: 'center',
    padding: 20,  
  },
});

Problem :

I have a logo I want to put in the middle of the screen but after defining the dimensions of the logo, it seems doing resizeMode=’contains’ doesn’t behave like it should.

If I use resizeMode='contain', my screen appears with the image overextending the page. I could just use resizeMode='center' to make it appear in the center like this but I’d like to know why contain doesn’t seem to work. I’ve tried adjusting the width and height of the logo dimensions but I keep getting the same result.

I’m using Expo Go on Android to display the screen, and React Native v0.68.2.

my code:

import {View, Text, Image, StyleSheet, useWindowDimensions} from 'react-native';
import Logo from '../../../assets/images/logo.png';
import CustomInput from "../../components/CustomInput";

const SignInScreen = () => {
  const {height} = useWindowDimensions();
   
  return (
    <View style={styles.root}>
      <Image 
        source={Logo} 
        styles={[styles.logo, {height: height * 0.3}]} 
        resizeMode='center'
      />

      <CustomInput/>
    </View>
  );
};

const styles = StyleSheet.create({
  root: {
    alignItems: 'center',
    padding: 20,  
  },
  logo: {
      width: '70%',
      maxWidth: 300,
      maxHeight: 100, 
  },
});
export default SignInScreen;

edit 1: I managed to get the logo to not cover the whole screen. But when I try adjusting the proportion the logo takes of total screen height like user DhavalR suggested or passing resizeMode in style suggested by user Bhavya Koshiya, it doesn’t affect the size of the image and just stays constantly like this.

READ  [FIXED] android - Flutter AWS : how to authenticate IAM role user of bucket and get AWS S3 object of hls video file and play in video player?
Powered by Inline Related Posts

what I changed the code to:

<Image 
        source={Logo} 
        style={[styles.logo, {width: height * 0.4, height: height * 0.4, resizeMode: "contain"}]}       
        // resizeMode="contain"
      />

the only thing now that works to adjust the size of the logo on my screen is if I used this:

<Image 
        source={Logo} 
        style={[styles.logo, {width: 150, height: 200}]}       
        resizeMode="contain"
      />

But only changing the number for width makes the logo bigger/smaller, and height doesn’t seem to do anything. I’m still unsure why height: height * 0.3 doesn’t work.

Android Tags:android, react-native

Post navigation

Previous Post: [FIXED] android.media.ImageWriter produces green screen screen on every second posted Image frame
Next Post: [FIXED] android – Notifee native module not found

Related Posts

[FIXED] java – Automatically show result of a calculator Android
[FIXED] java – Although I use AsyncTask the App doesn’t respond Android
[FIXED] java – RxJava Android: Observer incompatible types error Android
[FIXED] android studio – How to display an image saved as “/storage/emulated/0/Download/IMG_1582623402006.jpg” in emulator Android
[FIXED] android – Can’t access onActivityResult zxing for fragment Android
[FIXED] android – After using “pushReplacementNamed” in flutter I cannot push this Route again Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme