Scaling ImageViews For higher res phones - android

I have a few ImageViews located near the bottom of my application. When I run in WVGA (Nexus One), everything lines up nicely with the bottom.
When running on another higher res, such as FWVGA, as well as on my Droid 2, there is a space that is left on the bottom of the application.
Is there a way to scale imageviews so that they stretch to fill all the space needed in one or multiple directions?
At the moment I am using an AbsoluteView and my imageview code looks like. I have tried layout_x and layout_y.
<ImageView android:layout_height="400px"
android:id="#+id/widget33"
android:layout_width="300px"
android:background="#drawable/picture1"
android:layout_x="-45dp"
android:layout_y="216dp">
</ImageView>
I have also tried a few other things such as
android:baselineAlignBottom
android:padding

You had fixed the height and width of the imageview.
If you want to set imageview in your whole screen you can use fill_parent or match_parent if above Android2.2 .
And also I am giving you advice to use relative layout instead of Absolute layout.
In relative layout you can set Views in relative position so that your view will remains same even if the resolution of the device will increase.
And also use unit of length in dp instead of px so that your view will remains same in all screen density.

Related

How to set the height of the main view which will be sum of the height of other Views?

I am designing a scale where I have 100 boxes and their vertical space. Here is the code for that. The below code indicates one Block. Ther will be 100 blocks. in that scale. The height of this block from Imageview and view is 6.7dp. 6.7*100= 670dp.
<ImageView
android:id="#+id/img_2"
android:layout_width="6dp"
android:layout_height="6.5dp"
android:background="#color/boxcolor" />
<View
android:layout_width="6dp"
android:layout_height="0.2dp"
android:background="#android:color/transparent" />
But when I set the height of a graph-view 670 dp it does not match the height of the scale.
<charts.LineChart
android:id="#+id/graph"
android:layout_width="0dp"
android:layout_height="670dp"
android:layout_marginTop="11.7dp"
android:layout_marginStart="2dp"
android:background="#android:color/transparent"
Here are the pictures that explain what it looks like.
first_image,
I cannot directly the height of the scale because the boxes are in the inner views. so can't directly set the height that way.
Please check both pictures.
Here is the whole code file
XML file link
You can't count on math being consistent like that over dozens of objects of different size- eventually rounding will kill you. Devices aren't always exactly able to fill 1 dp, there might not be an even number of pixels in that size. For example, 330 dpi is a common screen size, that would have 2.0625 pixels per dp. That would likely round down to 2 for small numbers. Add a few of those together and now you're off by a few pixels of cumulative error. Where as the 600dp would still only be off by .0625 pixels, because it would calculate off the total.
If you want a view to go around other views, there's 2 ways to do it. One is to make it the parent. If so, put the other views inside it and make this view's height wrap_content. The other is to use a ConstraintLayout or RelativeLayout, make the height wrap_content, but constrain the top and bottom edges to the top of the first image and bottom of the last.

TextView above ImageView: Space between the top of the scaled SVG and the bottom of the TextView is scaled

