So to align an imageView with layout_gravity I must use wrap_content
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#drawable/heart">
</ImageView>
But If I want to scale the image in the view, I can't use layout_gravity because I need to define layout_width and layout_height.
How can I scale and use layout_gravity?
edit: I answered my own question with this solution
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:background="#drawable/heart">
</ImageView>
</LinearLayout>
Use this in your image view
android:scaleType="center"
and for more types of scaleType see here
ImageView is comes with different configuration options to support
different scaleTypes. scaleType options are used for scaling the
bounds of an image to the bounds of this view.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/heart"
android:scaleType="center"
android:adjustViewBounds="true">
</ImageView>
http://developer.android.com/intl/es/reference/android/widget/ImageView.ScaleType.html
When you use wrap_content, your ImageView takes the size of the image. The layout_gravity won't change by changing the imageView size. use android:scaleType="fitEnd" for changing the image alignment
I answered my own question. I had to nest the image view inside a parent layout, using gravity not layout_gravity
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:background="#drawable/heart">
</ImageView>
</LinearLayout>
No, you don't have to use wrap_content even if you set layout_gravity.
layout_gravity only works when I set the height and width to wrap content
Try to set the parent ViewGroup's width to match_parent to expand it full width.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="right"
android:background="#drawable/heart" />
</FramaLayout>
Related
for example, if a 80dp square layout contains a smaller ImageView, the size of ImageView will not be scaled:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#FFFF00"
android:clipChildren="true">
<ImageView
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
</RelativeLayout>
but if the parent is smaller than the ImageView, eg:30dp:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#FFFF00"
android:clipChildren="true">
<ImageView
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
</RelativeLayout>
it would be scaled automatically:
but I want the ImageView cropped instead of scaled:
how can I get this crop effect in xml?
There is alot of techniques available.
Try to Add both attribute in your ImageView xml
android:scaleType="matrix"
OR
Use margin-right,left,up,down.
OR
Search Icon size in google and then edit this icon in photoshop give it a fix size of icon .
try adding following attribute in your ImageView xml
android:scaleType="matrix"
hope it helps :)
I been struggeling with this for a whole day now. I want 3 Image Views in my LinearLayout. As you can see the scale is not correct at all. All of my icons have a size of 24x24 px. Been experimenting with different propertiers inside of my ImageViews.
Here is my XML-code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="45dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/tList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#drawable/ic_home_black_24px" />
<ImageView
android:id="#+id/ist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#drawable/ic_help_black_24px"
/>
<ImageView
android:id="#+id/ations"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#drawable/ic_settings_black_24px"
/>
</LinearLayout>
I pasted your code on a layout file in my Android Studio, an it seems that the weight and scaleType attributes are messing up your view. This is how I declared a 24 by 24 ImageView:
<ImageView
android:id="#+id/ations"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="#drawable/your_drawable"
/>
You can check full doc about weight here, but what is basically saying is that if you put weight on several attributes under a LinearLayout, you're adding a priority to grow when the screen does. Since the 3 ImageViews have 1 as weight, they grow with the same priority, and since the fill_parent is being called also, they will force to fit in the parent layout params, looking oddly.
Change all of your ImageView's width to "0dp" and replace background to src like this :
<ImageView
android:id="#+id/ations"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/ic_settings_black_24px"
/>
I have an ImageView defined as:
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-80dp"
android:layout_toRightOf="#+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="#drawable/tester"
android:adjustViewBounds="false" />
and I see this in Android Studio in the layout preview:
And notice the bounds of the ImageView. Is it possible to have the bounds be along the actual edges of the image I am displaying?
<ImageView
android:id="#+id/imageView"
android:src="#drawable/tester"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"/>
Adding the scaleType to centerCrop should fix it for you. You can change the centerCrop to any other suggestions according to your need.
you have to set the scaletype to fitCenter
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|top"
android:layout_marginBottom="50dp"
android:layout_marginTop="-80dp"
android:layout_toRightOf="#+id/prem_BACK"
android:layout_weight="0"
android:clickable="true"
android:paddingBottom="0dp"
android:src="#drawable/tester"
android:adjustViewBounds="false"
android:scaleType="fitCenter"
/>
set android:adjustViewBounds="true"
It will set the bounds of the imageView to match the content
I think, the image is too large and you re wraping it so the imageview layout will be larger than you wanted to. Crop the image seems like the only solution you can do. You can try to scale
There's something that is not working well here.
What I need: fill a part of a RelativeLayout with an ImageView with a pattern background.
What I have done: If i put defined number (in DP) to the ImageView height, it works. But it's not what I need, because the RelativeLayout has a wrap_content height. I need that the pattern fills all the relative, with margins top and bottom of 13dp. (And 10dp width).
If I put match parent to the ImageView height (What I think it's the right solution), it doesn't work (it only shows a line).
This is what I need:
http://postimg.org/image/csr16zccn/
Here is the code of the RelativeLayout:
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relative"
android:paddingBottom="13dp"
android:paddingTop="13dp" >
<ImageView
android:id="#+id/pattern"
android:layout_width="10dp"
android:layout_centerVertical="true"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:src="#drawable/row_pattern" />
<TextView
android:id="#+id/title"
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="18dp"
style="#style/p_general" />
</RelativeLayout>
And this is the row_pattern.xml
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/pattern"
android:tileMode="repeat"
android:dither="true" />
U can set layout_alginTop="#+id/title" and layout_alginBottom="#+id/title" attributes to your ImageView than your image will fill title's height.
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relative"
android:paddingBottom="13dp"
android:paddingTop="13dp" >
<TextView
android:id="#+id/title"
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingLeft="18dp"
style="#style/p_general" />
<ImageView
android:id="#+id/pattern"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alginTop="#+id/title"
android:layout_alginBottom="#+id/title"
android:src="#drawable/row_pattern" />
</RelativeLayout>
You cannot match_parent a child if the parent has wrap_content. It doesn't make sense. The child cannot match a parent which doesn't have it's dimensions defined. You can either give the parent a height, or just give your image an height. Or you can alwo wrap_content your image (the image view knows how to display your image, but in this case it will display the source's resolution and you probably don't want that).
I have added image in android, but it is fitting to middle of the screen, what can I do to set it for full screen?
what is wrap_content and fill parent, I'm not getting how to do this.
This is my code
<ImageView
android:scaleType="fitCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image" android:cropToPadding="false"/>
Try this
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
wrap_content says ImageView to fit image dimension and fill_parent to fit dimension of father layout.
Look for scaleType
<ImageView android:scaleType="fitCenter" />
You may try:
android:scaleType="fitXY"
Read more about ScaleType.