Android GridView (Caldroid) scroll margin on the bottom - android

I have a Caldroid Fragment that generates a calendar based on a GridView. It takes the complete screen. Then I added an AdView on the bottom of this activity, the problem is that banner covers some cells of the grid so user can't click on them. Basically, I want to set a margin in the inner part of the GridView to allow the user to scroll down the calendar, and when the calendar reach at the end, it continues scrolling for some more margin. This margin will be set with the height of the banner.
I am up to modify the caldroid code if necessary but I can't find how to produce this effect with a GridView. Setting a bottom padding avoids overlapping the AdView but by discarding the whole width, I would like to see the calendar at right and left of the banner when it is not completely scrolled down.
POSSIBLE SOLUTIONS:
-Maybe by adding a margin to the cells produced by the adapter will do it, but I need to know if they are from the last row. using getItemId always returns 0, and the methods getClipBounds and getClipToOutline are limited to API 21.

Use android:clipToPadding="false" with android:paddingBottom= in your GirdView
<GridView android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dip"
android:clipToPadding="false" />

I didn't get why you need inner margins.
Can't you put your GridView and your AdView in the same RelativeLayout
On the GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="x"
On the AdView
android:layout_alignParentBottom="true"
x being the height of your AdView
No overlapping, functional scroll.

Related

HorizontalScrollView's child not matching it's parent view's width even on setting android:layout_width="match_parent"

I want to display a horizontal list of scrollable buttons at top of the phone screen containing 30 items, for this purpose I am using a HorizontalScrollView with a LinearLayout with "horizontal" orientation as it's child but the linear layout is not taking up the entire phone width even on setting its width as "match-parent". Here's the code :
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<GridView
android:id="#+id/gridView_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchMode="columnWidth" />
</LinearLayout>
</HorizontalScrollView>
Here is shown in the image that linear layout is not taking up the entire space and LinearLayout is only covering some of the space. Also on changing the size to a fixed size, I noticed that the HorizontalScrollView was actually behaving like a vertical Scroll View only.
NOTE: Also if there is an alternative way to display a horizontal list of buttons with 30 items with numbers from 1 to 30 on it, please suggest it.
To achive this easily you may use Recyclerview with Horizotanl Layout Manager.For Example
recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false));
I don't think you need a HorizontalScrollView with a nested LinearLayout for what you are trying to achieve. It would be a better idea to simply use a horizontal RecyclerView if your button layouts are similar and the functionality of the buttons is similar too. Refer to this answer for help https://stackoverflow.com/a/40584425/9119277

How to resize Layout height based on the position of child views

I have LinearLayout that has child views. The child views are stack one at the top of the other because the parent layout's orientation is vertical. All the children have their height and width wrap_content so they move freely anywhere within the parent layout. User can move the views and remove them as they wish. When the user removes a view, the parent layout height resizes. So far my code works well.
But when the user moves one view to the side of another view I would like the parent layout to resize it's height the same way as if the user removed the view. I tried to get the height of the parent and subtract the distance the child moved and redraw the layout but that didn't work. I also tried other ways but I have not been successful yet. Can someone help me?
You can use GridView for your case, where you can set the number of columns (android:numColumns) to 1, when vertically aligned and when moving to the side make it to 2.
Here is the documentation on how to use GridView https://developer.android.com/guide/topics/ui/layout/gridview.html
<GridView android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_margin="10dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp" />

ViewPager and ViewPagerIndicator inside RelativeLayout making RelativeLayout expand through whole screen