In my application, a TextView is shown above an ImageView which contains a SVG image. Depending on the screen resolution, the ImageView, which is constrained (top and bottom e.g.) by another ImageView, is scaled: thus its SVG is also scaled by decreasing its height (and width, proportionnaly). Thus, the space between the TextView and the ImageView is technically the same than the one in Android Studio but is visually longer.
What I would want is: to keep the same space in Android Studio than on all screen resolutions, i.e.: 8dp.
Example:
In the Android Studio's visualizator, the space is 8dp:
In a Samsung Galaxy S7, I think the space is still 8dp but the SVG has been scaled: the space seems longer.
Proof that the SVG is scaled and that the ImageView is really at 8dp from the TextView:
Do you know how to keep the same space than in Android Studio, whatever the screen resolution?
Warning
I really want the SVG to be scaled (so I don't want, e.g. to set scale: fitXY or something like that). But I also want the space between the bottom of the TextView and the top of the scaled SVG be 8dp on all screen resolutions.
This is a guess but I think that your image is causing the problem.
As you said - your space is the same 8dp all the time but because your image has a white background, when you use a bigger image the white background scales and it looks like the space between your text and image is larger.
You can use some view with non-white background and scale it for testing - I believe that you will see the same spacing regards the view size. If this will be the case maybe try to change your image.
You can use LinearLayout layout with constraints to ensure a fixed proportion no matter what
E.g
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/main_layout"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="40"
/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="60"
/>
</LinearLayout>
So no matter what, in our example, the image will always be 40% of the horizontal space while the text will be 60%. I believe you can apply this idea to your situation

Android relative layout align center of background

I have a background image, which is simply colored panel such as silver purple, etc.,
This background image is set as andorid:background of RelativeLayout, which consist of a ImageView
I want that imageview's image resource to be appear like in center of the background image.
I tried alignParentTop and marginTop to position the combo image in center.
It works as expected, but when trying on different devices, as marginTop is hardcoded such as 10dp,, or 5dp, the alignment looks differently on different devices with different resolution.
I have even created various dimens.xml files for various configuration with various marginTop dp values. Still it only works on specific devices and doing fro all devices is obviously impossible. So i need to set or align the background and foreground images such that foreground image is centered over background image.
Note: Using Framelayout is not of my concern. I want this by RelativeLayout.
On the ImageView and the parent RelativeLayout, remove all of the padding and margin stuff you have, then add android:layout_centerInParent="true" to the ImageView. If you know the height and width of the image in the ImageViewbeforehand, set the ImageView to match it identically (in dp's). Otherwise, try setting the ImageView height and width to wrap_content and potentially android:adjustViewBounds="true".

How can I use this background image for all aspect ratio?

I have an image like this used as background in a RelativeLayout:
This image is used as background for all the levels of my game. Every level is drawn onto the blue area.
I want to keep fixed the aspect-ratio of the blue area, changing the size of the red edges to avoid to show to the user unused pixels of their screen. The green area must be fixed to 80dp for all phones. Then I must add a View (a GLSurfaceView) in my layout in such a way that it fit perfectly the blue area. Thus all levels of my Android game will be perfectly the same in all Android device.
How can I solve this problem?
The real image that I use is a little more complex. You can look it here:
Real image
I would use a FrameLayout for the middle part of the screen(blue), add an ImageView, containing the BackgroundImage you want to display, and put the GLSurfaceView on top of it.
Since the aspect ratio is always the same, you could set the ImageViews sclaing to fit xy and the image should always look the same.
Lets assume you are using a simple SurfaceView, the xml code id use to put a ImageView begind it would look like this
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
As i dont know how you build your View i cant post the code that does the job, but just add a FrameLayout instead of your GLSurfaceView to your View, with the Same Dimensions, the GLSurfaceView would have.
To that FrameLayout first add the ImageView, then the GLSurfaceView. Both with height and width set to match_parent.
To Figure out the size of your SurfaceView...
Retrieve Display Dimensions
Substract Green Bar Dimensions
Calculate the size of the Blue View, get the Height/Width (whatever is bigger) calculate the missing Dimension
Set the Red Views to Occupie the empty space.
So you would have to do this programmatically :)

how to create android UI so that it does not change with screen resolution?

I have an android app with a background image,which I would deploy on emulator and see its behavior.
Initially I was using emulator with skin WVGA800.In the UI all the textView declared in the layout were properly placed.
But when I change the emulator skin to 500x600 then in UI few of the textView are misplaced.
So could anyone please suggest how to handle this.
The good, but difficult thing about Android is that you don't know what size or aspect ratio the device your code runs on will be. If you want full-screen images, you need to be willing to have them scaled (proportionally or disproportionally) or cropped.
If you're trying to position text in a particular place on a background image, you're going to have a bad time. I've tried this before and quickly changed my mind.
Here's my suggestion: Read the screens support docs (as #abhilasha said) and use adaptive UI elements that scale nicely (like 9-patch images, relative layouts, etc).
Then if you want to place text in an image, instead of trying to place a TextView over an ImageView, set the image you want as the background of the TextView. Then you know where the text will be relative to the image.
My options:
1)for simple ui,i think u can use many attars with view,the following;
android:layout_centerHrizontal
android:layout_centerVertical
android:layout_centerInparent
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignWithParentIfMissing
android:layout_below
android:layout_above
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignTop
android:layout_alignLeft
android:layout_alignBottom
android:layout_alignRight
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
that is helpful to use layout or view attars to set its position.
2)u can use some layouts xml to fit it.like that
if(screen width < 480)
setcontentview(r.layout.for480lay)
else if(screen width > 480&&screen width <640)
setcontentview(r.layout.for640lay)
..
..

Categories

Resources