I am working on an app which should display several same size images on the screen. But it should only display only so much images as possible without offering scrolling.
E.g. On a "big" tablet it could display 10x10 Imageviews (screen is large, so there is much space for pictures)
On a "big" phone there might be enough space to display 6x6 ImageViews, so it should only display a 6x6 array of images.
On a small phone there is propably only space for 4x4 ImageViews, so it should only display this.
How can I make this in Android? I know about "layout-large", ... but if i make a special fixed xml-layout for a "large" device, it would not fit all devices correct. E.g. a Galaxy Nexus is a "normal" device and so is a Nexus One, but there would be at least be space for one or two more imageview rows on a Galaxy Nexus than on a Nexus One. So do I have to measure in code somehow how big the resolution is and display some TableRows accordingly? Or is there a special way how I can manage this?
you can query the properties of the screen of the device such as size, density etc. Once you know the size you can choose the layout to use.
Take a look at this stackoverflow post. Android: how to get screen dimensions
You should use a GridView to manage this.
GridView Android Developers
Edit: And indeed as Tomasz Gawel said: notice the GridView's android:numColumns="auto_fit" xml attribute
Edit: since you don't want it to be scrollable, you can just get the screensize, divide it by the size of your items, and put in only as much items as you want to display in the gridview.
The gridview will only scroll when it has more items than it can display.
getResources().getDisplayMetrics().widthPixels and heightPixels
Related
I wanna create a group of image views as below, and I can update background images. I wonder whether I should create 7 image-views on the layout or I should create each image view programmatically, like creating a list? Thanks.
I'd say it depends on the data. Will you always have 7 or could you have less, or maybe even more? Do you need to display 7 or can it depend on the screen width? Do they need to be a certain size or can they shrink to fit on narrower screens?
If it's guaranteed to be always exactly 7 and you're happy for them to scale to fit the screen then I'd use an XML layout but if it can vary then I'd render it programatically using a horizontal RecyclerView to show as many as possible on the screen and allow the user to scroll through them.
Let's imagine I want a picture of my dog as a background picture of a layout, and I want my application to be running on every smartphones.
I've read a lot of articles about this, and I just want to be certain that my dog will still look like a normal dog even if I use a tablet, a Nexus 5, or a moto G.
This subject is really similar to my problem:
Android: Background Image Size (in Pixel) which Support All Devices
According to the comments, it's not possible to have one image per resolution, because we have too many resolutions for android.
I just want an image of my dog as a portrait background.
Is it possible to do it? What is the easiest way to do it? What if my layout height is not exactly equal to the device screen height, but the device height minus another layout (fixed size) ?
I have a design that I need to follow in my app. In it there are several buttons, images and textviews placed all over the screen (some are aligned to the left, some to the right, some are centered etc). What I need to do is make all those elements appear EXACTLY as they are in the design image. The problem is, if I try to do this using dp, it can be wider or narrower than the design image specifies, and it depends on the properties of the screen. (I've had an app where I did it all in dp, and on my high-res phone it works perfectly, but on my friend's older phone it cuts away the edges of the activity)
My question is, what elements do I use for this? I've heard of using weight in Linear Layouts, but how to make items different width and height and position them all across the screen how I see fit? How to make sure it looks exactly the same on all screen sizes?
Thanks in advance! :)
Edit:
The point is, I need the buttons to be just as they are on the screen (this will be a listview element, I'm trying to make an xml for it). The distance, proportion, everything, it needs to scale to the width of the screen and be this size and distribution. And, I'm not just looking for a solution to THIS particular problem, I want to learn how to do it in general...
You don't. For very large and very small screens, you use separate layouts that scale the sizes, completely drop parts of the UI, or lays it out in a different manner. It's absolutely silly to think you can fit all the stuff on a 10 inch tablet and a 3.5 inch phone. If your designer expects that, tell him he's an idiot and he needs to get back to work.
For a more general answer on the best way of laying things out- it depends on the effect you're trying to get. You should never use pixel counts, and dp should only be used for small things- a bit of padding between 2 fields. Most things should be done via layout, either by using a linear layout and getting things in rows/columns, or a RelativeLayout and describing how to layout views relative to their siblings. But even with these tools you will not be able to fit on all screens and look good.
I am developing one application, right now i am on designing phase. i design one screen
on the 3.7WVGA(Nexus One) screen in eclipse using Linear Layout. but when i test it on 2.7
my some icon are go outside of the screen. my question is that which layout is suitable for all screen whether i design it in 3.7 inch or run it on 2.7.
Please give me a suggestion.
Thanks in Advance.
1st i design it in 3.7 and second in 2.7.
Don't ever, ever, ever design a screen for Android based on an actual screen size. You will always screw yourself up because there are a hundred different screens out there. What looks good on one phone will look like crap on another. That being said, here are some tips:
Use RelativeLayout to lay your button contents out. Once you understand the model it's much easier than you suspect and it will make it easy to automatically scale things.
Only use actual pixel sizes for things that "float". You never want to specify the width of something and try to fill the width of the screen.
Include multiple resolutions of your images. Let the system pick the right resolution for you.
A table/grid layout will make things easier for you on the overall design.
Big panels of buttons are played out. There are other UI options at your disposal (menu buttons, swiping left and right through screens, etc.). When users see a field of buttons it looks like the app was slapped together.
For that kind of layout use GridView if you want it scrollable, or a simple RelativeLayout if you want all the elements to scale depending on the size of the screen (use toRightOf, toLeftOf, above, below and weight to achieve that)
You need to consider the guide provided by android
Multiple Screen Support
What you will do is to provide all screens icon regarding different screens and you can also specify layouts for different screens, for example you want to provide drawables and layout for multiple screens, you will provide resources in that specific folder + below suffix.
Screens for layouts for drawables
ldpi layout-small drawable-ldpi
mdpi layout drawable-mdpi
hdpi layout-large drawable-hdpi
xhdpi layout-xlarge drawable-xhdpi
This topic will be more relavent to your need.
The following are the view groups in android. you can use any of these as per your requirement. But in your case You can use GridView
View Groups in android
FrameLayout Layout that acts as a view frame to display a single
object.
Gallery A horizontal scrolling display of images, from a bound
list.
GridView Displays a scrolling grid of m columns and n rows.
LinearLayout A layout that organizes its children into a single
horizontal or vertical row. It creates a scrollbar if the length of
the window exceeds the length of the screen.
ListView Displays a scrolling single column list.
RelativeLayout Enables you to specify the location of child objects
relative to each other (child A to the left of child B) or to the
parent (aligned to the top of the parent).
ScrollView A vertically scrolling column of elements.
Spinner Displays a single item at a time from a bound list, inside
a one-row textbox. Rather like a one-row listbox that can scroll either horizontally or vertically.
SurfaceView Provides direct access to a dedicated drawing surface.
It can hold child views layered on top of the surface, but is intended for applications that need to draw pixels, rather than using widgets.
TabHost Provides a tab selection list that monitors clicks and
enables theapplication to change the screen whenever a tab is clicked.
TableLayout A tabular layout with an arbitrary number of rows and
columns, each cell holding the widget of your choice. The rows
resize to fit the largest column. The cell borders are not visible.
ViewFlipper A list that displays one item at a time, inside a
one-row textbox. It can be set to swap items at timed intervals,
like a slide show.
ViewSwitcher Same as ViewFlipper.
I have the same problem but i found a very simple solution is use dp and sp instead px. You may read this:
What is the difference between "px", "dp", "dip" and "sp" on Android?
And you may create icons with different resolution and put it in suitble folder.
create a table layout and every row contains a vertical linear layout put all buttons inside and provides weight to each button according to need,at the last put this table layout to ScrollView that is suitable for all android devices
I created a tool that allows you to scale/adjust your layouts for tablets and small screen devices and made a blog post about it here: http://onemanmobile.blogspot.com/2012/04/how-to-scale-your-android-layouts-to.html
Basically, defining your layouts in dp units for one size is not enough if you want your app to fit on all devices and tablets, since there's four different "density-buckets". This tool will allow your layouts to be converted into fitting these density buckets.
It also explains in further detail how to make more flexible layouts for all resolutions.
I'm having a trouble here on hot to make my background image fit on a high resolution screen. It works just fine in a 4" screen but image won't stretch out when I deployed it on a larger screen. I tried to search for an answer but nothing helps as of now. well this might be a problem with my xml code but I'm not really sure. My design works with 1 table layout and 2 table rows for the 2 objects. And on the LinearLayout I set my background image.
For further understanding I made a sample on how it looks like when deployed.
There you go. It works fine on my phone while It doesn't occupy the whole space in Galaxy tab. Also as much as possible I would like to make my background image in smaller size around 320x480 pixel is size and just stretch it out.
Set your layout height and width to FILL_PARENT?
Its clear that you did not set Layout_height and Layout_width to fill_parent .
also for better rending put images in all three drawable folders drawable-mdpi, drawable-ldpi,drawable-hdpi .
also make your background image as 9patch will be nice for resizing a backgrond .
Well there are so many reasons I could point out for this problem of yours.
1)First thing the first screen shows a mobile screen and the next one shows a tab avd.
2)The image what u have should be in size to fit a mobile of particular resolutions only.
So your only hope is to get a image with resolution that fits the tab.
Or to change the wrap content to fill parent for your width and height.