This is what is causing me many problems:
<RelativeLayout
android:id="#+id/galleryLayout"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#color/white">
<android.support.v4.view.ViewPager
android:id="#+id/imageViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circlePageIndicator"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="#dimen/padding_middle"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Right now this code kind of works only because I've set RelativeLayout's height to 180dp but I don't want to have it this way. I want the whole thing to have height according to ViewPager's child.
There are exactly 2 problems if I set RelativeLayout's height to WrapContent.
Problem 1
ViewPager will expand throughout the whole screen. It just doesn't respect it's wrap_content height attribute. But I've partially solved that with this answer. I'd still appreciate if there's a better solution though.
Problem 2
I want have CirclePageIndicator on the bottom of it's parent (RelativeLayout) so I've added attribute layout_alignParentBottom="true" but now because of this, the RelativeLayout will expand throughout the whole screen for some reason.
So what I'm trying to have is a RelativeLayout which wraps around ViewPager which wraps around it's child. The child is downloaded from web so I can't pre-set it. And on the bottom of that RelativeLayout, I want to have a ViewPagerIndicator.
As for the problem 1 you solved the issue correctly, ViewPager will not wrap its children by default.
As for the second problem, this is a normal RelativeLayout behaviour. If you set its height to wrap_content and add two children, one with layout_alignParentTop="true" and second one with layout_alignParentBottom="true", they will stretch your layout height-wise.
What you should do is: ask yourself if you really need RelativeLayout. If you've provided the whole layout of yours I don't see a need for RelativeLayout (its costly). Vertical LinearLayout would do just fine. If you decide that you really need RelativeLayout, try changing your Indicator's rule from layout_alignParentBottom="true" to android:layout_below="#+id/imageViewPager".

Can't set side padding/margin to ViewPager container layout

I'm tryintg to implement Caldroid calendar in my app with few modifications. There is custom ViewPager there:
<com.antonyt.infiniteviewpager.InfiniteViewPager
android:id="#+id/months_infinite_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"/>
Pager is located under one parent layout. What I wanted was to position the pager inside it's parent using margin or padding. It appears that setting side (left, right) margin or padding makes the app loop somewhere inside ViewPager onMeasure() method.
Can't find any information, that ViewPager can't have a parent with side margin/padding set. onMeasure() contains now only super.onMeasure(widthMeasureSpec, heightMeasureSpec) and It seems that it loops in native libraries for some reason. That doesn't happen without margin/padding
Problem was in the container layout:
<LinearLayout
android:id="#+id/right_page"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
Just a layout taking half of the screen, so what? It seems the problem was android:layout_width="1dp". Change to android:layout_width="200dp" helped. it doesn't matter if it's 200, 100 or any other number. I think the ViewPager was positioning itself on 1dp first, and then recalculated from percentage width, but just guessing

ActionBar overlay with GridView padding or custom GridView to show entire first image?

I have an activity with a GridView inside it. The ActionBar is set to overlay mode.
Right now, you can only see half of the first image because the ActionBar cuts it in half.
How do I add padding to the interior of the GridView so that it initializes in such a way that you can see the entire first image? Or is there another way? For example, how would I go about extending GridView to create one that has a built-in configurable, dynamic gap at the front?
example (although ListView instead of GridView): reddit is fun app: https://play.google.com/store/apps/details?id=com.andrewshu.android.reddit
edit: I'm hiding the ActionBar whenever the user scrolls down at a certain rate or past a certain level.
Use a ViewTreeObserver.OnGlobalLayoutListener in your Activity or Fragment to determine the number of columns your GridView will display (presuming it varies based on screen size and orientation), then use that number in your Adapter implementation. In getView(), if position is less than the number of columns, return an empty view whose height matches the Action Bar, otherwise bind your data as you would normally.
There is an excellent example that does exactly what you want in Google's "Displaying Bitmaps Efficiently" sample application: https://developer.android.com/training/displaying-bitmaps/display-bitmap.html
Here is the relevant source code:
https://android.googlesource.com/platform/development/+/master/samples/training/bitmapfun/src/com/example/android/bitmapfun/ui/ImageGridFragment.java
Try adding android:layout_marginTop="?android:attr/actionBarSize" to the parent of your GridView, or the GridView itself if it doesn't have one.
This will push your layout down so that it rests below the ActionBar.
Edit
You may want to conside using a ListView instead of a GridView. Reason being, you can easily achieve that effect by creating a fake header and then calling ListView.addHeaderView. You can't do the same with a GridView. What you're talking about can definitely be done with a GridView, but it will require you to subclass it and modify it quite a bit.
Header
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize" />
Having ActionBar in overlay mode, the following works for me:
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
android:clipToPadding="false"
android:numColumns="auto_fit"
android:columnWidth="120dp"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:id="#+id/gridLibrary" />
The most important lines here are: android:paddingTop and android:clipToPadding.
In my application, when I open an activity with the gridview above, the first row is fully visible. Then, when I scroll down, the ActionBar hides and the gridview fills up all the screen.

Categories

Resources