Can this be done?
<ImageButton
android:id="#+id/test_button"
android:layout_width="70% of screen width"
android:layout_height="whatever is needed for the image to keep its original aspect ratio"
android:background="#drawable/image" />
You should be able to use something like this:
<ImageButton
android:id="#+id/test_button"
android:layout_width="200dp" // You can use any number here to use a specific size
// You can use dp for density independent pixels
// Or sp for screen independent pixels
android:layout_height="wrap_content" // This will set the height to a size just big
// enough to hold the image.
Related
I have an ImageView that is a part of ListItemView that is suppose to be displayed in the ListView. Without the Image, each item of listview takes less height. This is consistent across all the screens as they all are of same height. However, in one of the activities, I need to display an image in the listview items and this always increases the size of each item for that activity. I have the proper folder structure under res > drawable > family > family (hdpi), family (mdpi), family (xhdpi), family (xxhdpi), fmaily (xxxhdpi)
So each image has 5 version based upon the screen density.
I have the below in my layout file:
<ImageView
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="right|center"/>
and I am setting the image in Java code:
ImageView imageView = listItemView.findViewById(R.id.image);
imageView.setImageResource(word.getImage());
However, no matter what resolution I choose, the image size always seems to be the same and it affects the size of the whole ListItemView in the ListView.
Size of ListView item without image:
With ImageView:
You may have noticed the change in the height of the ListView item with gray background. Is this normal. I dont want to fix the size of the image. I would like to scale it based upon screen resolution, however, it should not distort the overall height of the ListView item. This image remains the same no matter what the screen density is. why won't it scale automatically and fit the ListView item.
Is this normal or is there any workaround for this?
You are allowing the Image to decide how much space it can use by setting android:layout_height="wrap_content" and then having the width scale accordingly (in order for your layout_weight to work properly).
You could change your layout to force the ImageView to the height you prefer and let the use the scaleTypeto get the preferred scaling. In this example I used fitCenter but there are others that can be chosen (eg. fitXY).
By using the size qualifier dp the image will automatically "scale" with screen density as dp is Density-independent Pixels which means it will change with the screen density.
<ImageView
android:id="#+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:layout_weight="1"
android:layout_gravity="right|center"/>
I'm using the Rotten Tomatoes sample app from here: Rotten Tomatoes sample app.
But the movie poster images are not displaying full size on my phone. The layout width and height properties are both set to "wrap_content", but the images display shrunken. I have to set an actual pixel size for the width and height in order for them to display properly. I don't know why this is happening.
Does not work:
<ImageView
android:id="#+id/ivPosterImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/large_movie_poster" />
Does work:
<ImageView
android:id="#+id/ivPosterImage"
android:layout_width="121dp"
android:layout_height="179dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/large_movie_poster" />
You should take a look at What is the difference between "px", "dp", "dip" and "sp" on Android? to understand what the unit types mean and get your head around how android scales things.
You can then set the scaletype of your image view's to the various different options available and see what gives you the results you expect OR set the units to px (not recommended), bear in mind that if you use the images 'at their full size' in px the image will be scaled differently in relation to other elements on different screen sizes
ImageView doesn't resize on its own.
Once you downloaded the image, get its height and width and set the ImageView's Layout Parameters to those sizes.
In case the image's width is greater than the phone screen, set the width of the ImageView to the screen width and scale the height accordingly.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/gray"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/darkgray"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/attenders"
android:layout_width="110dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#color/gray"
android:layout_marginRight="8dp"
android:text="Attenders" />
<Button
android:id="#+id/send"
android:layout_width="110dp"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_gravity="center"
android:background="#color/gray"
android:text="Send IM" />
</LinearLayout>
</LinearLayout>
this is my code but the dp is not working fine for all screen resolutions.
suggestions plz, plz tell me if i am doing anything wrong
problem is that when i use dp for setting height or width of a button
it does not gets fits to all resolutions i-e on small screens it looks
big and on big screens it looks small, whereas i know that when we use
dp for setting height and width of any component it automatically
converts/adjusts according to screen resolution
What I understand from this is that you thought using dp instead of px (or in, or cm) will magically work such that they will all have the same physical size on all devices, regardless of that device's density (ppi).
That's not the case.
dp, or dip, as explained here, is
An abstract unit that is based on the physical density of the screen.
These units are relative to a 160 dpi (dots per inch) screen, on which
1dp is roughly equal to 1px.
A screen with more dpi (denser, meaning more pixels are packed into a square area of the screen), will essentially draw a physically smaller image compared to a screen that has 160dpi when tasked to draw the same, say, 100x100 dp image.
When running on a higher density screen, the number of pixels used to
draw 1dp is scaled up by a factor appropriate for the screen's dpi.
Solution
There are two easy ways to have your app look proportionally the same on different screen sizes.
The first is to use different layout folders (layout-ldpi, layout-mdpi, etc.). This technique is well-explained here. A much more recommended way would be to use different style values for each density, so you can still maintain one layout folder and refer to the styles instead for measurement. This can be done using the same technique, but instead you will have values-ldpi, values-mdpi, etc. This is useful for having standard sized UI elements across screen sizes.
The other way is to use weights all over your layout. Weights adjust automatically regardless of screen size of density. This will help a lot if you want, say, three columns that have varying width -- you can easily use weights to tell the layout that column A needs to occupy 40% of the available width of the screen, and B and C will have 30% each. This is useful for having a standard layout across screen sizes.
A clean-looking, nicely coded app will implement both.
It is beacause you are giving fixed dimensions which can only be fit for a particular screen size which you are using. So try avoiding static dimensions and make use of match_parent,wrap_content and fill_parent so that you can get your layout fit for every screen size.
I'm finding Android's choice of image resources for buttons based on screen-density to be unhelpful.
I'm developing an app for kids, and so I want it to have nice big obvious buttons, but at the same time I have to limit myself to the available screen real-estate.
Essentially I want to use image sizes appropriate to the screen-size that I have, not to it's density.
For example, lets say I want to fit 5 buttons down the side of the screen in landscape mode. I'd like to do something like this..
Get the height of the screen in PIXELS.
Calculate the height of each button (hButton), allowing for some space in between.
Load the appropriate image resource. I.e. the smallest one that is at least hButton pixels.
Scale the image to be hButton pixels high.
Something like that.
Does Android have a nice way of doing this, or do I have to code that myself? How do I access the appropriate images? (most of my buttons use two images - Up and Down)
Thanks
EDIT:
It seems I wasn't clear enough in my question. I don't need to know how to lay out buttons, and I have a good understanding of how Android chooses images based on density - THAT is the problem.
Consider these two devices -
1/ Galaxy I9000 phone - screen size 4" HDPI
2/ Samsung Tab 3 tablet - screen size 7" HDPI
Because both have the same screen density, both will show images at the same physical size. An icon that is 1" square on one will be 1" square on the other. THAT is the problem. I want the buttons to be proportional to the screen size.
I could try using size specific resources (e.g. drawable-small), but then there will be issues between small devices of different densities, which would then mean creating lots more directories, which seems like a bad idea.
I leaning towards putting a variety of sizes in the no-dpi directory, and then loading as needed.
you can ask you button how bit it is
int hButton = imageButton.getHeight()
then you can get the height of the button in the same way and so some simple school math:
getHeight()/buttonCount+(buttonCount*padding)
load the biggest image you have got and scale it down to your needs, since you don't know how big it is, unless you map it yourself. By that I mean that you can build something like a utils class that contains a map with the imageNames and their size in px
The layout XML in Android is very flexible and powerful. You can layout your buttons dynamically like below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Button A"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Button A"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Button A"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Button A"/>
</LinearLayout>
As for image resources, if you think the standard Android image resource selection is not helpful, I'm sure you are misunderstanding something. It is not about the size of buttons. It is about the density of images.
If you are thinking about using simple shapes as buttons, you may want to know about ShapeDrawables.
https://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html
https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
for get screen runtime height and width
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
for button height
int buttonHeight = hButton.getHeight():
then get the smallest button height
calculate image height and set it as image resource.
if u r asking for image resizing code
then chek here.
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
I have this ImageView
<ImageView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/offer_img" />
I'm loading images into this ImageView from a URL and I want the image to always fit X (width of the screen) and readjust the height to keep the aspect ratio. Do I need some Java to do this or can I do it through XML alone?
Try this:
<ImageView
android:scaleType="centerInside"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/offer_img" />
You will obtain what you expect only if original image ratio is around the 8:5 ratio
You could try it with android:scaleType
You could use the method that is the answer in this question: Java image resize, maintain aspect ratio