I need to make a grid whose width and height is the same as the screen. I guess I would somehow use
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
to do this but I need to scale the images inside so that each ImageView is 1/10th of the width of the screen (with 10 images for example). Similarly for the image heights.
I have tried
a = (ImageView) findViewById(R.id.imageID);
a.setMaxHeight(metrics.heightPixels/numberOfImages);
But as you can imagine, this is very tedious with 130 images (and not to mention, it doesn't work!) It wouldn't be a problem if I didn't need to cater for lots of screen sizes!
Any help is appreciated! (Bit of an Android noob (-: )
Related
In my app, I download images from my web server (each having a different resolution) but I would like to show these images on a fragment screen consistently.
Ideally, I would like the image to have width="match_parent" and height to take 1/3 of the screen EXACTLY. Furthermore, the image should be shown in a as it's part of a content layout with other controls that could possibly grow larger than the screen height (hence the need for scrolling).
I have tried putting the image and the rest of the content in a LinearLayout and then setting the weight to 1 (with max weight being 3), but since the image and the other contents are in a scrollview, it doesn't quite seem to work. the image is either too large or too small (depending on the orientation and the resolution of the image) and my 1/3rd settings don't seem to be respected.
Is there any way to do this other than fixing the height to a pre-determined (dp) value? I would like to avoid that unless there's no other choice.
Many thanks,
You could always set the height for each of the images programatically to 1/3 the height of the screen, like so
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
ImageView img = (ImageView) findViewById(R.id.image);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) img.getLayoutParams();
params.height = (int)height / 3;
img.setLayoutParams(params);
my current android project requires the width of button should be set dynamically depending on the resolution available on device
i want to arrange 9 button in a row but the size will depend on the resolution available
for the current scene we are using below code
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//int sheight = displaymetrics.heightPixels;
int swidth = displaymetrics.widthPixels;
int px=swidth/11;
bW.setWidth(px);
bE.setWidth(px);
bR.setWidth(px);
bT.setWidth(px);
bY.setWidth(px);
bU.setWidth(px);
bI.setWidth(px);
bO.setWidth(px);
bP.setWidth(px);
somehow the width of button is not changed in any manner it remains the same
If i consider you buttons to be images, that you have set using the Selector file, then you need to keep those files in drawable-hdpi, drawable-mdpi, and drawable-ldpi, and the images will accomodate itself according to the available resolution..
i think you should use android:layout_weight property.Its going to consume space depending on the width available.
set to all the buttons as android:layout_weight="1"
Try this url
Linear Layout and weight in Android
Buttons and other objects that uses a ninepatch image won't get smaller than the ninepatch itself.
Make sure you are using an appropriate ninepatch image for you buttons that will fit the sizes you need.
i came up with this solution
ViewGroup.LayoutParams params = button.getLayoutParams();
params.width=px;
button.setLayoutParams(params);
So I have been facing this problem for a while and still I cant find solution for it. Hopefully you guys can give me a hand in this. I have a cards game that a player can see his cards like any other cards game.
The problem is that a lot of people complained that cards are too small.When I run it on my devicr (galaxy s2) the cards size are good. By chance someone sent me a screenshot from his HTC and the cards are indeed smaller than What I have.
Please note that I have this 3 different density folders and my Images view are set at wrap content.
I Have 2 solutions in mind and I appreciate if you can help further
1) instead of wrap content, I user a predefined width for each screen size (not density).This will garaunter a specifc size (space it occupies)on large screens for example
2) Make the imageview size relative to the width of the screen.I dont know how to do it so any pointers are appreciated.
3) any suggestions?
please help a programmer stuck in this delemma
You can use DesplayMetrics to get the dimensions of the device at run time and use the width and height you get.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
ImageView iv = (ImageView)findViewById(R.id.yourImageViewId);
iv.setWidth(width);
iv.setHeight(height * 2/3);
or something like that. this way you will never get such issues.
Do you have images in different sizes for different densities? Which scale type are you using for ImageViews?
You can create different layouts for densities but I would start with experimenting with scale types - http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
i have extended View and override onDraw and onSizechanged so i can draw on the screen after i have the full height and width of the screen.But when i tried on my phone the imgs are slightly in different position than that i have defined.Also i have define actual values(in px) in my code and i think i can't use dip at least i don't know....
What can i do to make it seems the same on many screens,when i'm not using Layouts and i do it on code?
Make images feel same on different screens, you have to first identify height and width of device and according to it you should write some method that will set your image at place where you want on different screens.
To get height and width of screen you have to write this method at start of activity:
float ht,wt;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ht = displaymetrics.heightPixels;
wt = displaymetrics.widthPixels;
I've made a couple of apps for android, but none used graphics that much. I'm now trying to make an actual game. I have some bitmaps that are on my canvas. My problem is I'm not sure where to initialize their positions. and in OnTouch events I want to make sure the bitmaps don't stray outside the screen.
For example, initializing one bitmap:
canvas.drawBitmap(glider.getGraphic(), 30, 20, null);
20 and 30 were arbitrary. But I want to be able to write something like screen.getWidth() so I know exactly where the bitmap is relative to the border of the screen. I can't find this in the developers reference.
If you're just looking to get the screen dimensions, take a look at the DisplayMetrics class. You can get an instance and the measurements like so:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Keep in mind this is the absolute screen size. If you're doing a fullscreen game, this will work fine, but if not, you have to keep in mind the size of the notification bar, and possibly the title bar. It might be better in that instance to just get the height of the SurfaceView itself and use it for your calculations.