Trying to get textView to stick to gridView scroll - android

I have a fragment with a textView giving instructions, and below a gridView. I wanted for the textView to scroll upwards with the gridView. In other words: for textView to disappear off screen when gridView is scrolling.
My idea of how to accomplish this is to give the gridView a fixed height that would make the entire layout scrollable - including the textView. However, up until I cannot achieve this. Is there a way to do this?
Layout no scroll:
When scrolled:
Any help/advice would be appreciated.
My layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/LightGrey"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/chooseLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="18dp"
android:text="#string/chooseLine"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="#+id/gridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="130dp"
android:fitsSystemWindows="false"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingTop="0dp"
android:stretchMode="columnWidth"
android:verticalSpacing="25dp" />
</LinearLayout>

Achieving this is is not going to be trivial. What you need is ListView's capability to add a header view, but unfortunately, GridView doesn't offer that functionality.
What I have done in the past to solve this problem is to convert the GridView to a ListView, and then add the header to the ListView. I created a wrapper adapter that takes the original adapter and combines a horizontal row's worth of grid cells into a single list row.
The tricky parts include: dynamically adapting the number of columns in a row based on the width of the screen, accounting for all combinations of view types within a row and remaining empty columns in the last row, and handling click interactions properly.

Put the whole LinearLayout in a ScrollView (How to use ScrollView in Android?) and for the GridView set android:layout_height="wrap_content"

Related

Android - Last view in RelativeLayout disappears

i want to display in a fragment 1 button, below this one gridView below which we have another button.
Everything is wrapped in a RelativeLayout like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/gridview_bouton_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/creer_categorie"
android:layout_centerHorizontal="true"></Button>
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:verticalSpacing="8dp"
android:padding="10dp"
android:layout_below="#+id/gridview_bouton_new">
</GridView>
<Button
android:id="#+id/gridview_bouton_valider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/valider"
android:layout_below="#+id/gridView"
android:layout_centerHorizontal="true"></Button>
It works fine until i have many items in the gridView.
Above 6 rows the fragment get vertically scrollable but the last button disappears !
I tried to nest everything in a ScrollView but only the first row is displayed....
If anyone has the solution it would be very helpful !!
Consider using RecyclerView for this purpose: https://developer.android.com/guide/topics/ui/layout/recyclerview
GridView is deprecated now and is not recommended to use. Also it is pretty limited.
RecyclerView, on the other hand, gives you ability to display as many items as you want dynamically.
You can also use GridLayoutManager to achieve desired grid look: https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager
You might Try LinearLayout instead of RelativeLayout and use ScrollView as parent.

Android ListView cutting off items wider than the first item

I have a Listview inside of a HorizontalScrollView. The listview holds file paths, and the horizontal scroll view is intended to eliminate the need to wrap the longer paths.
Everything is working exactly as desired with the exception that any items longer than the first item in the listview are being cut of at the length of the first item (whatever the width of the first item, none of the other items will scroll past that width).
Here is the section of the layout that is giving me trouble:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layoutListView"
android:orientation="vertical"
android:layout_below="#+id/spFileTypes">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/lvItems"/>
</HorizontalScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:id="#+id/tvStatus"/>
</LinearLayout>
Here is a screenshot of the issue:
The image is scrolled all the way to the right. The third item is cutting off ".php". I have tried this with long and short file names, lists of many (upper double digits) and lists with just a few, in each case, it seems the width limit is dependent of the width of the first item.
Whenever the lists changes, it always limits its width to whatever the first element is.
The desired behavior is to have it match the width of the widest item.
I've googled my heart out, dug through the official documentation, and I'm stumped. Any help is greatly appreciated :)
Instead of wrapping the ListView in HorizontalScrollView, try this:
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"/>

Expand ListView not working while adding button

I tried Expand ListView method from using the code from the following blog, https://wirasetiawan29.wordpress.com/2016/01/20/membuat-expand-listview-material-design-di-android/
Everything works fine. But if I add a button in FrameLayout then the touchevent for listview item not works properly. Also I tried changing FrameLayout to Relative & also to Linear, but still no success.
<FrameLayout
android:id="#+id/wrapper"
android:layout_below="#+id/rl_title_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/deskripsi"
android:padding="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="Share"/>
</FrameLayout>
Thanks in advance.
According to Frame Layout description by Google's documentation...
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
Hence, your original TextView is actually overlapping by the Button (Share). You can use android:layout_gravity="right" to position the button in the right end of the screen, however, then you will have to fix the maxium length of string for TextView, so that it doesn't get overlap by the Button on the right.
If you don't have any problem, might I suggest you to use LinearLayout? It's easier to handle and render by the GPU (As far as I know). Here's an example code of your item...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrapper"
android:layout_below="#+id/rl_title_wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/deskripsi"
android:padding="16dp"
android:layout_weight="1"
android:layout_gravity="left|center"
android:text="This is a big chunk of description for your listView item. you can write as much as you want...."
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:layout_gravity="right|center"/>
</LinearLayout>
You can also use RelativeLayout and GridLayout here.
I hope it answers your question. Cheers!

Horizontal scroll in android listView

I want to create list view, with ability to scrool horizontaly and vertically.
Image shows row item from list view.
Everything should act as normal list view - ability to scroll verticaly.
Besides Title ( Teszt Elk Nev ) rest data in row should also scrool horizontaly. ( as you can see on image - second row )
When I scroll horizontaly all rows in list view should scrool horizontaly, so all rows has the same horizontaly scrool position.
I spent few hours try to solve it. Best solution so far is horizontal scrool only one row.
My code for row item layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="30dp"
android:text="one" />
<com.example.testproject.CustomHorScroll
android:id="#+id/myHorScroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/myLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="70dp"
android:text="two" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="70dp"
android:text="three" />
<!-- rest items goes here --!>
</LinearLayout>
</com.example.testproject.CustomHorScroll>
</LinearLayout>
I want to handle horizontal scrool movement and propagate to all row in listview to set horizontalscroolView.scrolBy(x,0).
But I can't make it works. :/
Another idea is to skip listview and create custom layout with scroll view and horizontalScroll.
Unfortunately, Android doesn't support scrolling items within scrolling items generally speaking.
Your best bet is probably to use something such as a ScrollView or a HorizontalScrollView, and a LinearLayout containing lists (or other scrollable items) inside of that: http://developer.android.com/reference/android/widget/ScrollView.html
You probably want to look into use the two way listview library.
https://github.com/lucasr/twoway-view

Getting a button to scroll into view at the bottom of a GridView on Android

I am trying to port an existing iPhone application to android. I wish to have a button scroll into view at the bottom of a GridView to enable the user to load more data from the server. Presently, my solution simply fixes a button at the bottom of the screen instead of having it scroll into view.
Here is my layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="70dp"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#000000"
/>
<Button
android:id="#+id/load_more"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load More"
/>
</LinearLayout>
Fixing the button at the bottom of the screen won't work because I plan on placing an ad at the bottom.
Can anyone either explain conceptually how to get a load more button to scroll into view, or point me to some sample code, OR tell me why this is not idiomatic to Android and what other UI convention it uses to load more data in a GridView?
Thanks!
You can place a ScrollView inside your main LinearLayout. A ScrollView can only have one direct child, though, so you'll need to put another LinearLayout inside of it which would then contain your GridView and Button.
I had a similar problem with scrolling a GridView and after refreshing the underlying data, noticing that the scoll was not reset to the beginning. I solved it with the following code fragment
gvKeys.setSelection(0);
I'm guessing that if you know the number of items in your grid, N, that the following will work:
gvKeys.setSelection(N);

Categories

Resources