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
Related
I'm new to Android and discover lots of things. But now I'm quite stucked.
I want to make a hexagonal board game, with tiles. I found many tutos and finally decided to draw as mush hexagons as I want to.
I created a 'Hex' class extending from View, add some variables and so on. Here is what I put in my main XML layout:
<flocoolb.app6.Hex
android:layout_width="wrap_content"
android:layout_height="wrap_content"
flocoolb:r="50"
android:id="#+id/h01"/>
<flocoolb.app6.Hex
android:layout_width="wrap_content"
android:layout_height="wrap_content"
flocoolb:r="50"
flocoolb:x="75"
flocoolb:y="43.30127"
android:id="#+id/h02"/>
<flocoolb.app6.Hex
android:layout_width="wrap_content"
android:layout_height="wrap_content"
flocoolb:r="50"
flocoolb:x="150"
android:id="#+id/h03"/>
r=size, x and y are offsets on canvas.
Here is what I get on my activity:
Main activity screen
But now I want to select single Hex. For example I want to select to top left one, or the bottom right one. I add OnClickListener on each of them, but actually the size of the view is the whole screen.
For example, if I change the background color of the view, it change that color on the whole screen. Is it possible to have the view limited to the drawn lines?
I hope you will understand what I mean.
Many thanks :)
Although,I don't understand you very much.But I suggest you to override onTouch Method of your view.And implement your function in onTouch method.
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'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.
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.