Change the Image Size without changing its Place Holder - android

I have some ImageView in a Horizontal LinearLayout. I set their size in percentage mode.
<LinearLayout
android:weightSum="1"
android:orientation="horizontal" >
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:scaleType="fitStart"
android:src="#drawable/pjc_news_icon" />
</LinearLayout>
now if I install my software in a small size device, the width of images will get smaller to fit specified percent of their parent layout. their height will get smaller too. but their place holder's height doesn't change. I mean they have an useless blank space in their bottom.

try adding another view so that it covers up the remaining space.
<LinearLayout
android:weightSum="1"
android:orientation="horizontal" >
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:scaleType="fitStart"
android:src="#drawable/pjc_news_icon" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="0.75"
/>
</LinearLayout>
Just give a try to this, hop it works for you.

I found the answer by myself.
I added the "adjustViewBounds" property to my ImageView and set it's value true. then the space below the ImageView removed.
<LinearLayout
android:weightSum="1"
android:orientation="horizontal" >
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:scaleType="fitStart"
android:src="#drawable/pjc_news_icon" />
</LinearLayout>

Related

Size of imageView in LinearLayout

I have a horizontal LinearLayout with 3 ImageViews, but ImageView are much more wider than image are(rectangle when image is square). I've try to set adjustViewBound="true", than imageView become square but much more bigger.
Also I've tryed to set scaleType="firXY" but then the image is scaling to fit rectangle imageView.
Here is ma xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llButtons">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_start_text"
android:id="#+id/imgStart"
android:layout_weight="1.5"
android:src="#drawable/ic_play"/>
<ImageView
android:id="#+id/imgReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="#string/button_reset_text"
android:src="#drawable/ic_cls"/>
<ImageView
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="#string/button_next_text"
android:src="#drawable/ic_next"/>
</LinearLayout>
And this is a result:
How can I set ImageView size as picture size?
You had assigned the weight as 1.5 and this makes the issue.
Anyway You can adjust the width of each images By previewing the layout.
Preview the layout
Now Double Click on the layout Content.
Now You will be able to adjust the height and width of those imageviews.
Adjust it with proper height and width.
This gives you the proper idea how to manage items in a Linear Layout.
Try setting scaletype to CENTER_INSIDE
I assume you want to keep the images equidistant horizontally. Create a LinearLayout for each image, give each a weight of 1 (remove weights from ImageViews), and set each LinearLayout's gravity to center.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llButtons"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_start_text"
android:id="#+id/imgStart"
android:src="#drawable/ic_play"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:id="#+id/imgReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reset_text"
android:src="#drawable/ic_cls"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_next_text"
android:src="#drawable/ic_next"/>
</LinearLayout>
</LinearLayout>
Thank you all, I've solved it.
Following advice of #Khizar Hayat I've delete wight attribute and use wrap content instead. To place my ImageViews by screen sides and center I've using .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/llButtons">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_start_text"
android:id="#+id/imgStart"
android:src="#drawable/ic_play"/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<ImageView
android:id="#+id/imgReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reset_text"
android:src="#drawable/ic_cls"/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<ImageView
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_next_text"
android:src="#drawable/ic_next"/>
</LinearLayout>
Fix ImageView's size with a specific size (in dp) or use fill_parent
set android:scaleType to fitXY.

Flexible Horizontal Layout with `layout_weight` and `maxWidth`

