The relative layout leaving spaces where it should not? - android

Hi I am using a relative layout to align the image at the bottom but the image is giving away some margin at the top and at the bottom. My code is as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/back" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="#drawable/compare1footer"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>
Can someone help me out with this. Thank you
I tried using Fllo answer and i am attaching the screenshot
I am uploading the png

I think it's because you use android:layout_alignParentTop="true" at the same time. You don't need a parent LinearLayout but you need to set your RelativeLayout height to match_parent. Try as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/compare1footer"
android:layout_alignParentBottom="true"
android:scaleType="FIT_XY" />
</RelativeLayout>
As Rat-a-tat-a-tat Ratatouille suggested, I think it's the right way to avoid deformation with your image. The attribute scaleType according to the Reference is to:
Scale in X and Y independently, so that src matches dst exactly. This may change the aspect ratio of the src
It's not a bad idea.

try changing this:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="#drawable/compare1footer"
android:layout_alignParentBottom="true" />
to
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/compare1footer"
android:layout_alignParentBottom="true" />

Replace your code for Image View from:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="#drawable/compare1footer"
android:layout_alignParentBottom="true" />
To:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="#drawable/compare1footer"
android:adjustViewBounds="true"
android:layout_alignParentBottom="true" />
android:adjustViewBounds="true" is the solution of your problem.
Hope it will help you..

Try to change:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/back" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="#drawable/compare1footer"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>
to this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/ic_launcher" >
</ImageView>
</RelativeLayout>

Related

Scrollview empty space issue

I wanted to create the guideline in my apps,but i found that this empty space is on top of my image, how do i delete it to let my image stay on the top?Below is my layout.xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InfoActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/slide1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/slide1"
android:contentDescription="firstslide" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/HELLO"
android:id="#+id/textView" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Try adding this in your ImageView.
android:scaleType="fitStart"
Hope it helps.
I fixed it with android:scaleType="fitXY"

How do I center an image with no stretching on my Android XML layout Splash screen

Here's my code. It's stretching my image to the width of the screen. I just want it centered with no stretching. What am I getting wrong here?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="#+id/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FFFFFF"
android:src="#drawable/splash_screenhdpi" />
</RelativeLayout>
You need to set a scaleType
android:scaleType="center"
i don't understand very good your question, i hope be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="#+id/splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#FFFFFF"
android:src="#drawable/splash_screenhdpi"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scaleType="center" />
</RelativeLayout>
You have to use the attribute scale for ImageViews. Use center just to center the image in the image default size, and match_parent as the size for width and height.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="#drawable/ic_launcher"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Unable to align ImageView to bottom of RelativeLayout

I've read several tutorials on how to do this, but for whatever reason, my image won't budge from the center of the screen. The ninjas image is 1080x517px
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="#color/white">
<ImageView
android:id="#+id/ninjas"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ninjas"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
From what I can tell, this is correct. Any suggestions?
Thanks in advance
You make the image fill the parent currently. Change
android:layout_width="fill_parent"
android:layout_height="fill_parent"
to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
try this
<?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="#ffffff" >
<ImageView
android:id="#+id/ninjas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Try changing
<ImageView
android:id="#+id/ninjas"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ninjas"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
to
<ImageView
android:id="#+id/ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ninjas"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
At present your ImageView is occupying all the space in its parent. After changing layout_width and layout_height to wrap_content it will only occupy as much space as size of #drawable/ninjas
// Try this way,hope this will help you to solve your problem...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:gravity="bottom|right"
android:padding="5dp">
<ImageView
android:id="#+id/ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ninjas"
android:adjustViewBounds="true"/>
</LinearLayout

Android. ImageView wrap in FrameLayout

I want imageView wrap in FrameLayout. And ImageView show fullscreen.
1 variant:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff00" >
<FrameLayout
android:id="#+id/fl_draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00ff00">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
</FrameLayout>
</RelativeLayout>
(source: cs412821.vk.me)
2 variant:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff00" >
<FrameLayout
android:id="#+id/fl_draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00ff00">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
</FrameLayout>
</RelativeLayout>
(source: cs412821.vk.me)
AdjustViewBounds not working.
What I'm trying to achieve:
(source: cs412821.vk.me)
How do I handle this? Thank you very much. Sorry for my english.
Change your imageview attributes from :
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
To:
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/vertical_test"
android:adjustViewBounds="true" />
It also depends on how tall your image is.. If you know the width and height, you can adjust it accordingly.
Also, you can use gravity attribute if you want the want the image to be in the center position.

Vertical and horizontal center

I am able to center my image horizontal, but not vertical.. any idea's what might cause this? It should be in the center of the screen. I have also tried android:layout_gravity="center_vertical" but doesn't seem to do the trick...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical" >
<ImageView
android:id="#+id/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/splash" />
</LinearLayout>
A LinearLayout does not allow this behavior. Try a RelativeLayout instead.
<?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"
android:orientation="vertical" >
<ImageView
android:id="#+id/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/splash" />
</RelativeLayout>
Why do you need a LinearLayout at all? You can just use the ImageView with the scaleType=center
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/splash"
android:src="#drawable/splash"
android:scaleType="center" />
That's your whole xml...
You will have to use RelativeLayout for such behavior. I can't think of a good way to do it with LinearLayout. Additionally, you can try the Graphical Layout (depends on the editor you are using).
I used two images in invisible mode just for create space.
Try this:
<LinearLayout 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"
android:orientation="vertical" >
<ImageView
android:id="#+id/fake"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:src="#drawable/splash"
android:visibility="invisible" />
<ImageView
android:id="#+id/splash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/splash" />
<ImageView
android:id="#+id/fake"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/splash"
android:visibility="invisible" />
</LinearLayout>

Categories

Resources