I'm trying to space some buttons out across the display and have them be an equal distance apart.
Here's my view xml at the moment http://pastebin.com/DvURNWn3
For each button, change this:
android:layout_width="0dp"
and set parent element's width fill_parent or particular width as below :
android:layout_width="40dp"
It's good practice to set root element to RelativeLayout instead of LinearLayoutsince nested layout_weight may throw Nested weights are bad for performance warning.
Related
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"
I have a linear layout with horizontal orientation.
The left side of the layout is a linear layout with a vertical orientation.
The right side of the layout is a small view of fixed width 80dp * 80dp.
Problem:
If I set the left layout with width "match_parent" the right layout is not visible.
If I set the right layout with "width=0dp" and "weight=1" to get as much space as available it just wraps around the content. I mean that it does not go all the way to the next element on the right.
How can I make sure that the left element expands all the way to the parent width minus the 80 dp occupied by the right element?
Give this a try (background colors are for visualizing what is going on only):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#00ff00">
</LinearLayout>
<View
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#ff0000"/>
</LinearLayout>
The general approach for this is to leave the right element's width hardcoded to your 80dp and set the following on the left:
android:layout_width="0dp"
android:layout_weight="1"
This allows the right element to measure itself and reserve its 80dp and then the parent grants all the additional space to the left element since it's weight is 1.
Edit:
To explain a little further why match_parent pushes your right hand element offscreen -- LinearLayout will perform two layout passes when it has a child with a layout_weight. The first pass will allocate the minimum amount of space based on the specified widths/heights. It is during this pass that your match_parent element will behave as you're seeing and fill it's parent container, thus pushing the right element offscreen. Then, during the second layout pass, the parent LinearLayout will allocate any left-over space according to the layout_weight attributes. There is none left to allocate, so nothing changes.
If, however, you use a layout_width="0dp" on the left element, it will get no space during the first pass, and your right element will get the 80dp that it requested. During the second pass, the LinearLayout parent will allocate the remaining width according to the weights -- so in this case the left element will receive all of the unused space.
I believe the "match_parent" option will push out anything else that is with it in that layout. Try "fill_parent" on the left layout which leaves room for other things.
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 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.
Let's say I have a LinearLayout set to vertical and i've added 100 views to it each view is 50dp high. A user is going to scroll and fling up and down on that LinearLayout.
I need to know the index numbers of the views that are on the screen. i.e if they fling down to the middle and stop, and see 5 items on their screen, i'd need to infer 50-55.
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#id/listing_main">
</LinearLayout>
I've tried a bunch of ways to infer the current visible views - like taking the scrollY position and the height of my items...doesn't seem to work out.
view.getScrollY() seems totally arbitrary compared to the other scroll mesurements
If you put them inside a list then you can use getFirstVisiblePosition() and getLastVisiblePosition() and use these to get all of them from first to last.
http://developer.android.com/reference/android/widget/AdapterView.html