How do I properly align ImageViews on top of one another? - android

I'm currently trying to make a simple tic-tac-toe game, consisting of ImageViews on top of the board (also an ImageView). However, when I run the app and click on a tile, the picture appears out of place, even though it seems I had lined up the ImageViews correctly.
What I did was I set each original tile to a blank square, and then when the user clicks on a tile, it would change to either "X" or "O," depending on whose turn it was.
I'm pretty sure the problem has to do with dpi scaling on different devices, but I'm not quite sure how to fix it. Any help would be greatly appreciated!
I've included an image of my problem below:

Instead of using an image to display the grid, I would suggest to use XML views.
Horizontal line:
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#android:color/black"/>
Vertical Line:
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#android:color/black"/>
And constraint them between the images.

Related

Set of programmatically added images not wrapping

I have a layout:
<RelativeLayout
android:id="#+id/wordToFindLayout"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:orientation="horizontal">
</RelativeLayout>
On which I am programmatically adding images (images of letters), and I would like these images to go to the next line when they reach the right part of the screen (kind of carriage return). I initially don't know how many images will be displayed.
At the moment I just don't see the images exceeding the screen width and I didn't find a way to programmatically know when they reach the right side of the screen.
What would be the best way to do that?
I suggest you to use the GridView with the property android:numColumns="X" where X is the number of images per row.
See http://developer.android.com/guide/topics/ui/layout/gridview.html
You can use this library to go to next line:https://github.com/blazsolar/FlowLayout

Extending margins on larger Screens

Is there a way in android to make the margins extend to fill screens? The problem is that layouts I build for Nexus devices look great, but then when previewing on a regular device without the bottom controls there is an ugly space between the rest of the layout and the bottom. I would like the margins between items to increase when there is available space.
Pictures are added below. Sorry for the ugly cutting of some of the fields, I'm unable to show them at this time to due a contract. Notice how "advacned search" is far from the bottom, I would like the vertical margins between all items to increase and make sure this doesnt happen.
How can I acheive this in a relative layout?
Try using LinearLayout and empty Views between each of the items.
just empty View:
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/transparent" />

is it possible to specify android:minHeight to be the same as android:layout_width

I have an xml layout file for a venue details page, which specifies a few linear layouts and then a map view. Each linear layout has 2 TextViews, one as a header and another as the detail. At the bottom of the page is a mapView which shows the location of the venue.
On a large screen/tablet the map has plenty of room and looks fine, on a smaller screen, e.g. on a phone the map is squeezed into this tiny space at the bottom of the screen.
I'm happy with main layout of the page except for the map at the bottom.
What I would like to do is to specify that the minimum height of the map is at least the same dp as the available width - i.e. make sure its square. If there is more space for the map to fill at the bottom of the screen - i.e. to make it a portrait aspect rectangle, then it should do so.
Edit : I don't want to reduce the already dynamic size of the LinearLayouts as the information they provide is actually what the user will want to see.
pseudo code below:
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:minHeight="android:layout_width"
/>
I can see that it would be possible to do this through Java code. I can also see that I would be able to set a specific size such as 320dp, but this then requires more effort to get the size correct for multiple screen sizes.
Is this possible in xml alone?
Having had time to think about the comments made by #confused_at_times, I decided that in fact the problem was in the layout of my activity and not in the unavailable option in the xml.
My chosen solution is to have the map as the main element of the page with the marker pin being clickable and showing all available detail upon click.
It is not possible in XML for most View types.
As you know it can be accomplished in code, but I have always thought this type of a feature would be very helpful.
However for an ImageView you can accomplish something like this using the adjustViewBounds property, for example if you want a banner image at the top of your screen, and you want it to size correctly.
This XML below shows how to use that property:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="#drawable/big_image" />
First impression is that this image will match_parent in both height & width i.e. fill the screen. The scaleType will allow it to change size if required.
The trick is using adjustViewBounds set to true.
The image is scaled, but the bottom bound of the view is brought up to wrap_content of the newly scaled image.
Bear in mind that this method will not stretch a smaller image, but will work when shrinking a larger image onto a smaller screen.

how to keep aspect ratio with different android devices

So I'm trying to put an image inside a scrollview and have the image stretch to fit different sized screens. It'll stretch horizontally, but vertically it always comes out messed up.
I've read so many pages of people with similar problems, but their solutions never work for me. I've tried every different combination possible using different scaleType,layout_width and layout_height, along with trying changing other things around. The closest I've gotten to getting it to work is shown in the code below, using scaleType="centerCrop". It's the only one that stretches the image vertically in ratio, but it cuts off a big chunk of the image from the top and bottom, it'll scroll up and down the image, but only the middle part shows.
The image is a 480x5500 jpeg, if that matters. Originally before I started messing with all that, the app worked just fine on my phone, but then later I realized the image was crunched when I tried it on a tablet. There's gotta be a way to do this in the xml right? I really don't want to have to do things with the image in the java code part. Also I'm hoping to be able to do this using just one image, I don't want to have use a different image for different screen sizes. Please help, thanks.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/paper" />
</ScrollView>
may be this help you,
make both height and width wrap_content of ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
and no need ScrollView here.
Try using custom ImageView like AspectRatioImageView mentioned here:
https://stackoverflow.com/a/4688335/944070
the next sample is more than what you ask for :
http://www.androidviews.net/2012/11/photoview/
https://github.com/chrisbanes/PhotoView/tree/master/sample
https://play.google.com/store/apps/details?id=uk.co.senab.p
hotoview.sample
it fits the image to the screen , plus it allows to zoom in/out using pinching gestures.

Android: ImageSwitcher inside Scrollview places image at bottom of the screen

So I created a simple little app that contains rather tall images inside an ImageSwitcher and due to the height of the images, I've wrapped the ImageSwitcher inside a ScrollView (so the entire image is eventually visible). The interesting thing is that the Image doesnt start at the top of the ScrollView but rather the bottom. The scrolling all works correctly, but I was just curious as to why the top of the image is almost at the bottom of the device screen.
Does anyone know what is causing this behavior?
My images are large drawables (800x600) that are obviously being scaled down in order to fit in the device window, but that still doesn't explain why there is such a large amount of empty space above the image.
I would prefer to just scroll the ImageSwitcher itself but that hasn't worked thus far.
Here is my layout:
<ScrollView android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher android:id="#+id/switcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"/>
</ScrollView>
My suspicion is that the images are being displayed due to the resolution of the original image. I set ImageView.setScaleType(ScaleType.FIT_START); and this appears to have fixed the problem.
Try to call ImageView.setAdjustViewBounds(true) in addition to setScaleType. If you still have problems with your ImageView (inside ImageSwitcher) taking more space than needed, try to peek at the ImageView with the hierarchyviewer in the SDK tools.

Categories

Resources