I have a TextView and a square ImageView that I would like to show in a horizontal linear layout. Each should take half of the parent view's width and the height should fit the content (i.e. the image). The text should be centered vertically.
Additional constraint is that the image should not grow beyond a given maxWidth (= maxHeight), and excess width should be made available to the TextView. Obviously, this conflicts with the 50/50 rule above. Is there a way to prioritize the constraints, i.e. allow the image to take half of the space unless it exceeds a given size?
These are layouts I tried:
The first one nicely stretches the left side to the available space. But the image takes more than half of the width as its layout_weight is not specified (as in image below).
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some text."/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:adjustViewBounds="true"
android:src="#drawable/image" />
</LinearLayout>
When I add layout_weight to the ImageView it always takes half of the width and ignore the maxWidth (see image below).
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="200dp"
android:adjustViewBounds="true"
android:src="#drawable/image" />
Enclosing the ImageView in another LinearLayout enables the maxWidth again, but the enclosing LinearLayout still takes half of the available space (see image below).
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="200dp"
android:adjustViewBounds="true"
android:src="#drawable/image" />
</LinearLayout>
Another option I have found for similar scenarios is to use a RelativeLayout with an invisible view as a center divider, but that locks the division to 50/50 (or wherever the divider is placed).
Okay, gonna go on ahead and post the xml I created, it's pretty much simple, it does most of your conditions.. One thing I'm having trouble with is the part where each view takes half of the parent view's width. Hope this still helps you in some way.
sample_layout.xml
<?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="wrap_content"
android:layout_margin="2dp"
android:background="#android:color/darker_gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/img_sample"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="2dp"
android:layout_toLeftOf="#id/img_sample"
android:background="#color/colorPrimary"
android:gravity="center_vertical"
android:text="Some text." />
<ImageView
android:id="#id/img_sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="2dp"
android:adjustViewBounds="true"
android:maxWidth="200dp"
android:src="#drawable/sample_img" />
</RelativeLayout>
Here is a sample screenshot:
If ever you figure out how to the the half of each thing, kindly comment it here, It'd be nice to know about it for possible future use. :)
PS: Have you considered doing the half of each thing programatically? Like checking the LayoutParams then dynamically setting weightSum and the layout_weights.
Sample Image retrieved from this link
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#color/blue"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toStartOf="#id/imageView"
android:background="#color/colorPrimary"
android:padding="10dp"
android:text="Some text. " />
</RelativeLayout>
It won't be a perfect solution in term of performance. But you can achieve it by playing with FrameLayout and ImageView.
Here is the output:
Layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/textGray"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/skyBlue">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:src="#drawable/ic_settings"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="!" />
</FrameLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="200dp"
android:src="#drawable/ic_settings" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
In order to achieve desire behaviour, You would have to set same images to both imagesviews in this layout. one of them is invisble, It is just helping to make parent with same width and heights which would lead us to create TextView with same dimensions.
Note: If you want to change textView alignment, you can do it with layout_gravity or gravity.

Images height too tall

I am trying to setup a section of a screen with 4 images around each other but there is huge white space between them as per image example:
I would like them all right next to each other. Below is the XML I am using:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageView1"
android:src="#drawable/test1"
android:layout_weight="1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/test"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/test1"
android:layout_weight="1"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#drawable/test"
android:layout_weight="1"/>
</LinearLayout>
I am also trying to get them to the top of the screen but that white space is preventing that.
The images you see dont have any white space at all, the edge of the colors are the edge of the image?
Thanks
You need to set the scaleType to centerInside and the adjustViewBounds to true like this: (adjust view bounds should get rid of the space you don't want.)
<ImageView
...
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:adjustViewBounds="true" />
#x10sion:For your information,
Why should i add this
android:scaleType="centerInside" is going to center the image inside the container, rather than making the edges match exactly. Reference
android:adjustViewBounds="true" this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
This: android:layout_height="wrap_content"
should be like this instead: android:layout_height="fill_parent", or android:layout_height="match_parent", on your four ImageViews.

I can't place two images on the center of layout

I try to place the two images in the center of layout,
but I have the extra space on the top and on the bottom of my layout.
Images are png files.
Important part of my XML is below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/backDark2"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageDriverProfileFace"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/driver"
/>
<ImageView
android:id="#+id/imageDriverProfileCar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/car"
/>
</LinearLayout>
Any ideas please how to remove mentioned extra space?
Thanks!
set android:adjustViewBounds="true" param to your imageview's

Button taking too much space

I still dont really get it how many space layouts take.
I have the following xml.
The inner LinearLayout's for the bottom space height is set to wrap_content, so it should take the height of the maximum height in there. Which is 52dp. And there is the ImageButton which is set to wrap_content, so the button should be centered vertically on the right side, since its an 24dp image (in xhdpi it has 32x32 pixels). So why is it stretched over the full height like displayed here?:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<de.ph1b.audiobook.utils.SquareImageView
android:id="#+id/cover"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:layout_gravity="bottom">
<TextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="52dp"
android:paddingLeft="16dp"
android:maxLines="2"
android:ellipsize="end"
android:paddingRight="16dp"
android:gravity="center_vertical"
android:textSize="16sp" />
<ImageButton
android:id="#+id/editBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_more_vert_grey600_24dp" />
</LinearLayout>
</LinearLayout>
either #drawable/ic_more_vert_grey600_24dp is larger than you think or borderlessButtonStyle has a larger background.
Please check the size. I have replicated the behavior and the ImageButton is set to wrap_content and is centered.
change your image button code
<ImageButton
android:id="#+id/editBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:scaleType="fitXY"
android:gravity="center_vertical"
android:src="#drawable/ic_more_vert_grey600_24dp" />

Categories

Resources