Android... positioning components accurately over background image - android

I want my app to run on a wide variety of android devices in portrait only. I have a whole-screen background image that includes borders for other components. I want to place items like listviews/sponsor ads accurately within those borders... overlay them on top of the background in exactly the right place. Is using Framelayout the best/easiest way to do this? How do I make sure I get the listviews or sponsor ads in exactly the right place, and exactly the right size given that the background will be stretched to different dimensions for different devices? I've considered cutting up the background image instead but that seems like more work. What's the best approach? Thanks for any thoughts!

Unfortunately, this scheme is never going to work consistently across all screen sizes. If you want borders, you need to create 9-patch graphics out of those and assigne them as teh background to the UI item.

Related

Titanium/ Alloy/ Appcelerator: Cover entire screen with image

I have a JPEG photo which I want to use to as a background for one of the screens.
I want the behaviour to be simlar to the css backgroundSize: cover property. (Play with the following tool to see what I mean: https://davidwalsh.name/demo/background-size.html)
For iPhone, this issue is fairly easy, as you can literally have several different versions of the same image depending which iPhone the app is being installed on.
For Android, this is much more difficult as they have various different aspect ratios and resolutions.
Therefore my question is, how can I specify an image that covers the entire screen.
You can always fake this by adding the imageview inside a View and calculate the proper height/width you need to cover the screen (get the shortest side and scale it up and multiply the other side by the original ratio).
You could also use https://github.com/AndreaVitale/imageview that supports a cover mode
For Android, My suggestion is to use 9-patch Image instead of normal image. It will stretch it self to entire Screen, if you placed image as background
of Window it self as well as worked for ImageView perfectly.
Thank you.

Controlling the positions of a views in Android

I am trying to create an android app where I have a single relatively big button in the middle (the light blue in the picture) and it is surrounded by other smaller buttons as shown in the picture (some of small buttons might be visible or invisible based on some criteria).
I started with the RelativLayout setting the big on in the center and making the rest placed in relation to it, but it is a miss and the central button get shifted and doesn't stay in the center. I tried placing them in FrameLayout and used margin to adjust their locations, that worked the best however, the spacing changes on different screen resolutions.
So what is the best way to achieve such layout that will look consistent on any device?
Android's Percent Support Library allows you to use proportions to lay out your views, which may allow you to get closer to your goal.
http://developer.android.com/tools/support-library/features.html#percent

Android: clip background image to screen boundaries

Looking around, I see many questions that are very similar but not exactly like mine. The questions I've seen either want to avoid clipping, or clip to some complex path. I just want a simple clip. Here goes.
I want to create an Android app which uses for the background of its various Activies a large photo that cannot be stretched or 9-patched (this is a firm customer requirement). Instead, I just want to provide an image large enough to accommodate the various screen sizes/densities, with excess simply clipped or not displayed - as if it just overflowed the screen.
The images are not patterned or otherwise regular or similar to each other. They are photos indeed, with subjects and ideas presented, so I will of course have to decide which parts are displayed, but I'm not concerned with that right now - just how to approach the problem.
Are there layout XML parameters I can use? Or do I have to cook up a custom layout?
There's probably a better way, but the immediate thing that came to mind was just to set the image in an image view with a scale type set to not scale, and then to put all your UI elements on top of it with transparent backgrounds. Like this:
<RelativeLayout>
<ImageView android:width=fill_parent
android height=fill_parent
android:scaleType=center />
//your UI here
</RelativeLayout>

Android - How would i go about using RelativeLayout with background image scale right on different devices

As you can see in the photo I have a scaling problem with my design. Actually there is two problems. One involving a textview and one about scaling. I found out that the easiest solution for layout was to implement a background image to my RelativeLayout. (This may be a stupid approach i don't know.)
As you can see in the images, the text changes position on textview.setText("blabla"). And is creating visual problem. How would I go about fixing this?
Secondly, how would I solve the scaling issue on different devices? My initial plan was to make one background for each size. Like mdpi, hdpi and so on.. But how do I tell the app to choose between background? And is this even possible?
Am I doing it all wrong? :)
Positioning problem http://chk.no/bue/stackoverflow.png

Which Android layout and view to use for simple image-based game?

I would like to make a simple Android game where a large background image is displayed and some other images are displayed in specific locations over it, where the other images may be clickable.
Here's a quick sample image of what I'm talking about:
The user should be able to tap the soccer player or the moose (ah, the classic "soccer player moose problem"!)
How should I render this screen (which layouts and views?) so the user can interact with it and it will scale properly on different devices?
I would use a RelativeLayout.
You can set the you background image to the layout (fill_parent for height and width).
You can then put your ImageViews, containing your moose and soccer player down on the layout relative to the top or sides of the sceen, or relative to each other (making sure to specify "dp" units for everything). Set the backgrounds of your ImageViews to be transparent, and there won't be a bounding box problem (and/or you can also set your ImageViews alignment to be relative to each other, ensuring they don't overlap).
I think this is the simplest way to do this - it is then super easy to attach onClickListener to your ImageViews in your Activity, and you are done.
This type of layout will work the same on all devices and screen sizes.
There are some small gotcha's with RelativeLayouts, but they are pretty simple once you get into them, and provide fast rendering (since the view hierarchy is usually shallow). Good Luck.
ImageView for the clickable elements seems like a fine choice to me. For the background I would just set your image as the background of the parent layout i.e. RelativeLayout
SurfaceView for the whole thing (with your field as a background) and regular *ImageView*s for added elements. You can easily recover the click coordinates from the SurfaceView and thus know what element has been touched.
SurfaceView might offer you additional possibilities anyway.
For most images, I'd use an ImageView for each one, like FoamyGuy said.
If they're close enough for overlapping bounding boxes to be an issue, you can still use an ImageView for each, but with a variation of this answer, testing alpha for each ImageView in range.
I would agree with both FoamyGuy and Booger that if your only goal is to place static images onto the screen that do something when you click them, RelativeLayout and ImageViews all the way.
But...
If you are looking to randomly spawn multiple images onto the screen in intervals and have them move around for the player to interact with while explosions are going off and maidens are being kidnapped, you should look into SurfaceView, Canvas, Drawable, TouchEvents, and FrameBuffers.

Categories

Resources