I have a ListView each item of which is ViewPager with some content (mostly images). Everything works fine bot not on Android 4.3 and above ViewPager's content is missing and ViewPager not scrollable. I don't know which part of source code to provide here - ask me and I'll try to provide you maximum info.
EDIT: Found out that ViewPager in my case has zero dimensions. But only on Android 4.3+.
Here is part of my layout:
<...AdaptableHeightImageView
android:id="#android:id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/slide"
android:visibility="invisible"
tools:ignore="ContentDescription" />
<...UninterceptableViewPager
android:id="#+id/list_item_showcase_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#android:id/icon"
android:overScrollMode="never" />
When I set ViewPager's width and height 100dp for example it becomes visible.
The UninterceptableViewPager is ViewPager subclass with locked X-axes. The AdaptableHeightImageView is ImageView subclass which fits the setted width but stretches it's height to save aspect ratio. The goal is to have ViewPager with the same dimensions that ImageView has.
If I write something like this:
<...UninterceptableViewPager
android:id="#+id/list_item_showcase_images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#android:id/icon"
android:layout_alignLeft="#android:id/icon"
android:layout_alignRight="#android:id/icon"
android:layout_alignTop="#android:id/icon"
android:overScrollMode="never" />
Then once my Views inside ViewPager or parent ListView are recycled they never appear again. Also in this case any ViewPager content which wasn't shown when data array is set will never be shown. But ViewPager will have dimensions and I even able to scroll it. Seems like Google changed something in Android 4.3.
In conclusion: everything works fine only if I set concrete width and height
Related
I ran into the issue concerning a ViewPager's height not being set correctly when setting android:height="wrap_content"
Now, I've successfully applied the fix mentioned here and it is working as expected. My ViewPager is correctly assuming the height of its tallest element.
However! Each of the items inside of my viewpager contains a linearlayout, which contains 2 elements, like so: (This is my container)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_margin="#dimen/gutterSpaceHalf"
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="#+id/foo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
This gives me in effect a layout that kind-of looks like this:
And what I'm trying to illustrate here is that, on the second page, LinearLayout foo will have a different height from the first page. This is expected.
But the problem comes in when I want to align the button on each page to the bottom! This is of course an issue as the bottom of foo on page one will not be the same as the bottom of foo on page two.
The standard solution, in my mind, would be to set the height of foo to match_parent. However, if I do this, the whole page becomes the height of my imageview, as the applied fix mentioned above can not calculate each child's height correctly anymore.
So, bearing in mind that my entire goal is to have equal-height pages, with dynamic content (the text), while keeping the buttons aligned to the bottom of the page, what solution would you suggest here?
Things I've tried include jiggering the height settings on layouts up the tree, and setting the height of each view to the measured height of its container upon being added in my ViewPagerAdapter, but nothing seems to be working how I want it to.
Any suggestions would be highly appreciated. Please comment if I've missed something or you'd like to see more information.
IVE DONE IT YES
All I had to do was add a view with weight=1 above my button, and give my button a static height. Now the button lines up to the bottom correctly on each page, due to the added view expanding to fill remaining size.
I also had to add +1 to the height as defined in the "hacky" solution I mentioned above, else the view I added would push items out of the largest page's layout, like so:
if (height != 0) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height + 1 /* This is a heuristic solution */, MeasureSpec.EXACTLY);
}
YESSSSSS
(layout changes):
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<View <!-- ADDED -->
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="#dimen/buttonHeight"/>
I have a very irritating problem with my views. As you see on a galaxy S7 my views is perfectly in center, but on a galaxy S4 mini they are placed down at the bottom!
I have tried to change alot in XML without any succses. I think it may be of the way I designet the whole thing with recyclerview, tabviews etc.
The following dont work either:
layout_InParent, Gravity:Center, layout_centerVertical
Imagebutton item for the recyclerview. item_colorbutton.xml:
android:id="#+id/colorbutton"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="15dp"
android:background="#drawable/bbtn">
BACKGROUND tabfragement. tab_fragment_1.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_marginTop="10dp"
android:id="#+id/rvColors"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is the viewpager for the different tabs: BACKGROUND, FONT, TEXT STYLE
<org.m.muddzboy.QuoteCreator.ViewPager.NonSwipeableViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="51pt"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:background="#517293" />
Your parent view seems to be limited height... and your recyclerview wraps children, plus it has top margin of 10dp. So you have 10dp, then 15dp + 15dp top and bottom margin of child view (you set general margin of 15dp, so all sides), plus 48dp height of child view. Seems like on low density devices that is not enough for your view which contains recyclerview. So it follows the rules and pushes your views down. It cannot break set margin. It has to set them as you requested.
In situations like this it's better to have dimensions for different device configurations. And if it's too complex, setting relative dimensions programmatically based on measured height of containing view.
I'm trying to insert a fragment into my application and it's essentially a coloured bar with a few buttons on it. However, whenever I put the fragment onto the main xml file, there's always a bit of a white margin regardless of whatever I do. Here's an sample of some of the fragment code I have in my main xml:
<fragment
android:layout_width="fill_parent"
android:layout_height="50dp"
android:name="sample"
android:id="#+id/sample"
tools:layout="#layout/sample"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_margin="0dp" />
Even though I set the margin to 0dp and the width to fill_parent, there's still a white margin/border on the outside. Is there any way to make a fragment fill the screen widthwise entirely? Thank you!
You have padding and/or margin defined in your parent's ViewGroup that is causing the Fragment's extra spacing. Check the Parent who contains the Fragment and remove the padding :)
I have a ViewPager:
<android.support.v4.view.ViewPager android:id="#+id/ft_graph_pager"
android:layout_width="match_parent"
android:layout_height="#dimen/bar_graph_pager_height"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ft_rsvp_graph_indicator"/>
Which is a child of a RelativeLayout. Behind this ViewPager is a 9patch image representing the lines of a graph.
The content of the ViewPager should line up with the 9patch graph lines, but I can't just make them the same height and call it a day because the ViewPager contents have a caption which should be positioned below the graph lines.
With the XML above, this is what it looks like:
I'm super close. As you can see, if I can just get the ViewPager's contents (the bar graph items and San Diego below) to shift down slightly I'm in business.
The first thing I tried to do is set layout_marginBottom="-6dp" which did nothing.
Then I tried to set paddingBottom="-6dp" which also did nothing.
Setting paddingTop="6dp" doesn't do anything either, but setting the bottom padding to a positive number DOES have an affect, even though it is completely unhelpful for me.
The RelativeLayout parent for this Pager and 9patch contains another view as well, one which is larger than both and hinges on the 9patch being perfectly vertically centered, so I can't make the 9patch move instead.
The full contents of the RelativeLayout are below:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/ft_bar_graph_indicator"
android:layout_width="#dimen/bar_graph_indicator_width"
android:layout_height="#dimen/bar_graph_indicator_height"
android:orientation="vertical">
<!--TODO fill-->
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="#dimen/bar_graph_height"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ft_bar_graph_indicator"
android:background="#drawable/graph_lines"/>
<android.support.v4.view.ViewPager android:id="#+id/ft_graph_pager"
android:layout_width="match_parent"
android:layout_height="#dimen/bar_graph_pager_height"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ft_rsvp_graph_indicator"/>
</RelativeLayout>
Any ideas?
Well, I was unable to figure out how to do this via XML, so if someone is able to do that go ahead and post it and I will make that the official answer.
However I was able to come up with a work around in Java. When I declared my ViewPager in my Activity, I just added this:
Display display = mActivity.getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
ViewHelper.setTranslationY(mGraphPager, 5 * metrics.density);
5 being the dp I ended up shifting it down. ViewHelper is part of the NineOldAndroids library, which helps simulate methods from the View class added in API 11+
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