I have 5 images that I want to place in image buttons inside a horizontal LinearLayout. The images are saved in different sizes on the disk, but when placed in the layout, I want them to be of the same height, but in different weights of 0.5,1,1,1,0.5 (meaning the weight sum of the layout is 4). I tryed using this code and it didn't work:
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4" >
<ImageButton
android:id="#+id/scrollLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/left"
android:layout_weight="0.5" />
<ImageButton
android:id="#+id/dog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/dog"
android:layout_weight="1" />
<ImageButton
android:id="#+id/fish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fish"
android:layout_weight="1" />
<ImageButton
android:id="#+id/cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cat"
android:layout_weight="1" />
<ImageButton
android:id="#+id/scrollRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/right"
android:layout_weight="0.5" />
</LinearLayout>
Are there any parameters of layout_width and layout_height that I can change that will get this to work, or do I need to create a scaled bitmap from java code for each one of the buttons?
Thanks!
try defining the same height and then you scaling to keep the ratio
<ImageView
....
android:layout_height="match_parent"
android:scaleType="centerInside"
.... />
EDIT using a match_parentfor the height will make the ImageButtons have the same height and keep the scale ratio using scaleType
Related
I'm trying tot display 3 images:
- play button on the center
- two icons on the right
it's like: [___x__xx]
I added two black images on the left so that the playbutton while centered.
The problem now is that the two icons are not the original sizes, but fetched.
How can i solve this?
my code:
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/relativeLayoutText1"
android:layout_below="#id/relativeLayout3"
android:layout_height="wrap_content">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spacex"
android:layout_weight="1" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/spacex"
android:layout_weight="1" />
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/btnPlayPause"
android:background="#drawable/togglebutton_playpause"
android:layout_centerVertical="true"
android:textOff=" "
android:textOn=" "
android:layout_toLeftOf="#+id/netwerkImg"
android:layout_marginBottom="#dimen/abc_button_inset_vertical_material"
android:layout_weight="1" />
<ImageView
android:src="#android:drawable/ic_menu_gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/netwerkImg"
android:paddingRight="10px"
android:layout_centerVertical="true"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="center" />
<ImageView
android:src="#android:drawable/ic_menu_gallery"
android:id="#+id/exitImg"
android:scaleType="centerInside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_weight="1" />
If I understood the question correctly, you should be able to find the answer here: How to display an image full size with ImageView?
However, since there are many different device sizes, I would advise against that.
Fix the android:layout_width and android:layout_height to a value, when you set wrap_content be sure that your images will not appear in the same way using different devices, the design will be deformed.
for example :
android:layout_width="40dp"
android:layout_height="40dp"
Also to modify :
android:scaleType="centerInside" to android:scaleType="fitXY"
Also add :
android:adjustViewBounds="true"
Use scaletype attribute of imageview.
android:scaleType="centerCrop"
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.
I have 2 ImageButtons and a ToggleButton in a relative layout that fills it's parent. Here is the xml code:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/relative2">
<ImageButton
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/imageButton4"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:src="#drawable/ic_exposure_minus1"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp" />
<ImageButton
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/imageButton6"
android:src="#drawable/ic_cancel_black"
android:layout_alignTop="#+id/imageButton4"
android:layout_toEndOf="#+id/imageButton4"
android:layout_marginLeft="5dp" />
<ToggleButton
android:layout_width="80dp"
android:layout_height="80dp"
android:text="New ToggleButton"
android:id="#+id/toggleButton"
android:checked="false"
android:clickable="true"
android:textOn="+"
android:textOff="-"
android:textSize="18dp"
android:layout_below="#+id/imageButton4"
android:layout_toStartOf="#+id/imageButton6"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
When the device is in portrait mode there is enough height room for the buttons. When the device is rotated to landscape, the relative layout gets smaller and there is no room for all the buttons. The lack of room is in the height dimension. So the Togglebutton that lays at the bottom of the other buttons is scaled automatically to smaller height, so that it fits.
I would prefer that all the buttons' height would be changed equally instead of just the last one (the ToggleButton) getting smaller in height. I would then have 3 buttons that have the same height, instead of 2 keeping the initial height and one with smaller height.
Could I get this result by changing the xml file?
PS: To make it more clear:
To make 3 buttons equal height, change your parent layout from relative layout to linear layout. And enable layout weight attribute equally. So they will be in same size.
You should use LinearLayout if you want to make a uniform UI across different orientation.
Use Orientation property along with layout weights and you will get your desired result.
You just have to make nested Linearlayouts accordingly.
Here is your layout made using linear layout.
Hope you understand the significance of using linearlayouts.
<?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:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp" >
<ImageButton
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="0.5"
android:id="#+id/imageButton4"
android:src="#drawable/ic_exposure_minus1"
android:layout_marginLeft="5dp"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_weight="0.5"
android:id="#+id/imageButton6"
android:src="#drawable/ic_cancel_black"
android:layout_marginLeft="5dp" />
</LinearLayout>
<ToggleButton
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="New ToggleButton"
android:id="#+id/toggleButton"
android:checked="false"
android:clickable="true"
android:textOn="+"
android:textOff="-"
android:textSize="18dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
There are some rows in listView and each rows has the same format :
From left to Right
ImageView TextViews ImageButton.
i am using relative layout to fix the position of the components but the size of the ImageButton is scaled and only imageButton displayed on each rows.
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_resultItem_Layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imgBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<LinearLayout
android:id="#+id/bBasicDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignRight="#id/imgBook">
<TextView
android:id="#+id/bid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone" />
<TextView
android:id="#+id/bName"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="18sp" />
<TextView
android:id="#+id/authors"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:text="#string/authors" />
<TextView
android:id="#+id/publisher"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:text="#string/publisher" />
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:text="#string/status" />
</LinearLayout>
<ImageButton
android:id="#+id/btnMore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/temp"
android:layout_toRightOf="#id/bBasicDesc"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Any wrong in my xml coding?
Because you are doing wrap_content it takes the size of the image that you set as src. That is why its getting scaled and bigger..
To resolve this, instead of using wrap_content you should think about what is the right height that you want for the listView item, and then for both ImageView and ImageButton set something like:
android:layout_width="50dp" // sample value of 50dp
android:layout_height="50dp" // sample value of 50dp
android:adjustViewBounds="true"
android:scaleType="centerCrop" // or android:scaleType="centerInside"
You can set custom height and width of the image you are inputting into your layout. This way no matter what the image will stay that size and not try to stretch or shrink.
You can do this within your activity's .xml file using:
android:layout_width="yourwidthhere.dp"
android:layout_height="yourheighthere.dp"
Is there any way to do this? I have tried padding the image and setting the width/height of the view, but neither seems to work. Here is an example:
<ImageButton
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/search_small"
android:paddingTop="4sp"
android:paddingBottom="4sp"
android:paddingLeft="6sp"
android:paddingRight="6sp"
android:layout_marginRight="10sp"
android:layout_marginTop="6sp"
android:layout_marginBottom="6sp"
android:layout_alignParentRight="true"
/>
I want the button to be wider than it is tall, but it is coming out the other way round.
Just had a play to try and understand your problem.
Seems ImageButton is a composite view which has a few pre-set values. Such as some sort of margin which you cannot override with the XML. If you cannot change your image to match what you want to happen then you are better to create your own composite view.
Here is my example of a composite view you can make yourself:
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/saveSearchButton"
android:layout_width="50dp"
android:layout_height="50dp" />
<ImageView android:layout_width="45dp"
android:layout_height="45dp"
android:scaleType="fitXY"
android:src="#drawable/ic_menu_save"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/clearSearchButton"
android:layout_width="50dp"
android:layout_height="50dp" />
<ImageView android:layout_width="45dp"
android:layout_height="45dp"
android:scaleType="fitXY"
android:src="#drawable/ic_menu_close_clear_cancel"
android:layout_gravity="center"/>
</FrameLayout>
And the original buttons:
<ImageButton android:id="#+id/imageButton1"
android:src="#drawable/ic_menu_save"
android:layout_height="45dp" android:layout_width="45dp"/>
<ImageButton android:id="#+id/imageButton2"
android:src="#drawable/ic_menu_close_clear_cancel"
android:layout_height="45dp"
android:layout_width="45dp"/>
Here we can see custom image/button composite followed by the build in ImageButton as part of the SDK:
Set android:background instead of android:src to set the image on the button. This will adjust the image to your button's size. Then adjust the padding after.
You shouldn't use sp as a size dimension - dp should be used as it will help your view scale directly with different screen density and resolutions. See Here for dimensions.
padding will push other elements away from your view boundary. margin will push the contents of your view inward from the your boundary (ie would squash the available space for your picture) . The boundary is specified by height and width. Without more information I would guess you are being confused by your margins - delete them and experiment.
Also useful to you: android:scaleType="fitXY" makes the image stretch to match both the X and Y dimensions that are available to it. It helps you to see the canvas available to your image. Once you feel the area is large enough for a correctly scaled image change the scale type to centerInside. See Here for all scale types.
I use minWidth and minHeight attributes, combined with a fitXY scale type and wrapping its content to modulate the shape of my button.
<ImageButton
android:id="#+id/fooButton"
android:background="#drawable/play_button"
android:backgroundTint="#00000000"
android:minWidth="200"
android:minHeight="100"
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playStuff"
/>
Can you explain your question more widely so that we can more understood.
As per my understanding You want to set your ImageButton Height/Width. But it doesn't work is it? I want to ask you that, if you write any specific height/width then also it doesn't work?
I copied your code in my files and I changed the height/width manually then it will work.
Please explain your question.
Thanks.
I finished the layout following Graeme's answer. Four "imageButton" fix the bottom, same width, changeable image size. thanks!
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#color/#000"
android:weightSum="100" >
<FrameLayout
android:id="#+id/flBottom1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="25"
>
<Button
android:id="#+id/ibBottom1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/detail_tab_bg_selector"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:src="#drawable/icon_home_48_48"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/tvBottom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:text="#string/bailty_text_home"
style="#style/bailtyTextBottom"
/>
</FrameLayout>
<FrameLayout
android:id="#+id/flBottom2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="25"
>
<Button
android:id="#+id/ibBottom2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/detail_tab_bg_selector"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:src="#drawable/icon_compose_48_48"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="#+id/tvBottom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:text="#string/bailty_text_comment"
style="#style/bailtyTextBottom"
/>
</FrameLayout>
<FrameLayout
android:id="#+id/flBottom3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="25"
>
<Button
android:id="#+id/ibBottom3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/detail_tab_bg_selector"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:src="#drawable/icon_search_48_48"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="#+id/tvBottom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:text="#string/bailty_text_search"
style="#style/bailtyTextBottom"
/>
</FrameLayout>
<FrameLayout
android:id="#+id/flBottom4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="25"
>
<Button
android:id="#+id/ibBottom4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/detail_tab_bg_selector"/>
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:scaleType="fitXY"
android:src="#drawable/icon_barcode_48_48"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="#+id/tvBottom4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:text="#string/bailty_text_scan_again"
style="#style/bailtyTextBottom"
/>
</FrameLayout>
</LinearLayout>