I have a problem with ImageView as below:
I have an image with big size (bigger than its part in screen), when I set src for imageView as:
<ImageView
android:id="#+id/splash_second_iv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="#drawable/phone_white"
android:layout_toLeftOf="#id/middle"
android:visibility="visible"
android:padding="0dp"
android:layout_marginBottom="0dp"/>
then I put it into a RelativeLayout, problem is the content of image is displayed as wrap in the ImageView but size of ImageView is very big (seems as it auto create padding in the image view in four sides of ImageView, since my calculation for layout fails (can not align property position of child)).
Anybody can tell me what's the problem in here and how do I resolve this problem?
Please have a look at the image to have a better understanding. Also I added attribute:
android:adjustViewBounds="true"
android:scaleType="fitEnd"
I can align the content as I want but size of ImageView still very big.
The xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:id="#+id/splash_slide_forth">
<TextView
android:id="#+id/splash_forth_tv_title"
android:paddingLeft="60dp"
android:paddingRight="60dp"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="#string/splash_4th_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#2b333b"
android:layout_marginTop="40dp"
android:textSize="28sp"
/>
<TextView
android:gravity="center_horizontal"
android:layout_below="#id/splash_forth_tv_title"
android:id="#+id/splash_forth_tv_desc"
android:paddingLeft="60dp"
android:paddingRight="60dp"
android:layout_centerHorizontal="true"
android:text="#string/splash_4th_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2b333b"
android:layout_marginTop="10dp"
android:textSize="15sp"
/>
<ImageView
android:layout_below="#id/splash_forth_tv_desc"
android:id="#+id/splash_forth_iv_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/phone_vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:scaleType="fitEnd"
android:adjustViewBounds="true"
android:visibility="gone"
/>
<ImageView
android:layout_below="#id/splash_forth_tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/splash_forth_iv_horizontal"
android:src="#drawable/horizontal_phone"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitEnd"
android:visibility="visible"
/>
Please set image as background,
<ImageView
android:id="#+id/splash_forth_iv_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_below="#id/splash_forth_tv_desc"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitEnd"
android:background="#drawable/demo_image"
android:visibility="visible" />
I really wish it works for you,otherwise we will do something else.
Related
I have an ImageView with a mail icon, however, it was too big so i reduced its size.
The actual size of the image is smaller, but the content it wraps is still the same, resulting in my lines not being even. The image below demonstrates my problem
As you can see, the size of the imageview does not wrap around the image itself.
Below is the XML for the imageview
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_mail"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:scaleX="0.5"
android:scaleY="0.5"/>
Is there a way to fix this?
Below this is the full XML for the whole layout if that would help
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/PaleGrey">
<Space
android:id="#+id/dummyTopSpaceSc"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:layout_alignParentTop="true"/>
<TextView
android:id="#+id/nameTextView"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dummyTopSpaceSc"
android:text="name"
android:textStyle="bold"/>
<TextView
android:id="#+id/titleTextView"
android:layout_width="wrap_content"
android:paddingLeft="10dp"
android:layout_height="wrap_content"
android:layout_below="#+id/nameTextView"
android:text="title"/>
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/titleTextView"
android:id="#+id/phoneContainer">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_phone"
android:scaleType="fitCenter"
android:scaleX="0.5"
android:scaleY="0.5"/>
<TextView
android:id="#+id/phoneTextView"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
app:layout_marginLeftPercent="15%"
android:layout_height="wrap_content"
android:autoLink="phone"
android:text="phone"/>
</android.support.percent.PercentRelativeLayout>
<!-- below this comment is the container for the mail icon -->
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mailContainer"
android:layout_below="#+id/phoneContainer">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_mail"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:scaleX="0.5"
android:scaleY="0.5"/>
<TextView
android:id="#+id/emailTextView"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
app:layout_marginLeftPercent="15%"
android:layout_height="wrap_content"
android:autoLink="email"
android:text="email"/>
</android.support.percent.PercentRelativeLayout>
<Space
android:id="#+id/dummyBottomSpaceSc"
android:layout_width="wrap_content"
android:layout_height="10dp"
android:layout_below="#id/mailContainer"/>
</RelativeLayout>
You should resize your image by giving it exact values for width and height instead of wrap_content if you actually want the bounding box to get smaller as you change the size. Then you can add padding/margins back to it as you wish. Also, you should use the android:src attribute (or maybe even app:srcCompat) instead of android:background.
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#mipmap/ic_mail" />
How to achive the below image
1. background yellow.
2. selection image on right top.
3. other image on middle
4. text may be above or below based on need
How to achieve this by button or textview or view
RelativeLayout will resolve this problem
Code:
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#000000" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="Text" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Snapshot::
{EDIT}
You cannot achieve this as a single view
Workaround
You can achieve this as single view using a background drawable
Make a background image having both the images in an another image
Set that final image as a background for a button
You can even use selectors for the button to give a corner background
Use RelativeLayout. The code will somehow look like this :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- First image -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<!-- Second image -->
<ImageView
android:id="#+id/mySecondImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/mySecondImage" />
</RelativeLayout
I am trying to give a border to an image view using a layout to wrap it and set the border as background to that particular layout. However the image view appears small and in the top left corner of the frame background.
Here is my code:
<RelativeLayout
android:id="#+id/Relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="40dp"
android:background="#drawable/pic_frame_big_01" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
How can I modify this code so that image fits into the relative layout and occupies it.
EDIT Changed the code of ImageView the image is centered now, but it appears as a very small image centered in the parent.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
Try adding: android:padding="10dp" to the RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="40dp"
android:background="#drawable/pic_frame_big_01"
android:padding="10dp">
<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/image1" />
</RelativeLayout>
android:src="#drawable/image1 is important too.. where you replace image1 with some image in your project.
Because you set it to align top left. Remove them and change width and height to match. Try this:
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
edit: Your relative layout is also aligned top left. Try to remove alignment attributes.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Relative1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:background="#drawable/border_red" >
<ImageView
android:src="#drawable/holotaglogo"
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
This is how I have my view at the moment.
As you can see, the icons are not all aligned all the way to the left. The red line is my objective. If they go all the way to the left, no problem, because I can solve it later with marginLeft.
This is my XML code (layout/listview_style_listview.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/BackGroundColor"
android:orientation="vertical"
android:scaleType="center" >
<LinearLayout
android:id="#+id/titleLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:background="#color/BackGroundColor"
android:orientation="horizontal" >
<TextView
android:id="#+id/sectionTitle"
style="#style/sectionTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:text="(blank)" />
<Button
android:id="#+id/sectionInfoButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/infoicon" />
<Button
android:id="#+id/sectionInfoButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/filtericon" />
<Button
android:id="#+id/sectionOptionsButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/menuicontop2" />
<Button
android:id="#+id/addCatButton"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_marginLeft="3dp"
android:layout_weight="0"
android:background="#drawable/effect_button_add_cat_click" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dip"
android:layout_marginRight="0dip"
android:background="#color/blueOceanStroke" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/BlueOcean"
android:dividerHeight="1dp"
android:listSelector="#drawable/listview_style_list_selector"
android:paddingBottom="1dp" />
</LinearLayout>
This is my XML code (layout/listview_style_row.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="35dp" >
<ImageView
android:id="#+id/catThumbnail"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/bank1" />
<TextView
android:id="#+id/sectionTitle"
style="#style/title"
android:layout_width="97dp"
android:layout_height="32dp"
android:layout_alignBottom="#+id/catThumbnail"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="66dp"
android:gravity="center_vertical"
android:text="Title Text"
android:textColor="#color/listsTextColor"
android:textSize="15sp"
android:textStyle="normal"
android:typeface="normal" />
<ImageView
android:layout_width="25dip"
android:layout_height="32dp"
android:layout_alignBottom="#+id/sectionTitle"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:src="#drawable/arrow" />
</RelativeLayout>
PS: I added an arrow icon to the right after the screenshot and that's why it's not showing on the image but I guess it's not important.
From the screenshot the layout is OK because the divider with back color #color/blueOceanStroke goes all the way and so do the separators. The problem must be in the "#+id/catThumbnail", if you measure the distance between your red line and the left edge of the yellow square and compare it with the distance between the right edge of the square and the left edge of letter "B" they are about the same which it shouldn't be.
My guess is that there is some king of padding in the #drawable/bank1. Try to remove it completely and see if the letter "B" left aligns with the title.
Hope this helps...
I was able to fix it with android:layout_marginLeft="-10dp" on the "ImageView #+id/catThumbnail" in listview_style_row.xml, but i really dont think this is a valid solution for a final perfectly designed app, yes? For me to put negative values in there, theres something wrong, right?
As someone else mentioned, I would try removing the ImageView and seeing if the TextView will be flush against the red line. If it is, the ImageView has some issues, if not it's something else. Also, the ImageView may not have the same height and width and that could cause issues with the scaling you're doing by setting to 32 dp, see: Unwanted padding around an ImageView.
It might be easier to wrap your ImageView in a FrameLayout. Something like this:
<FrameLayout>
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/catThumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/bank1" />
</FrameLayout>
Or if that doesn't work, try setting the width and height of the image view and/or the frame layout to wrap_content. Also if that image is a 9-patch, there may be other causes.
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>