Click here to Skip to main content
15,884,997 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
i added an image in Android studio to my view. The image is about 1cm from the bottom in the xml view in Android studio.

When i load the program into my phone then half of the image is outside of the screen area.

I have set the view device to Nexus 5 (5inch) and my phone is Galaxy S4 (also 5inch). So there should be no difference there.

Here is how it looks in the editor:
<img src='http://s15.postimg.org/4gbo4yzkn/alignemtn.jpg' border='0' alt="alignemtn" />


Thanks in advance!

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.testo.testapp.testapp.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>


XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$CarrotFragment"
    android:removed="#ffffff">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textSize="@dimen/abc_text_size_large_material"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/carrot"
        android:layout_gravity="center"
        android:layout_marginBottom="45dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:src="@drawable/carrot"
        android:layout_alignWithParentIfMissing="false"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>


What I have tried:

I have tried to tinker with all kinds of alignment options but nothing seems to work.
Posted
Updated 18-Feb-16 0:58am
v3
Comments
Sergey Alexandrovich Kryukov 15-Feb-16 17:47pm    
"About 1cm" makes no sense at all. Isn't it obvious that it all depends on the screen size in pixels and image position/size in pixels?
Why using the section "What I have tried" without actually telling us what you have tried? No code sample, no nothing...
—SA
elelont2 16-Feb-16 4:32am    
I will try to post some code in the evening.

Buyt what is did was take a random picture, some 100*100px and placed that inside an imageView. I placed the imageview in my view in Android studio so that it was about 1cm from the bottom of the screen in the xml view with Nexus 5.

For some reason the image is displayed partially out of screen on the Galaxy S4.

The source of my confusion lies in the fact that both of these devices have a 5" FullHD screen. Why would the image be displayed differently.

1 solution

Your relative layout contains "fill_parent" for height. Therefore it is trying span whole screen area. Then you have image view in "wrap_content". Therefore it is taking much area so that image could be shown full.
If you really want to fit all view inside display area you need to use weight_sum in parent view and and layout_weight in child view appropriately for manipulating height so that everything dynamically fits into display area, and noting goes out of screen area.
 
Share this answer
 
Comments
elelont2 24-Feb-16 9:26am    
Hi,
thanks for the reply, unfortunately i'm still a bit confused as to the "why".

My relativeLayout is filling up the screen, my imagebox is INSIDE this relativeLayout, therefore it should not go outside of the relativeLayout component.

Also, why does this behavior not show up in the AndroidStudio xml view?
Here is how it looks in the editor: http://s22.postimg.org/ubzl669kh/Clipboard02.jpg

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900