Padding not working in my Button - android

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"
/>

Related

How to rotate ImageView and have its size adjust automatically?

Image 1 is without rotation,I want image 3 when I rotate ImageView, but something like image 2 is displaying. How can I have the ImageView change its dimensions automatically?
My guess is that after rotation, the width and height of the ImageView is preserved, and I do not want that
P.S. I don't want to change the orientation of the layout
here is the picture(Rectangle is the phone, person is the imageview)
Here is my xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/page">
<ImageView
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>
</FrameLayout>
<!-- I tried all scaleTypes, and set height/width to fill_parent, wrap_content,and match_parent nothing worked-->
any help is appreciated. thanks!
Remove adjustViewBounds and use scaleType fitCenter. It should work. Try this:
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>
Try this:
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:adjustViewBounds="true"
android:rotation="90"
/>
Edit:
android:adjustViewBounds="true"
will set scaleType="fitCenter", so I think you should try scaleType or adjustBounds ,no both.
<ImageView
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/character"
android:rotation="90"
/>

ImageView Padding attribute not displaying Properly in JellyBean

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.

Android Studio Layout for 4 images

I'm having some issues with laying out an activity layout in xml, well I'm using the Design option and the layout is shown in xml below, which is all embedded in a RelativeLayout.
Basically, what I want to have is four even ImageViews which I'll be adding images too, for a Quiz and there are 4 squares, two on top and two on bottom which altogether will make a larger square.
What I have is ok, but I think there should be a better or standard way or laying out stuff like this with Android?
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:minHeight="100dp"
android:minWidth="100dp"
android:nestedScrollingEnabled="false"
android:layout_toStartOf="#+id/imageView2"
android:layout_alignTop="#+id/imageView2"
android:layout_toLeftOf="#+id/imageView2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:minHeight="100dp"
android:minWidth="100dp"
android:nestedScrollingEnabled="false"
android:layout_marginTop="94dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="89dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:minHeight="100dp"
android:minWidth="100dp"
android:nestedScrollingEnabled="false"
android:layout_below="#+id/imageView2"
android:layout_alignLeft="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:minHeight="100dp"
android:minWidth="100dp"
android:nestedScrollingEnabled="false"
android:layout_below="#+id/imageView"
android:layout_alignLeft="#+id/imageView"
android:layout_alignStart="#+id/imageView" />
You can use a GridView for the layout of your images and I think you can use layout_weight and weightSum to avoid having to set a definite size for your images and worrying about it not working properly on different screen sizes. That and the worry of images overlapping.
Here's an SO question that covers using a two column gridview for images as well, I'm not sure if this would help you either but since you said two images on top and two on bottom this might still be useful in a way. You can refer to this SO question and the marked answer for a better explanation on what layout_weight does and here's a reference for weightSum.
I have solved this problem by using the LinearLayouts, each with 2 ImageViews, nested within a parent LinearLayout for the actual form. The parent LinearLayout has the orientation="vertical" and the nested LinearLayout="horizontal".

ImageView width and height does not work

I use an ImageView in my Relative Layout.
This is the Code:
<ImageView
android:id="#+id/jpicture_imageView"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/jName_TextView"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="fitXY" />
For testing purpose, I set width and height to 10dp, but the picture is always it's original size.
What am doing wrong?
When doing this kind of resizing stuff try going from crap and adding attributes little by little (when you are not sure what you are doing ;)
<ImageView
android:id="#+id/jpicture_imageView"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/jName_TextView"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="fitXY" />
This part can be the problem :
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/jName_TextView"
android:layout_centerVertical="true"
You align the view left and right, below another view and center vertically.
With the attibutes fitXY and adjustViewBounds the view will not be only 10dp. Try changing those attributes.
Delete them and add them again one by one to see the changes for yourself
the part with
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
was the problem. Removing one of them fixed it.
Thanks for yur answers.
Best regards,
Peter

Android: layout issue with Image Views

I am keeping three image views with 3 different images of the same height. My code is as mentioned below..
<LinearLayout
android:id="#+id/LinearLayout06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" android:layout_gravity="center_vertical|center_horizontal">
<ImageView
android:id="#+id/stopServiceButton"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#null"
android:paddingRight="5dp"
android:src="#drawable/stop_service_button_selector" />
<ImageView
android:id="#+id/calibrateButton"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#null"
android:padding="5dp"
android:src="#drawable/calibrate_button_selector" />
<ImageView
android:id="#+id/doneButton"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#null"
android:padding="5dp"
android:src="#drawable/done_button_selector" />
</LinearLayout>
In large layouts i was able to manage these images(i dont have to use weight parameter for it) but for small layout i have to keep the weight parameter as one to fit them in the screen, but then the height changes as shown below.
Can someone help me out in this ?
the android:adjustViewBounds="true" preserves the aspect ratio of the images. since the width is to wrap content, the height gets reduced accordingly.
To reduce the blurring effect that you mentioned in your comment, you can try setting android:filter the attribute to the drawables.

Categories

Resources