I have a problem with my ImageButton because it appears to be resizing together with the source image. This is quite problematic because I'm using a TableLayout with equally weighted TableRows to achieve a uniform grid layout but for some reason, that particular ImageButton is resizing itself with the image and making that entire row appear bigger than the others even if they all have the same layout weight.
empty src:
https://gyazo.com/b50ab6d7afb6608db0505d701a2a40c9
with src, adjustViewBounds=true
https://gyazo.com/e2a2dae581d1fae4503b6e130ced776d
with src, adjustViewBounds=false
https://gyazo.com/2623f08fcffea586d5b7917332ae7291
XML Tag:
<ImageButton
android:id="#+id/stat_analyze"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/sp2_calc_button"
android:onClick="btnClicked"
android:src="#drawable/statistics_white"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
Update:
So far, I've tried replacing the source image which is a png with a smaller version and with an android vector resource replacement but so far, I've still been stuck with the same results. I guess that means the only way to do this really is to lock the layout height. I've tried programatically setting the height at runtime but I still get the same result.
Update:
I tried once again, resizing the image to a fixed value. I noticed, however, that this particular button does not take up the whole area until it reaches a height quite far beyond the intended height. What I mean by this is for example, I set it to 60dp, the entire row will already begin to take up more space even if the button itself hasn't matched the parent height yet. I don't know why this is happening as this is the first time I've seen this happen. Can someone help me with this?
Have you tried the scaleType fitXY attribute:
android:scaleType="fitXY"
Related
I'm creating an app that uses an ImageView in the main activity that covers the whole width of the screen, the height should fit the image's resolution.
Here's my xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:adjustViewBounds="true"
android:id="#+id/bgImage"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bg_home_blue"/>
</RelativeLayout>
Here's what my layout looks like:
Everything seems to work fine but I don't know where these paddings are coming from, I've tried to change the scaleType but nothing seems to work.
Edit:
If I put instead of src just a blue background, the paddings go away and it fills the whole width (than I need to set a specific height as well)
I guess your image is png and have that space around it, even when your scaleType is FitXY it doesn't mean that it will remove the space around the png file, check that out and let us know what happened, or at least upload your image file
Image view elements stay the same regardless of device size. So while is optimized for a smaller screen like a Nexus 6, the orientation or spacing of the various buttons and image views are not appropriate on a tablet device like the Nexus 7.
Is this because the layout_width and layout_height attributes are defined specifically?
As for the aspect ratio, answers suggestions on certain questions have suggested that android:scaleType=fitXY be used have not yielded any effective results.
<ImageView
android:scaleType="fitXY"
android:id="#+id/ring"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:contentDescription="#null"
android:src="#drawable/ring"
android:layout_width="200dp"
android:layout_height="200dp" />
Do image view elements have to be both resized and scaled?
Somewhat similar questions:
Layout screwed up on bigger screen sizes.
How to evaluate view size to keep same proportion in all devices?
you should use percent for width and height of your image like below
android1:layout_width="0dip"
android1:layout_height="fill_parent"
android1:layout_weight="0.4885"
use layout-weight attribute for the elements.
Suppose inside a layout, there are 2 TextView elements.
Setting layout-weight="1" for the first and layout-weight="2" for the second will divide the total available space to the 2 TextView elements in the ratio of 1:2 regardless of the screen size. So, instead of using dp values for dimensions, use layout-weight attribute to indicate how much space you want your elements to cover.
I'm creating a bar at the top of my app much like the one above.
The graphics are embedded into the background and the numbers that represent the level etc is using TextViews.
As you can see the TextView has to be pixel(dp) perfect to fit in the center of the white bar thingy. With some margins or padding i can achieve that.
But how do games like that keep the same pixel perfect across all devices?
I know about resource qualifiers and such but some devices within the same bucket have different sizes.
Is there a good way or trick to achieve the perfect centering across most devices?
In general Android will handle the relative sizes across devices as long as you do everything in relative units and include 9-patch or images for each screensize.
For your question of how to handle images attached to a TextView, you can accomplish this a few ways.
A TextView will allow you to attach a drawable to it on the top, bottom, left, and right. You can do this with code using the various setCompoundDrawable* methods. For example, this will add a drawable to the right and left of the TextView.
txtScore.setCompoundDrawables(R.drawable.imageLeft, null, R.drawable.imageRight, null)
You can also do this in XML with the drawableLeft, etc properties. You may need to mess with padding and bounds for the drawables.
<TextView
android:id="#+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/imageLeft"
android:drawableRight="#drawable/imageRight"
android:drawablePadding="10dp"
android:singleLine="true"
android:text="125"/>
There are a number of different options in code as well if you look over those setCompoundDrawable type APIs for the TextView.
Your other option is to create your own View that does all of the work for you, but may be overkill if you only use it in one spot.
Again, you will really want to consider a 9-patch image or create an image for each screensize in the drawable folders. This will allow Android to determine which image to use for each device.
I am trying to make a small image appear on my screen. I want it to be a small square. With the below code, it shows up as a long flat rectangle. I have attached a picture of what it looks like below.
java code
ImageView q1Image = (ImageView)findViewById(R.id.q1Image);
q1Image.setScaleType(ScaleType.FIT_XY);
xml code
<TableRow
android:id="#+id/row4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/q1Image"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight=".1"
android:textSize="8sp"
android:gravity="center_horizontal" />
EDIT
If I make the width and height equal to 50dp each, the image gets bigger but is still a flat looking rectangle.
Your scaling the Image wrong. Here are all scaletypes:
Top row (l-r) center, centerCrop, centerInside.
Bottom row (l-r): fitCenter, fitStart, fitEnd, fitXY.
You probably need fitCenter:
q1Image.setScaleType(ScaleType.FIT_CENTER);
FIT_XY will stretch your image to fit all yout ImageView.
Use scale type fitCenter to preserve aspect ratio by placing the image in center if your ImageView
q1Image.setScaleType(ScaleType.FIT_CENTER);
Refer to http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
For other options.
Without the full layout XML this is just speculation, but seeing as you don't appear to have fixed the problem by changing the scale type, here are some other suggestions:
Wouldn't it be better if the layout_weight of the ImageView was zero?
Are you specifying android:stretchColumns on the TableLayout?
The image looks like it is being stretched to match the width of the text below. How many columns have you got in your table? It looks like the text in the other rows is in column 0 (as is the image in your first row) and the text in the first row is in column 1. If you want to leave column zero blank for the other rows, you need to specify android:layout_column on the text views.
I am creating a small Battleship game and for the Android UI I was hoping to create a 10x10 grid of image buttons or something (anything easy).
I was following the tutorial provided at this link
My problem is that I can't get the images to display in a proper 10x10 format. Using the GridView, I set the num_columns to "10" in the XML but there is no such attribute for the number of rows.
As phone sizes differ, is there a way to have a 10x10 grid auto fit any screen?
By following the steps in the above link I can display 100 clickable images but my problem is with the formatting. Here's a screenshot of said result:
I've tried changing values in XML to scale the images down but cannot figure out how to. Apologies for lack of code snippets but I really haven't altered much from the sample given in the above link other than the vertical and horizontal spacing.
Would appreciate any enlightenment, even if it's just to say I am I going about this the wrong way completely.
You can achieve this with a table layout : http://www.mkyong.com/android/android-tablelayout-example/
GridViews, just as ListViews have been designed to contain any number of rows.
But I really doubt you want to use views in your game and not only use a canvas, draw anything and detect clicks without relying on views built-in mechanisms. It's more work but you end with something that will make you much more free to do what you want (for instance not having to deal with the number of rows of a container...)
go to this link and try 2nd example Custom Adapter example.
just change android:numColumns="auto_fit" to android:numColumns="10".
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:numColumns="10"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
You can probably do what you want with a GridLayout, but not with a GridView (which automatically scrolls vertically). Using a GridLayout, set all the buttons to have 0dp layout width and height, and set their layout gravity to fill.