this is my code ,
i want visiable horitonzal scrollbar in gridview , so i tried to fix it , my gridview have 1row and 3 Columns , icant use vertical scroll because content page have scroll
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="#+id/linearLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:id="#+id/linearLayout3"
android:background="#android:drawable/dialog_holo_light_frame">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="طرح های پیشنهادی"
android:id="#+id/textView3"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:layout_gravity="right"
android:textColor="#cc0066"
android:textStyle="bold" />
<GridView
android:id="#+id/gridview1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:verticalSpacing="5dp"
android:numColumns="auto_fit"
android:scrollbars="horizontal" />
</LinearLayout>
</HorizontalScrollView>
you can either enable nestedscrolling in xml or you can enclose your gridview inside a nestedScrollView . Try that , if that's not working let me know.
Did you try to add ScrollView?
HERE is that people already talked about that. If i didn't understand you wrong.
I adding example codes for vertical and horizontal.
How to scroll gridview to position?
How can I add a vertical scrollbar to a gridview?
Firstly, end of the LinearLayout, get rid of the slash: /> to >
Here is direct answer for your question:
Put into ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<!-- your codes here -->
</ScrollView>
For horizontal:
<HorizontalScrollView>
<ScrollView>
<!-- your codes here -->
</ScrollView>
</HorizontalScrollView>
Related
I have two gridview in one scroll view, however the only gridview to scroll is the pink one
[
and gridview 3 is fixed, but i want the two gridview to scroll together
how it looks like, i don't know if you can actually see... but when i scroll the page, only the pink gridview scrolls, and this vertical gridview doesn't
[
and then it messes the numbers...
my scrollview code:
<!-- Items -->
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fillViewport="true"
android:layout_alignParentStart="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Content itself -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/grid_content"
android:gravity="left"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:numColumns="6"
android:clickable="true"
android:background="#color/colorAccent"
>
</GridView>
</LinearLayout>
<!-- Numbers -->
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="right"
android:background="#color/white"
android:gravity="right"
android:padding="0dp"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/grid_numbers"
android:gravity="right"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="1"
android:clickable="true"
android:background="#drawable/border_vertical"
>
</GridView>
</LinearLayout>
</RelativeLayout>
</ScrollView>
I think that gridview already have a scrollbar. Therefore, it "overrides" your scrollview behavior, and then the gridview 3 does not scroll.
You should consider using only one gridView.
Edit :
If for some reasons you don't want to, I'd suggest to overwrite "onTouch" functions in order to prevent grid 2 and 3 to catch the touch event, and therefore, allowing the scrollview to deal with it.
However, this approach may lead to further problems if you need to catch other behaviors (click, move, ...)
I have a problem with Android's ScrollView. I am facing infinite vertical scrolling. Even though I've run out of elements, I can keep scrolling through white space. I want the ScrollView to scroll down to the end of relative Layout. If you have any solution for this it would be much appreciated.
Thanks in advance
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll1"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/info"
android:background="#color/main_backgroundColor" >
<LinearLayout
android:id="#+id/Line"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
<com.etsy.android.grid.StaggeredGridView
android:id="#+id/mainGridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:layout_below="#+id/Line"
android:verticalSpacing="1dp" />
<WebView
android:id="#+id/WebView_home"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible" />
</RelativeLayout>
</ScrollView>
Ofcourse I don't know the structure of your project but why you don't use ListView with CWAC MergeAdapter? It can help you with infinite scroll.
Here is a link: https://github.com/commonsguy/cwac-merge
I use a vertical ScrollView to carry ViewPager and Gridview. ViewPager's position is above the Gridview,and Gridview is use "expand all" to keep it behaviors well inside scrollview.
But. After I use adapter to fill content to these two view. The position is always scroll to Gridview's position at first.User need to scroll it up to see Viewpager themself.
I want its position can at the first viewpager, not the gridview.
Could you give me some suggestions.
Thanks.
The following is my layout file.
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<LinearLayout
android:id="#+id/linearlayout_dynamicRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/viewpager_fl"
android:layout_width="fill_parent"
android:layout_height="160dp" >
<myViewPager
android:id="#+id/vp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#B0000000"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_dot_dynamic"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="0dp" />
</LinearLayout>
<TextView
android:id="#+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:ellipsize="end"
android:gravity="center_vertical|center_horizontal"
android:maxLines="1"
android:textColor="#FF7700"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="GridView_Header1"
android:textColor="#ff7700"
android:textSize="20dp"
android:visibility="gone" />
<GridView
android:id="#+id/gv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</GridView>
</LinearLayout>
</LinearLayout>
</ScrollView>
Further reading has revealed that there are issues with scrolling components inside scrolling components.
One solution is to 'disable' the vertical scrolling of the ScrollView on the area of the contained scrollable component, in my case a ViewPager.
The code for this solution is detailed here(HorizontalScrollView within ScrollView Touch Handling) (and its simply brilliant!)
I have GridView with 6 images in 2 columns. I want to display this 6 images in the middle of the screen. I set gravity properties to center but this center elements only horizontally. GridView takes whole screen.
<GridView
android:id="#+id/gridview"
android:layout_above="#id/ad" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:background="#ffffff"
android:gravity="center"
/>
Here is what I did to get the items in the gridview to center (notice the stretchmode, columnwidth, gravity and horizontal and vertical spacing):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" />
</LinearLayout>
It took me a while to figure it out, but messing with those different values I was able to get them to center.
Try wrapping your GridView in a LinearLayout that has it's child elements centered and change your gridview's layout_height to "wrap_content". ex:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_gravity="center">
<GridView
android:id="#+id/homeGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
Need to set the following attributes:
android:stretchMode="columnWidth"
...and...
android:gravity="center"
well, in case you are using a custom adapter thing to inflate single rowview inside your gridview then try to add android:gravity="center" to the layout of your single_row.. this will do the trick hopefully :)
i.e,
<?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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivClrBlock"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="#+id/tvClrName"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="#string/color_name"
android:textSize="15sp" />
</LinearLayout>
Issue occurred when I updated the gridview adapter from:
gridView = inflater.inflate(R.layout.mobile, null);
to
gridView = inflater.inflate(R.layout.mobile, parent, false);
so I just reversed back using a try catch if the old (null) version failed.
Try to change this:
android:layout_height="fill_parent"
into
android:layout_height="wrap_content"
You are now telling your grid view to fill it's parent height and width fully.
Here is my answer:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dip" />
</LinearLayout>
Don't use padding or horizontalSpacing if you don't need it as it may cause problems with other views.
Note also that fill_parent is already deprecated.
I hope this will solve your problem:
<LinearLayout
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<GridLayout
android:id="#+id/gridlayou4x3"
android:rowCount="4"
android:columnCount="3"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="80"
android:minWidth="25px"
android:minHeight="25px" />
</LinearLayout>
I found that you just have the gridlayout wrap content on height and width.
android:layout_centerHorizontal="true"
It will center itself for the size it needs but if you have it matching the parent. You are technically centering it based with a lot of parent empty space offsetting your children.
What I want is a central column using GridView, but everything I try does not work. It always aligns to the left. I have tried messing around with layout_gravity and gravity to no avail.
I have a GridView in a LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:numColumns="1"
android:columnWidth="100dp"
android:stretchMode="spacingWidth"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical">
<GridView
android:layout_width="5px"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:numColumns="1"
android:columnWidth="100dp"
android:stretchMode="spacingWidth"
android:layout_gravity="1"/>
</LinearLayout>
This works fine for me. I changed the gravity to center_vertical and set the gravity of the gridview to 1. I also changed the gridview width just to see if it worked or not. With fill_parent it was hard to see if it was centered or not.
I ended up doing this instead:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id="#+id/grid_accounts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:verticalSpacing="25dp"
android:listSelector="#color/black"
/>
</FrameLayout>
And made time set the gravity of the images to center