I have a list view with two textviews, one of them has its visibility set to View.GONE by default, so the listview does the wrap_content for the minimum size, the size of the listview with all the secondary textviews GONE, when I turn one of them View.VISIBLE (by clicking on the item), the height stays the same height it calculated from the very first wrap_content when it was first viewed. Tried different approaches that I read but none of them worked on my case such as
mListView.invalidate();
mListView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)mListView.getLayoutParams();
params.height = params.WRAP_CONTENT;
is there any way to force the view to recalculate the height wrap_content value after each click?
Edit1:- This is my listview layout (two is the one that is set to GONE)
<?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">
<TextView
android:id="#+id/list_item_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Question?"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="8dp"
android:paddingBottom="4dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/list_item_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer!"
android:layout_below="#id/list_item_one"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"/>
</RelativeLayout>
You should call requestLayout() when you want to re-calculate the dimensions. That is according to this neat view lifecycle:
Original link here.
Related
I am using the RecyclerView to inflate a Linear Vertical listViews, However, although the width of a child is set to match_parent, RecyclerView wraps that at runtime.
Here is a screenshot of the problem:
item_recyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tag_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:maxLines="2"/>
<TextView
android:id="#+id/tag_role"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/user_pic"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_weight="0"
android:rotation="-45"
android:scaleType="centerCrop"
android:src="#drawable/ic_user"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.circleImageView" />
</LinearLayout>
When inflating a layout file, the layout_ attributes on the root view will be ignored unless you specify a parent ViewGroup in the inflate() call. You are not currently doing so.
In your onCreateViewHolder() method, replace this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater);
with this:
ItemQuestionPostBinding binding = ItemQuestionPostBinding.inflate(inflater, parent, false);
Note also that you probably don't want to be using match_parent for the height of your RecyclerView's items:
android:layout_width="match_parent"
android:layout_height="match_parent"
This will give the appearance that only a single item is visible in your RecyclerView, since each item will be the full height of the screen. This is currently not a problem for the exact same reason that the match_parent width is being ignored.
give for your parent linearlayout weightSum 5
give for imageView weight 1 and do his width 0
and for child linearlayout weight 4
enter code here
My view is like:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
/>
</LinearLayout>
It will give 50dp to TextView and rest of area to RecyclerView.
But if items in RecyclerView are less than, say 1 or 2, then it should shrink to the height of it's child views other wise behave like layout specifies. Can any one help??
I think the best option would be to change LinearLayout.LayoutParams of the root view dynamically. When the RecyclerViewhas a low amount of children, then set LinearLayout params to:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.setParams(params);
otherwise, leave as it is.
It is very easy to detect if RecyclerView should be wrapped. Obtain LinearLayout height:
int height = layout.getTop() - layout.getBottom()
If this height is lower than the expected maximum height (screen height or other predefined value).
A simple approach would be using RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/recyclerView" />
</RelativeLayout>
I am new to Android programming and I would like to make a table/grid (generically speaking, not sure the best layout to use just yet). I want the table to fill the entire screen, but I want to have variable sized height and width for cells. For example I would like column one to be 1/3 of the display width, and column 2 to be 2/3 of the display width. Similarly, I would like some cells to be 1/4 of the screen height, and others to be 1/2 of the screen height, etc. What is the best layout to use for this? I am looking at GridLayout, LinearLayout, and TableLayout.
Tx!
You can do it easily using LinearLayout.
Set its layout_weight at 1.
Then, set the layout_weight of your first element at 0.3 and the layout_weight of your second at 0.66. Then it will give to your first element 1/3 or the screen and the rest to your second one.
Put for each element a width of 0dp. Then according to the weight your gave to your elements, your elements will spread on the screen.
Hope it helps.
There are two approaches to this:
1) If you know what all your items will be and can pre-define them.
I would use LinearLayouts inside LinearLayouts (parent orientation set to vertical, children set to horizontal). Then use weight_sum on the parent (e.g. 3) and layout_weight on the children (e.g. 2, 1). Set the children layout_width to 0dp.
2) If you are trying to represent a variable list of data
This is a little more complicated, but you might be best off using a RecyclerView with a GridLayoutManager which will allow you to programmatically set the span of the items based on type.
Edit:
Example of #1:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4">
<TextView
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:background="#abc"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#cda"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4">
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:background="#af0"/>
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:background="#ffa"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4">
<TextView
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:background="#f00"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Is it possible to have two views that are the same height of the activity screen inside of a linear_layout inside of a scrollview? I tried achieving this like so:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
The only thing that appears one button with the height of the screen. The other button seems to have disappeared.
I suppose it is paradoxical to have two views to with the attribute match_parent.
However, I was hoping to get two views the same size of the available screen using xml, and having the scrollview make both views accessible. I am aware that it can be done via java, but I want an answer for xml.
In Linear Layout you have to define two things...
1 .android:orientation="vertical"
2 .android:weightSum="2"
weightSum is sum of all element in it's parent layout.It Enable you to define the proportion of width/height a element should take in a layout..
For further details go to weightSum
<ScrollView 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:layout_below="#+id/textView1"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<Button
android:id="#+id/but1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
<Button
android:id="#+id/but2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
</LinearLayout>
</ScrollView>
output
You cannot achieve this in XML only.
As android support multiple screen sizes, At runtime you need to check for each device , the height for each device can be calculated like this
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
With above code you will get the height of screen and you need to set this height in dp to your view at runtime.
Do this in your activity for both the buttons:
// get view you want to resize
Button but1= (Button) findViewById(R.id.but1);
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
height , LinearLayout.LayoutParams.MATCH_PARENT);
but1.setLayoutParams(listLayoutParams);
I have a listview inside a linear layout whose height is set as "WRAP_CONTENT", so the list is appearing inside the linear layout, when i populate the list, i attach some hidden list items within each list row, i want to show these items onListitemClick listner,
Everything is working fine,but if i have only one list child, then i have to scroll the view to see that layout that is visible now,
My question is:
I want to increase the height of that linear layout when the list item is clicked and and the view is visible..
here is xml..
<LinearLayout
android:id="#+id/listContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//used this relative layout to set different backgroung for each time..
<RelativeLayout
android:id="#+id/listHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/categoryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</RelativeLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
When the hidden item is visible, then i use this code to increase height,
LayoutParams paramsLinearLayout;
LinearLayout layoutContainer;
layoutContainer = (LinearLayout) findViewById(R.id.listContainer);
paramsLinearLayout = layoutContainer.getLayoutParams();
layoutContainer.setMinimumHeight(paramsLinearLayout.WRAP_CONTENT);
Use this piece of code :
LinearLayout linear = new LinearLayout(getContext());
LayoutParams linearParams=new LayoutParams(LayoutParams.MATCH_PARENT,Height what you want);
instead of :
LayoutParams paramsLinearLayout;
LinearLayout layoutContainer;
layoutContainer = (LinearLayout) findViewById(R.id.listContainer);
paramsLinearLayout = layoutContainer.getLayoutParams();
layoutContainer.setMinimumHeight(paramsLinearLayout.WRAP_CONTENT);
I think you should not have a view with height match_parent inside a layout with height wrap_content, as in your case:
<LinearLayout
android:id="#+id/listContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
//used this relative layout to set different backgroung for each time..
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Can't you set linearlayout's height to match_parent as well?