ImageView layout - android

Essentially I'm looking for something like scaleType="centerCrop" without the center.
I am having trouble making an ImageView display correctly when it's larger than the screen dimensions. I'm trying to display an ImageView that starts top=0, left=0 and is not scaled. It's okay that the image does not fit on the screen. Right now I have it in a relative layout:
<?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="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/postagram_blank_large"/>
</RelativeLayout>
I've tried using a wrap_content on the RelativeLayout height and width. I've also played with scaleType as well as using a Scrollview. The problem with the ScrollView is that the image is both taller and wider than the display port (again this is meant to happen).
How can I make this work?

could it be that android:background scales the image to the screensize? try to use android:src instead.

Try this:
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/postagram_blank_large"
android:scaleType="matrix"
/>
wrap_content instead of fill_parent also works, use whatever you want

Related

How to make a layout center inside a background?

I am using a RelativeLayout of height 400dp and width 300dp. I want it to be in the center of a background image but I can't figure out how to do so. I have tried using android:layout_centerInParent= "true" but still it doesn't work. Here is my code.
<?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="300dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
tools:context=".ChooseLanguage">
</RelativeLayout>
I can use the marginLeft and marginTop commands to get it roughly in the center but it would not be the right method as what this would work only in the screen dimensions of my preview phone, it may change as the phone changes. I want it to remain in the center of the image background irrespective of the phone it is displayed on.
I am new to android studio and any help would be appreciated
As per my above comment You need to use
android:layout_gravity="center"
instead of
android:layout_centerInParent="true"
The android:layout_centerInParent="true" will work only when the parent layout is RelativeLayout
Read more here about android:layout_gravity
Try using
android:layout_gravity="center"
or
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
or
android:gravity="center_vertical|center_horizontal"
The simplest solution will be to replace centerInParent with
android:layout_gravity="center"

Why wrap_content has no effect?

Set wrap_content in RecycleView holder:
<?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:background="#android:color/black">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:textColor="#android:color/white"
android:textSize="17dp"
android:background="#drawable/bubbleblue170x140_2" />
</RelativeLayout>
But text in TextView is not wrapped, there is a lot of empty space in the bubble, inside TextView.
How can I force to see something like this in iOS version:
In Android using 9-patch image as background.
The height of your text view is wrap and the big size is becuase of your 9patch image (probably having a big size)
And the width of that is match parent as you wanted in xml layput.
Just change the size of your 9 patch and make the width wrap content too
Resize your 9patch. You should also change the RelativeLayout's height and width to wrap_content. Or, even better, you should remove the RelativeLayout (it seems useless).

Android activity border that can not be removed?

So I have this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bgdark"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="76dp"
android:src="#drawable/myheader_250x60"
/>
<ListView
android:id="#+id/mainListview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
Note I have now edited this post. I was using a grey background image, so I thought that was what was visible to right/left of header image.See below for an image showing the problem.
However, moving the background to the listview did not solve the problem. Hence it would seem it is the imageview that somehow has a left/right border in grey. As i have set width to "match_parent" it should have taken he maximum width.
The problem is that to the top/right of header there's a grey pixel border? which I would like to remove if possible.
Try scaleType attribute for your ImageView.
Anothrer way - replace
android:src="#drawable/myheader_250x60"
for
android:background="#drawable/myheader_250x60"
Try using fill_parent instead of match_parent except in listView height . Use wrap_content in listView height
set this scletype property
android:scaleType=fitXY
and set image to imageview like this
android:background="#drawable/myheader_250x60"

How to make image fill RelativeLayout background without stretching

In my Android project, I am not quite sure how to make my background image fill the entirety of the RelativeLayout root element in XML, which is the size of the screen. I want to be sure that this works for all aspect ratios, so the image will clip vertically or horizontally as necessary. Does someone know how to do this easily? I've only seen questions regarding ImageViews and Buttons, but not really generic Views.
My XML file currently:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/enclosing_rl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:fitsSystemWindows="false">
<!-- Other elements -->
</RelativeLayout>
Other than turning your image into a nine patch I don't think this is possible. What you could do instead is-
Add an ImageView as the first view in your RelativeLayout.
Set layout_centerInParent to true.
Have the layout_width and layout_height set to match_parent.
Then set scaleType to centerCrop.
That will make sure the image fills the screen without any distortion, but depending on screen size/orientation either some of the top/bottom or left/right of the image may be cut off.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/background" />
Any other views in the RelativeLayout will appear on top of the ImageView, as long as it is the first view in the RelativeLayout (when you are in the xml).
Create a bitmap drawable XML resource in your res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" />
Use that drawable as background instead of #drawable/background
according to this answer If you want your ImageView fill your RelativeLayout,use align parameters for ImageView.
you can put all of your views to a LinearLayout and set align parameter to your background ImageView:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/my_background"
android:scaleType="centerCrop"
android:layout_alignTop="#+id/my_views"
android:layout_alignBottom="#+id/my_views"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_views"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>
</RelativeLayout>
Its smart and 100% working answer is to set the property scaleType of image view !
android:scaleType="fitCenter"

Android. ImageView won't position itself correctly

I'm trying to place an image that fills the width of the screen, has the height of the actual image (wrap_content) and sits at the very bottom of the Layout.
I've wrote the below code, but between the second ImageView (the one with drawable/user_board) and the very bottom of the screen, there is a small space. Why is this? I've tried setting padding and margins to 0dp, but it seems it doesn't do the trick. Any ideas?
Here is the code:
<?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:id="#+id/LayoutBoard"
android:noHistory="true"
android:padding="0dp" >
<ImageView
android:src="#drawable/game_interface_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix" />
<ImageView
android:src="#drawable/user_board"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:padding="0dp" />
</RelativeLayout>
Doesn't your user_board image have some transparent space at the bottom? Or maybe you can try setting negative value in the padding field of the second ImageView.
Use android:scaleType="fitXY" and the bitmap contained in your ImageView will be resized to fit the whole ImageView. So using this attribute in both your imageViews will probably hide that empty space. However keep in mind that this does not keep the aspect ratio of the image. See here for more information about the android:scaleType attribute tag and the possible values it can take.

Categories

Resources