I am trying to achieve something that on the face of it I thought would be easy but have researched loads and still struggling.
I am setting an ImageView that will act as a background where parts of the image are areas that buttons should sit on top. For example the background looks a bit like this:
This is set to fitCenter so that it scales with the correct aspect ratio on all screen types like this:
Now I am trying to place ImageButtons over the white areas like so:
I am trying this by aligning each Imagebutton to the top left of the background imageView and then setting margins to aset it in the right place. This works for some screen types, but when the background gets resized because the device is smaller than the background image for example I am finding that the buttons on top dont resize with it so I get the effect like the following:
Here is the type of layout I am trying (with just one button here):
<RelativeLayout android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/outer" xmlns:tools="http://schemas.android.com/tools">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/imageView1" android:src="#drawable/no_pills_pane" android:scaleType="fitCenter" android:adjustViewBounds="true" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/imageButton1" android:src="#drawable/pill_pink" android:scaleType="fitXY" android:background="#null" android:layout_marginTop="50dp" android:layout_marginLeft="44dp" android:layout_alignTop="#id/imageView1" android:layout_alignLeft="#id/imageView1"/>
</RelativeLayout>
I'm sure there must be an easier way to go about this, any help would be greatly appreciated.
Thanks
Simon
If all you need is to center ImageButton within ImageView, simply set alignBottom, alignLeft, alignRight, alignTop to ImageView
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignRight="#+id/imageView1"
android:layout_alignTop="#+id/imageView1"
Related
I am using ImageButton in my custom bottom navigation. And I have set icons of 32*32 size as src to them.
here is xml
<ImageButton
android:id="#+id/bottom_nav_profile"
android:layout_width="match_parent"
android:layout_height="50sp"
android:layout_weight="1"
android:src="#drawable/btm_nav_user_act"
android:text="Profile"
android:adjustViewBounds="true"
android:scaleType="center"
android:background="#drawable/bottom_nav_bg"
android:textColor="#000"/>
When I view it on my phone they look pixelated. I'm fine them being smaller but I dont want them to be pixelated. How can I make adujustment so that they look sharper.
Here are the icons
It seems that your image is very small to begin with, you have 2 options:
Remove the hardcoded height (50dp), your original image may be smaller than this, forcing its height may be reducing its quality on certain devices.
Have the same drawable you're using but for different phone densities. I'm guessing you only have one version of your drawable image in the drawable folder, on certain phones with certain densities, this image may display badly (just as it may be displayed just fine on certain other phones).
Lets make custom Image Button with linear layout and imageview
<LinearLayout
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/btm_nav_user_act"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#000"/>
</LinearLayout>
Now use linear layout as a button.
How can I get an ImageButton that has a fixed height and is only as wide as it needs according to the ratio of its source image?
I tried the following:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="8pt"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/list_download"
android:background="#android:color/black"
android:padding="0pt"/>
I set background to black just to see the boundaries of it exactly, and here is the outcome.
I have seen some solutions that suggest to use negative paddings but it is not an elegant solution. There should be a better one.
I tried similar configurations on an ImageView too, but it also had extra padding. So it's not an ImageButton specific problem (i.e. it is not related to this nine-patch issue).
EDIT: If I change scaleType from fitCenter to fitStart, then the outcome is like this. So there is somehow a minimum width.
It's because you've set a limited height. That limits the image height, but when Android calculates the width of the content (the original unrezised image), it's wider.
You'll get the image without the black "padding" on the side if you use wrap_content for the height as well.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0pt"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/list_download"
android:background="#android:color/black"
android:padding="0pt"/>
So, if you want the picture to be a specific size, you could resize it in your drawable folder to the size you want.
you just need use android:scaleType="fitXY" and set padding 0dp.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:padding="0dp"/>
you can put it in a Frame like so :
<FrameLayout
android:layout_width="wrap_content"
android:background="#android:color/black"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/list_download"
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="8dp"/>
</FrameLayout>
it will be much easier to play with it like so
I have an ImageButton that is the child to a RelativeLayout that uses a mipmap resource for it's source. However I'm having an issue with the mipmap stretching to fill the ImageButton.
In all of the threads I've read people have recommended "android:scaleType="fitXY"" yet this does not seem to work for me. The XML is listed below.
<RelativeLayout
android:layout_width="330dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp">
<EditText
android:id="#+id/search_edit_text"
android:layout_width="280dp"
android:layout_height="50dp"
android:hint="#string/search_hint"
android:background="#EAEAEA"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_margin="0dp"
android:textColorHint="#C9C9C9"/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toRightOf="#id/search_edit_text"
android:layout_margin="0dp"
android:scaleType="fitXY"
android:padding="0dp"
android:stateListAnimator="#null"
android:src="#mipmap/search_button_icon"
android:background="#null"/>
</RelativeLayout>
The image below is the result of the above XML, however the search button (to the right of the edittext element) should be the same height, and be attached to the edittext.
Any help is appreciated.
I just copy/pasted your code, and the image below is what I get (I used default android:src="#mipmap/ic_launcher")
And I get a visual similar to yours.
Where it looks like the image isn't "stretching"
However if you click on the image you see that image's boarder perfect matches the EditText to its Left.
So my guess is that your input Image 'search_button_icon' Has built in padding in the Image itself and would need to be cropped if you want it to be 'exact' visually as the EditText.
I have an activity that looks like this:
When I run the app on the emulator, the pushpin is not over the post-it note, so I know there will be problems with different sized screens. The post-it note and cork board are the background. How could I go about making it so that, on any sized screen, the pushpin will be at the correct location? It's currently a linear layout.
Best solution for you is use a RelativeLayout. I advise that your post-it note not be a part of the background but an own ImageView so that you may re-size it any time to fit any screens. And due to that you can then add this to your code for your cork.
<ImageView
android:id="#+id/cork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignTop="#+id/postit_note"
/>
It will center the cork in the center and align it to the top of the post-it note.
You may view all the other attributes to help you with RelativeLayouts Here
Based on the above recommendation, do something like this:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:src="#drawable/postit"/>
<ImageButton
android:id="#+id/angry_btn"
android:background="#drawable/pushpin"
android:layout_width="80dp"
android:layout_height="80dp"
android:shadowDx="0"
android:shadowDy="0"
android:layout_gravity="center"
android:shadowRadius="5"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
Note: You should make the brown part as the background for the outermost layout, and have a separate image for the post-it note, which you should set to the image view I have shown above.
So I'm working on making a design for my app and I need to put a imagebutton directly below an imageview. But since my imageview has a border around it with a drop shadow I need to hide (shift up) maybe 10 pixels of my imagebutton behind my imageview. Here is a quick drawing of what I want.
I hope that makes sense. I've been messing around with all kinds of different arrangements but I cant get what I want. Any help would be greatly appreciated.
First of all use a RelativeLayout. Add first the ImageButton and then the ImageView, in this way the ImageView will be on top of your ImageButton. Then you should set on your ImageButton the following :
<ImageButton
....
android:layout_below="#id/ImageViewId"
android:layout_marginTop="-10px" />
Use relative layout as said. Refer
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
for more positions and easy design.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Imageview"
android:layout_marginTop="-16dp" />
<ImageView
android:id="#+id/Imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:adjustViewBounds="true"
android:contentDescription="#string/description_logo"
android:src="#drawable/imagename" />
</RelativeLayout>