How to set imageview fit to sreen? - android

I have use the android studio and set the image view fit to screen using relative background image i give layout top margin and i set image view fit to screen below part
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#drawable/backgroud">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="500dp"
android:background="#drawable/blacksquare"
android:alpha=".33"
/>

Write the Following ImageView Tag in your Parent Layout.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="#drawable/blacksquare"
/>
and you can also remove the Paddings from your parent Layout Tag to get fit the Image View in screen without padding..

Use following code for set image in full screen.
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blacksquare"
android:alpha=".33"
android:layout_marginTop="10dp"
ScaleType="fitXY"
/>

Related

Image taking Double the space in imageView in Android

i have put an image in Imageview, but it is taking double the space. There is large gap between the image and textview beneath it. how can i set the image to use the height as per image?
My layout is as follows:
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<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="wrap_content"
android:orientation="vertical"
android:gravity="center" android:padding="5dp" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/babyfa"
android:scaleType="fitStart"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/whatis"
/>
I have attached the imge, the red line represents the extra space the imageview is taking. kindly advise.Image
This is likely being caused by android:scaletype="fitStart". Try removing this from your XML. See this helpful guide for the effects of different scaletype properties (fitStart is at the bottom).
Edit: Try also setting android:adjustViewBounds="true". This will hopefully force the ImageView to preserve the image's aspect ratio.

Putting an image inside another image in Android

I am trying to achieve the following screenshot in android.
To get Round Image I have Round Image Library from here:
https://github.com/vinc3m1/RoundedImageView
So I tried the following code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/marker"
android:orientation="horizontal" >
<com.ylg.RoundImageView
android:id="#+id/profilePhoto"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center_horizontal"
android:src="#drawable/profilepic" />
but I ended up getting error:
NOTE: This project contains Java compilation errors, which can cause rendering failures for custom views. Fix compilation problems first.
Exception raised during rendering: Index: 0, Size: 0
I need 9 patch to my marker image: Which I did below:
What could be wrong here. How do I achieve the abouve screenshot in my layout xml?
just play with margin to fit it exactly:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="130dip"
android:layout_height="130dip"
android:background="#drawable/bg" >
<com.example.mmlooloo.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/imageView1"
android:layout_width="90dip"
android:layout_height="90dip"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dip"
android:layout_marginTop="10dip"
android:scaleType="centerCrop"
android:src="#drawable/profile"
app:border_color="#0000FF"
app:border_width="2dip"
app:corner_radius="80dip"
app:oval="true" />
</RelativeLayout>
</LinearLayout>
and:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
result:
Can't you use FrameLayout like this?
<FrameLayout 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="com.example.testproject.MainActivity" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/bg" >
</ImageView>
<ImageView
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_launcher"/>
</FrameLayout>
I used your border image as #drawable/bg and I am getting this output:
In your case, image of user will be smaller than border image, so that should fit.
Hope this helps.
Try setting the background of ImageView to a drawable(xml) resource containing the background image and set the ImageView src to your desired picture. This should solve your problem.
EDIT
Try this code
<ImageView
android:id="#+id/img_chatUserImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#drawable/bb"
android:paddingBottom="40.0dip"
android:paddingTop="10.0dip"
android:paddingRight="10.0dip"
android:paddingLeft="10.0dip"
android:src="#drawable/ic_smiley"/>
where 'bb' is the backgournd white image and src contains the actual image tat is displayed over it.
I have adjusted my padding accordingly.If the image size varies then wrap the imageview in another LinearLayout and set the background and padding to it and position the image view inside it and fix the size so it always remains inside the white border

Android - ImageView as header

I want to display an image on top of my activity that always fill the screens width. If I set its width to match parent and its height to wrap content there is a white stripe at the sides of the image on larger screen. How can I make that this image always fill the screen horizontally?
My 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="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/hello_world"
android:src="#drawable/tbbt" />
</RelativeLayout>
Thanks in advance.
change imageview to fit the complete screen width
android:scaleType="fitXY"
or
android:scaleType="centerCrop"

Center ImageView inside another ImageView in Android

Well I have to fit one ImageView inside another one. It is smaller and has to be exactly at the center. I have both images scaled for different screen resolutions but I can test only on one phone and I am wondering if I set the second image's height and width with dpi to fit my screen resolution will it be perfectly matched on all screens or is there any property in Android aside from setting the height and width that will automatically center one ImageView inside of other ImageView?
Edit: It's in RelativeLayout and the ImageViews are at the top left so I haven't added anything specific to the because they by default are there.
Edit2: Well changing the first ImageView to be RelativeLayout helps center the second image inside of it but then the RelativeLayout grows too big and enlarges the image that is background. Is there a way to make the RelativeLayout be as big as its background?
try 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"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="250dp"
android:layout_height="250dp" >
<ImageView
android:id="#+id/base"
android:layout_height="250dp"
android:layout_width="250dp"
android:src="#drawable/home"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/center"
android:layout_height="150dp"
android:layout_width="150dp"
android:src="#drawable/home"
android:layout_centerInParent="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
You cannot put an imageView inside another. Hope the following code will be helpful for you.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_image"
android:gravity="center" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image_on_center" />
</LinearLayout>
Use RelativeLayout and then use android:layout_centerInParent="true" remember the order you add the ImageViews will decide what is on top :)
Try this:
android:layout_gravity="center"

ImageView not displaying in LinearLayout

My goal is to display a couple of buttons and then a PNG image. The problem is that the image doesn't display at all. If I take out all the buttons, then the Image will display. What exactly is wrong with this xml file?
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:adjustViewBounds="true"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option1"
android:onClick="sendMessage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/OR" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/option2"
android:onClick="sendMessage" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/Pic"
android:src="#drawable/soup" />
</LinearLayout>
LinearLayout will lay out each child until it runs out of space. The ImageView won't be on screen if the first three children take at least as much space as the width of the screen (I say width because LinearLayouts are horizontal by default -- if you intended it to be vertical, you must include the android:orientation="vertical" attribute).
You either need to specify layout weights to give each chid a certain portion of the available space, or you need to choose another layout that better suits your needs.
Set your linearlayout with "android:orientation" to horizontal or vertical. That might fix it.
There is nothing wrong. As you are using "wrap_content" for individual views , you are probably crossing the layout bounds. You can set layout weights in this case to fit all.

Categories

Resources