I'm trying to use PercenRelativeLayout into ListView, but it doesnot works,
height and width percents are ignored and nothing were showing into the listview.
it works only for marshmallows.
here is list item xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="#d20404"
android:id="#+id/test_image_id"
android:layout_width="match_parent"
android:layout_height="300dp" />
<TextView
android:background="#000"
android:text="sfgfashdsfg"
android:layout_below="#+id/test_image_id"
app:layout_heightPercent="50%"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.percent.PercentRelativeLayout>
I have a sample project on github
I have opened this issue to google. The answer were
The height of your ListView row item is insufficiently specified.
The first child says it wants to take 60% of the row height (note that
this is heightPercent and not aspectRatio), and the second child says
it wants to take 10% of the row height. But nothing tells ListView how
tall the entire row wants to be. So it ends up being with height 0.
Note that the semantics of height=match_parent do not work in ListView
rows, and if this works in any one particular Android platform version
(for however much you can say it works), it is purely incidental.
https://code.google.com/p/android/issues/detail?id=202479
I encountered the same problem with android K and android L.
The percent layout won't work correctly before android M. The reason is that they depend on the size hint being delivered in the measuring step. Before android M most layouts would provide size hint 0.
Convert your percent relative layout to relative layout and set the height of the views programmatically according to your device height.
This is not a very elegant solution but it worked for me.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/test_image_id"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#d20404" />
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/test_image_id"
android:background="#000"
android:text="sfgfashdsfg" />
</RelativeLayout>
Java Code:
// to get the height of the screen
int height = getWindowManager().getDefaultDisplay().getHeight();
// to set height to each textview to 50% of screen
textView1.getLayoutParams().height = (int) (height * 0.50);
Related
Newbie question here, but I'm struggling to find the answer.
When laying out components, clearly if android can fit all the components on the screen while respecting all the requested attributes (height, margin etc), then it will do that.
But what if there is not enough room on the screen for everything? I had thought that it would simply put what it could on the screen starting at 0,0, and then have horizontal or vertical scrollbars for the rest.
However, that's not what I'm seeing, but I can't figure out what I am seeing. For example, when I try to view the following layout on my phone, the text has disappeared off the top of the screen (I can get this by turning the phone to landscape mode where there is obviously less available height).
What gives? How is android determining how to lay out these components and where to place the centre of the screen?
Many thanks!
EDIT: It looks like the problem has to do with me using so many dp values that mean that there's no way for everything to fit on the screen. I've fixed my immediate problem by using layout 'weight' to scale my components proportionally to the screen rather than trying to specify absolute sizes. I'm still curious why the layout code would choose to push my text off the top though - perhaps it priortises dp values?
The layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="#string/TextView1Text2" />
<Button
android:id="#+id/button"
android:layout_width="200dp"
android:layout_height="150dp"
android:onClick="#string/Button1OnClick"
android:text="#string/Button1Text" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/bla"
app:srcCompat="#drawable/blank_1024_200" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I need to implement an activity where I have a listView and each item of the listView is a ViewPager which has a swipeable list of images fetched from a server (Similar to the AirBnB app, in which on choosing a city you are presented with a list of available homes and each home has a swipeable set of images).
My listView row item is like this (row_item.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
tools:context=".MainActivity" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rootView"
android:descendantFocusability="blocksDescendants">
<android.support.v4.view.ViewPager
android:id="#+id/photos_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
The layout for the ViewPager is like this (view_pager.xml):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/root"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
I want the images loaded to fill the width of the mobile phone and scale the height appropriately.
But in row_item.xml unless I specify an explicit height for the FrameLayout, i.e. layout_height= "250dip" or something like that, I cannot see the images loaded. It appears as if there is no space allocated on the layout for the viewPager unless an explicit height is specified.
I do not want to specify an explicit height as there will be scaling issues on some devices. An image might have a smaller height than the specified height in dip, and if it tries to scale up to match the specified height the width of the image gets cropped out in the process.
How do I fix this?
I am wondering if it is possible to make one View adjust it's height to it's parent height when the parent has layout_height="wrap_content". In following example:
<?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:background="#color/red"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/time"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"/>
</RelativeLayout>
the nested ImageView makes RelativeLayout expand it's height to match it's own parent. What I would like to achieve is that RelativeLayout has height equal to the one of the inner TextView and ImageView should work here as a background spanning only behind the text. Is this possible in pure XML without Java code tricks?
If you use a RelativeLayout (as you do), you can have Views or ViewGroups layout referencing other Views or ViewGroups by id.
In your case, if you assign an id to your TextView android:ud="#+id/tvHello" then you can align the top and bottom of your ImageView to that TextView:
android:layout_alignTop="#+id/tvHello"
android:layout_alignBottom="#+id/tvHello"
For completeness, here's your layout:
<?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:background="#color/red"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignTop="#+id/tvHello"
android:layout_alignBottom="#+id/tvHello"
android:src="#drawable/time"/>
<TextView
android:id="#+id/tvHello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"/>
</RelativeLayout>
As you can see, I made the height of the ImageView 0dp. I could've chosen for wrap_content or maybe even match_parent, but because the alignTop and alignBottom overrules it's, it is better to give the view a fixed height. This is due to performance. Now android doesn't have to measure the height of the ImageView, before it's to decide to make it the same height as your TextView after all.
One other way (and perhaps better - depending on how you want your images to scale), is to attach a drawable as background to your RelativeLayout. As such:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/time"
android:orientation="vertical">
I hope this helps you out a little bit and it's helpful (it is my first time answering on stackoverflow).
happy coding!
I am dealing with a weird problem.
I am using the following layout for ListView rows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:jn="http://schemas.android.com/apk/res/com.appeaser.justnoteproject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<BUNCH OF VIEWS />
<RelativeLayout
android:id="#+id/rlCon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/margin_note_bottom"
android:layout_marginLeft="#dimen/margin_note_left"
android:layout_marginRight="#dimen/margin_note_right"
android:layout_marginTop="#dimen/margin_note_bottom" >
<BUNCH OF VIEWS />
<View
android:id="#+id/vOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blue_overlay"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
In my list adapter, I toggle vOverlay's visibility based on a certain condition. But, even after toggling the visibility, vOverlay is not displayed.
To verify that the code works as intended, I changed the height and width of vOverlay to 50dp. When I did this, vOverlay's visibility worked as intended - except that the size I need is match_parent.
Could someone explain what I'm doing wrong? How can I get intended results?
replace outer RelativeLayout with FrameLayout and move your overlay to be a second child of that FrameLayout
I would like to restrict the height and width of a ListView, wrapping the height based on the number of children's and wrapping the width based on width of list view row.
So far, I have been able to restrict the height of the ListView. The code I'm using for restricting height is:
listview.setLayoutParams(new linealayout.layoutparams(LayoutParams.WRAP_CONTENT, 0,1));
How can I also restrict the width?
I hope you should be more specific about your desired screen look/view
But when it comes to listview I would say never use 20,30 dp to set width and height just use weight and wrap_content to make it flexible.
see below code example this may help you
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ListView
android:layout_height="wrap_content"
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_weight="1"></ListView>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
let me know if you still have any trouble to get this one.