Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Handling images size for multiple screens

I find it really hard to handle images with Android, I think it's the hardest part of Android development...

1) I have an image, I want it to be the background of my application so I do

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" 
    android:background="@drawable/accueil">
</RelativeLayout>

The background should fill the entire screen but it's not the cas. Only a part of the background is shown on some (small) screens. How could I say: I want the image to fill all the screen even if the image is bigger then the screen the image should be reduced so I see all the background?

Maybe I should put my background instead of using it as a background

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">


       <ImageView
           android:id="@+id/imageView1"
           android:layout_width="500dp"
           android:layout_height="500dp"
           android:adjustViewBounds="false"
           android:src="@drawable/accueil" />

</RelativeLayout>

Yet in some devices the image is a bit cropped.

2) I have to place a layout in another layout but I want its position to be precise, its width to be relative to the parent layout. Yet if I do

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fond">

 <LinearLayout
     android:layout_width="300dp"
     android:layout_height="350dp"
     android:layout_marginTop="20dp"
     android:background="@drawable/fondshare"
     android:orientation="vertical" >

you can see the result is not what I expected (the linear layout being the rectangle with the smiley and all the buttons) enter image description here

like image 621
morg Avatar asked Feb 07 '13 09:02

morg


People also ask

How do I deal with different screen sizes on Android?

The best way to create a responsive layout is to use ConstraintLayout as the base layout in your UI. ConstraintLayout enables you to specify the position and size of each view according to spatial relationships with other views in the layout. All the views can then move and resize together as the screen size changes.

How do I make my Android picture fit the screen?

setMinimumWidth(width); imgView. setMinimumHeight(height); imgView. setMaxWidth(width); imgView. setMaxHeight(height);

Why is it problematic to define Sizesusing pixels on Android?

The first pitfall you must avoid is using pixels to define distances or sizes. Defining dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices.


Video Answer


1 Answers

Android categorizes device screens using two general properties: size and density. You should expect that your app will be installed on devices with screens that range in both size and density. As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities. There are four generalized sizes: small, normal, large, xlargeAnd four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)

To declare different layouts and bitmaps you'd like to use for different screens, you must place these alternative resources in separate directories, similar to how you do for different language strings.

Also be aware that the screens orientation (landscape or portrait) is considered a variation of screen size, so many apps should revise the layout to optimize the user experience in each orientation.

See android.com's "Supporting Different Screens" article for more information.

like image 79
Md Maidul Islam Avatar answered Oct 26 '22 14:10

Md Maidul Islam