I want to implement next:
in each row of grid view some nomber of visible elements, to reach other elements we can use horizontal scroll. But:
Code 1:
<GridView
android:id="#+id/gvTimeTape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:numColumns="30"
android:paddingLeft="200dp"
android:scrollbars="horizontal" >
</GridView>
Result (One row, all elements fit the screen):
Code 2:
<GridView
android:id="#+id/gvTimeTape"
android:layout_width="5000dp"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:numColumns="30"
android:paddingLeft="200dp"
android:scrollbars="horizontal" >
</GridView>
Result (elements not fit on the sreen and horizontall scroll not appears)
Does anybody knows in what trick?
Try to put your GridView to HorizontalScrollView.
Related
Please help me in this code below :(
I am working on a grid view which should show 4 fixed number of rows in landscape mode. It should display data from an arraylist and should scroll horizontally. I used normal grid view and rotated it by 270. Then inside the grid cell I have used a single Text View which I had to rotate by 90 in order to show the text straight. I set the numColumns property to 4. So the expected is to show 4 rows with data from the arraylist. But I am not getting the grid cell displaying text properky in 4 rows. I am pasting the below xml code for gridview and the single customized xml containing the textview which is inside the gridcell. I tried changing all the properties of the grid view, scroll view and text view. But dont know whats wrong with this code.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_installed_app" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:orientation="vertical" >
<GridView
android:id="#+id/gridView"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:columnWidth="10dp"
android:gravity="center"
android:numColumns="4"
android:rotation="270" >
</GridView>
</LinearLayout>
</ScrollView>
Customized TextView inside gridview cell:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginRight="10dp"
android:id="#+id/textview_gridcell"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:rotation="90"
/>
Use the below code to make GridView. problem is if GridView has 4 child then it show first three in the First Row and fourth element in the second row. i want fourth element to fill the complete width of Second Row.
If GridView has 5 Child then then last two child to fill the complete width of second Row.
any suggestion?
<GridView
android:id="#+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</GridView>
Child View layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px"
android:src="#drawable/android_logo" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:layout_marginTop="5px"
android:textSize="15px" >
</TextView>
when you say to your gridView that you want a columnWidth="100dp" , it will take your device screen width size and divide it with 100dp ... then you declared numColumns="auto_fit" so it will fill fit them to screen size ...
eg: screen size = 440dp ... 440/100 = 4 ... 4 rows can be in one line ... image then you have 5 elements in gridview ... so it will just put first four into first line and the fifth to second line ... so maybe in your case the best solution can be to hardcore say
android:numColumns="2/3/4" (how many you want to, this number can be decided from screen width)
I have a GridView , which displays pictures of alphabets
what happens is , every time in the last row there are less no of alphabets.
and the last row is left aligned , which doesn't look good
so I want make the last row to be in centre,
any suggestion
My GridView code:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="70dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center"
android:stretchMode="spacingWidthUniform" >
</GridView>
I have the same problem, but I managed this using two gridviews, with first gridview displaying the rows except the last row, with the second gridview displaying the last row. This is the simplest way to achieve what I need.
I have not find a better solution. Looking forward to see a nice and simple way also.
you could try using invisible elements, it not the most efficient solution but is simple, you can put two elements, one at the start, one at the end of the row and make them invisible
For those who are still looking for a solution for this here's my take for this, create a TableLayout and, and for each TableRow set its weigth to android:weightSum="4" and dont forget to add android:gravity="center"
For complete code check this gist for reference.
You could center the grid row inside of a relative layout and then add android:layout_centerHorizontal="true" to the alphabet ImageView's. Assuming that they're ImageView's.
So your GridView would be wrapped in something like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_cotent"
android:orientation="horizontal" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="70dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="20dp"
android:gravity="center"
android:stretchMode="spacingWidthUniform" >
</GridView>
</RelativeLayout>
And the individual alphabet images would need android:layout_centerHorizontal="true" like this:
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
</ImageView>
I am doing a task which retrieves images from server and displays in GridView in the application. This Grid view is scrolling up and down. But i want to scroll this view left to right as the menu screen scrolls. Is it possible with grid view? Or Is there any better way to do this? please help me in doing this
Add this line in your gridview.
android:scrollbars="horizontal"
for ex:
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animationCache="true"
android:numColumns="3"
android:scrollbars="horizontal"
android:scrollingCache="false" >
</GridView>
I have a gridview that is inflated by multiple buttons. I used just a gridview and it worked fine. The grid was in correct order and was centered. Although, I wanted to add a button below the grid of buttons that was different and centered. To do this I decided to section off the gridview and then add the button (therefore it wouldn't be affected by the gridview). I tried the following code to seperate the views:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:id="#+id/tbl_gridcreator"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TableRow>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="150px"
android:numColumns="7"
android:stretchMode="spacingWidthUniform"
android:gravity="center">
<!--
android:horizontalSpacing="0px" -->
</GridView>
</TableRow>
<TableRow android:gravity="center">
<Button android:text="Submit"
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>
</LinearLayout>
The problem is that this code smushes the gridview to the left and oddly it flips it (Position 0 is ont he bottom right and the last button is on the top left pushed off screen). The following image shows what is the result:
Any idea why its pushed to the left and flipped? Or maybe how I could have just the Gridview with the button on the bottom?
I figured it out. Not sure why I was using a TableLayout to encapsulate the GridView and the Button but I fixed it by removing the TableLayout and just having the the GridView above the Button in a RelativeLayout instead of a LinearLayout. There still some bugs but the most of the issue has been solved.