ImageView Padding attribute not displaying Properly in JellyBean - android

I have same xml code but when I am running it in Lolipop the padding attribute is working fine but in JellyBean its not working also In my ImageView I am using an oval from drawable that is set with background attribute and transparent wifi image that has set by src attribute
<ImageView
android:id="#+id/imageView"
android:layout_width="42dp"
android:layout_height="42dp"
android:padding="8dp"
android:background="#drawable/icon_circle_primary"
android:src="#drawable/ic_info_black_48dp" />
Here the screenshot from both version

Try putting the ImageView inside of a FrameLayout and put the android:padding="8dp" and the android:background="#drawable/icon_circle_primary" attributes to the FrameLayout.
Something like:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#drawable/icon_circle_primary"
android:padding="8dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="#drawable/ic_info_black_48dp" />
</FrameLayout>

Just in case this will help someone, I had a problem where the ImageView padding was not working for me where I also had android:scaleType="centerCrop" set.
Adding android:cropToPadding="true" did it for me.

Related

Is there any way to make background image dark in Android

I want to darken the background of a Textview and when I lower the alpha of the background it kinda becomes white. Can anyone help?
Here is my code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:gravity="center">
<ImageView
android:id="#+id/bg_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/abg32"
android:alpha="0.6"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:textAlignment="center"
android:textSize="17sp"
android:textColor="#color/colorWhite"
tools:text="#tools:sample/lorem/random" />
</RelativeLayout>
I have tried using the tint attribute but it doesn't work.
First Approach:
I can see that you have mentioned the tint attribute didnt work, could you provide the code you wrote? It could be some error in it. Either way you could perhaps try this and see if it works.
<ImageView
...
app:tint="#6F000000"/>
Second Approach:
Add a view on-top of you ImageView and set the background as color and opacity desired
<View
android:id="#+id/overlay_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background=“#6F000000" />
*Make the width and height as same as your ImageView's height and width

Round Corners using CardView and ImageView

I am trying to round the corners of an ImageView in android xml development. I use a parent of CardView with CornerRadius and I made sure the API is above 19 to be able to use the elevation feature. For the moment I don't want to use any 3rd party, even though I tried and that didn't work either for unknown reasons.
<androidx.cardview.widget.CardView
android:id="#+id/profilecardview"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
app:cardCornerRadius="100dp"
android:elevation="10dp"
app:cardPreventCornerOverlap="false">
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/profpic" />
</androidx.cardview.widget.CardView>
Thank for the help!

Padding not working in my Button

In my Button I have this XML:
<Button
android:id="#+id/btnFiltrarResultados"
android:layout_width="18dp"
android:layout_height="17dp"
android:layout_above="#+id/searchView"
android:layout_alignParentEnd="true"
android:layout_marginBottom="-37dp"
android:layout_marginEnd="29dp"
android:background="#drawable/filtrar_explorar"
android:cropToPadding="true"
android:padding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/txtExploreTitulo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.01999998" />
But I can't seem to be able to add padding to it. I've tried using cropToPadding or using android:src but nothing seems to help...
You have
android:layout_width="18dp"
android:layout_height="17dp"
and expect padding 20dp
Well it's not a way to add image in button, as you did in your xml fileandroid:background="#drawable/filtrar_explorar". because by default background image try to scale as much as possible and ignore padding.So the good practice is use ImageButton with android:src="#drawable/use_your_image" and add android:scaletype="fitCenter"
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/use_your_image"
android:scaleType="fitCenter"
android:padding="10dp"
/>

ImageView bounds extend beyond image

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

How do I remove extra space above and below imageView?

I have a really simple image within a RelativeLayout and for some reason I am getting extra spacing on the top and bottom which I can't remove. How can I clear it out?
Here is what it looks like:
Here is the code:
<?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:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/slice11pp" />
</RelativeLayout>
Try this
android:adjustViewBounds="true"
In your above imageView just add android:scaleType="fitXY"
Your problem is probably that you have no scale type set... Add it to the XML for the ImageView, "fitCenter" should be correct, but there are others, check: ScaleType.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/slice11pp"
android:scaleType="fitCenter" />
Must be enough adding the property:
android:adjustViewBounds="true"
for example:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/slice11pp"
android:adjustViewBounds="true" />
If your image view height is match_parent, even if you set android:adjustViewBounds="true" ImageView will add some extra blank space at top and bottom side. so, change ImageView height to wrap_content and set android:adjustViewBounds="true"
for example
<ImageView
android:id="#+id/img_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
Use drawable-nodpi folder if there is no specific requirement for images. Then android: adjustViewBounds = "true" acts as the default.
If you use drawable-nodpi you don't need to set android:adjustViewBounds = "true".
I think this is the most effortless method.
android:scaleType="centerCrop" by adding this
<ImageView
android:id="#+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#drawable/temp_image"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
works for me Before and After Image
this line of code will solve the problem
android:adjustViewBounds="true"
don't forget to rotate the image
just add ScaleType="fitxy" inside the Image view
Try this
<ImageView
(...)
android:adjustViewBounds="true" />
this is the perfect solution
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
it will not leave any extra single dp in any side. however the image will be distorted if it is not portrait image.

Categories

Resources