How to Make Grid View Scroll Horizontally Not Vertically in Android

How to make grid view scroll horizontally not vertically in android?

You can try putting the GridView in a HorizontalScrollView. You may try to set fixed height to the GridView inside the HorizontalScrollView.

Then you can dynamically calculate the number of columns of the GridView based on your content and use setNumColumns(int) method to set it.

Horizontal and vertical scrolling GridView Android

As per what i know you should never use a HorizontalScrollView with a ListView, since ListView takes care of its own scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by HorizontalScrollView.

http://developer.android.com/reference/android/widget/HorizontalScrollView.html

Since you may be forced to use a two dimensional scrollview, you may consider using this: Internet archive of blog.gorges.us/2010/06/android-two-dimensional-scrollview/

I haven't used this but it may be a reasonable approach.

Maybe try By adding this in your XML file and in your JAVA

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ScrollView
android:id="@+id/scrollVertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

//WateverViewYouWant

</ScrollView>
</HorizontalScrollView>

And the code onCreate/onCreateView

final HorizontalScrollView hScroll = (HorizontalScrollView) value.findViewById(R.id.scrollHorizontal);
final ScrollView vScroll = (ScrollView) value.findViewById(R.id.scrollVertical);
vScroll.setOnTouchListener(new View.OnTouchListener() { //inner scroll listener
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
hScroll.setOnTouchListener(new View.OnTouchListener() { //outer scroll listener
private float mx, my, curX, curY;
private boolean started = false;

@Override
public boolean onTouch(View v, MotionEvent event) {
curX = event.getX();
curY = event.getY();
int dx = (int) (mx - curX);
int dy = (int) (my - curY);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (started) {
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
} else {
started = true;
}
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
started = false;
break;
}
return true;
}
});

Android GridView with Both Horizontal and Vertical Scrolbars at the same time

I changed some code and finally able get tabular view.

Solution: I added HorizontalScrollView and now my LinearLayout is its child. Also I added android:stretchColumns="*" in TableLayout. Along with this i had to chnage android:layout_width and android:layout_height of different controls in following ways.

Updated code is below:

Main Activity Layout:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<!-- This Table Layout is header followed by the gridview -->
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:stretchColumns="*" >

<TableRow android:layout_width="wrap_content" >

<TextView
android:id="@+id/schemeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Scheme"
android:textStyle="bold" />

<TextView
android:id="@+id/nameTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />

<TextView
android:id="@+id/productTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Product"
android:textStyle="bold" />

<TextView
android:id="@+id/channelTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Channel"
android:textStyle="bold" />

<TextView
android:id="@+id/typeTitle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Type"
android:textStyle="bold" />

<TextView
android:id="@+id/customerSelectionTitle"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Customer Selection"
android:textStyle="bold" />

<TextView
android:id="@+id/brandTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Brand"
android:textStyle="bold" />

<TextView
android:id="@+id/wsTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="W/S"
android:textStyle="bold" />

<TextView
android:id="@+id/startDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Start Date"
android:textStyle="bold" />

<TextView
android:id="@+id/endDateTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="End Date"
android:textStyle="bold" />

<TextView
android:id="@+id/codeTitle"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Code"
android:textStyle="bold" />

<TextView
android:id="@+id/tsidTitle"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="TSID"
android:textStyle="bold" />
</TableRow>
</TableLayout>

<GridView
android:id="@+id/schemeGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:numColumns="1" >
</GridView>
</LinearLayout>

</HorizontalScrollView>

Grid Layout:

Only following tag is chnaged:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*">

Source Code:

No change in Java code.

I got idea from: How can I make my layout scroll both horizontally and vertically? and How to put a very long table layout inside the horizontal Scroll view in the CORRECT way?

Horizontal scrolling grid view

You can

  • use a TableLayout inside a HorizontalScrollView, or
  • stay with your approach with an horizontal LinearLayout but adding vertical LinearLayouts instead of directly the images. E.g., adding three to four images per vertical LinearLayout in portrait, and redrawing to add only two in landscape.

I would try the TableLayout approach first.

PS1: for next time, try to remove all the non-relevant code (the less code is there, the easier is to understand what you did).

PS2: Remember that System.out is usually redirected to /dev/null and thus lost, so I strongly suggest you to use Log.d instead.

Complete example

Adapt this to the onCreate() method or wherever you need it:

public void horizontalScrollGalleryLayout () {
HorizontalScrollView sv = new HorizontalScrollView(this);
LinearLayout llh = new LinearLayout(this);
llh.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(40, 40);
LinearLayout.LayoutParams layoutParamsLL = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i=0; i<20; i++) {
LinearLayout llv = new LinearLayout(this);
llv.setOrientation(LinearLayout.VERTICAL);
TestView testView1 = new TestView(this, Color.rgb(i*12, 0, 0));
TestView testView2 = new TestView(this, true, Color.rgb(i*12, i*12, 0));
TestView testView3 = new TestView(this, true, Color.rgb(0, i*12, 0));
llv.addView(testView1, layoutParamsTV);
llv.addView(testView2, layoutParamsTV);
llv.addView(testView3, layoutParamsTV);
llh.addView(llv, layoutParamsLL);
}
sv.addView(llh, layoutParamsLL);
setContentView(sv);
}

I'm using a very simple View as an example:

public class TestView extends View {
Context context;
int color;

public TestView(Context context, int color) {
super(context);
this.context = context;
this.color = color;
}

@Override
public void onDraw (Canvas canvas) {
super.onDraw(canvas);
this.setBackgroundColor(Color.LTGRAY);
Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
canvas.drawCircle(20, 20, 20, paint);
}
}

How to make grid-view horizontally scrollable in android

I found Two-way GridView helpful on github.

It has some methods:

scrollDirectionPortrait (vertical | horizontal)

scrollDirectionLandscape (vertical | horizontal)

numRows()

etc

Android - make GridView smaller to fit vertically without scrolling

Ok, I found a pretty good workaraound which does exactly what I want. It's pretty simple:

since my GridView is quadratic, I managed to get the height of the LinearLayout and set the width of the GridView with that value. (I did not directly set the width on the GridView but on another LinearLayout I added to solve some import problems).

So here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">

<LinearLayout
android:id="@+id/layout_help_p2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">

<GridView
android:id="@+id/gridViewPlayer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:gravity="center"
android:layout_gravity="center"
android:stretchMode="columnWidth"
android:horizontalSpacing="3dp"
android:verticalSpacing="3dp"
android:rotation="180" >

</GridView>

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Chronometer
android:id="@+id/timer_p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="Chronometer"
android:gravity="right"
android:rotation="180" />

<Chronometer
android:id="@+id/timer_p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="right" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:id="@+id/layout_grid_p1">

<LinearLayout
android:id="@+id/layout_help_p1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">

<GridView
android:id="@+id/gridViewPlayer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:gravity="center"
android:layout_gravity="center"
android:stretchMode="columnWidth"
android:horizontalSpacing="3dp"
android:verticalSpacing="3dp" >

</GridView>

</LinearLayout>
</LinearLayout>

</LinearLayout>

And here's how I managed the described process:

final LinearLayout mLayoutP1 = (LinearLayout) findViewById(R.id.layout_grid_p1);final LinearLayout mLayoutHelpP1 = (LinearLayout) findViewById(R.id.layout_help_p1);final LinearLayout mLayoutHelpP2 = (LinearLayout) findViewById(R.id.layout_help_p2);  mLayoutP1.post(new Runnable(){
@Override public void run() { layoutHeightP1 = mLayoutP1.getHeight(); mLayoutHelpP1.setLayoutParams(new LinearLayout.LayoutParams(layoutHeightP1, LinearLayout.LayoutParams.MATCH_PARENT)); mLayoutHelpP2.setLayoutParams(new LinearLayout.LayoutParams(layoutHeightP1, LinearLayout.LayoutParams.MATCH_PARENT)); } });

How can i get Vertical scrolling in GridView android?

Use android:numColumns="3" // this will force gridview to have 3 columns and if you have more than 3 items in grid view, you can have vertical scrolling.

Ex.:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="3"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

</GridView>


Related Topics



Leave a reply



Submit