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?
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
I am trying to align dynamically created Linear layout to right of its parent which is a ListView. The code of list view is
<LinearLayout
android:id="#+id/mychatlinear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="7"
android:orientation="horizontal"
android:padding="5dp" >
<ListView
android:id="#+id/chatmainlist"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:divider="#color/ripple_material_dark"
android:dividerHeight="5dp"
android:padding="5dp"
tools:listitem="#layout/chatrecieve" >
</ListView>
</LinearLayout>
Now in the inner class extending the BaseAdapter i have used
arg1 = getLayoutInflater().inflate(R.layout.chatitem, null);
LinearLayout layout = (LinearLayout) arg1;
This layout is aligned to the left in the list view. I want it to aligned to right. Please suggest some solution. Thanks
Change LinearLayout in chatitem.xml add android:gravity="right"
There is a frame layout on my screen (see code below). This layout consists of 2 layers. The first layer is LinearLayout with several views and its height = "wrap_content". The second is ProgressBar with "fill_parent". I need the progressbar to stretch itself to the linear layout's height, but now it doesn't consider linear layout's height.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progress"
android:indeterminateOnly="false"
android:progressDrawable="#drawable/progressbar_drawable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/file_item_background">
---
</LinearLayout>
</FrameLayout>
Instead of a FrameLayout, try a RelativeLayout.
You can then set android:layout_alignTop and android:layout_alignBottom on your ProgressBar to match the LinearLayout's height like so:
<ProgressBar
android:id="#+id/progress"
android:indeterminateOnly="false"
android:progressDrawable="#drawable/progressbar_drawable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/my_linear_layout"
android:layout_alignBottom="#+id/my_linear_layout" />
<LinearLayout
android:id="#+id/my_linear_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/file_item_background">
---
</LinearLayout>
Another option would be to manually adjust the height in code.
In general, you want to use a RelativeLayout if your layout requirements call for a position or a size relative to another view.
I have a data entry type activity and im using a linear layout to space out evenly the sets of textviews and edittexts. Then I have a scroll view that is supposed to make it so the user can scroll while the softkeyboard is up.
If I use android:fillViewport the linearlayout works properly and fills the screen and spreads each item out evenly but then when the keyboard comes up and stops each item being spread out evenly. If i use android:windowSoftInputMode="stateVisible|adjustPan" then the linearlayout remains spread out but the scroll view doesn't work anymore (from all the unsolved posts on this i don't think you can have a working scroll view with adjustPan)
is there any way to have a linearlayout inside a scrollview, with items spread out evenly and still work while the softkeyboard is up with out changing the linearlayout?
<?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
>
<LinearLayout
android:gravity="left"
android:orientation="vertical"
android:id="#id/tab2"
android:paddingLeft="40.0dip"
android:paddingTop="0.0dip"
android:paddingRight="40.0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<TextView
android:id="#id/textViewBrand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/brand" />
<AutoCompleteTextView
android:id="#id/editTextBrand"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</AutoCompleteTextView>
</LinearLayout>
...more linearlayouts with textview and edittext to be spaced out evenly
set the property android:fillViewport="true" in your scrollview it will work
Maybe the ScrollView isn't working because your
android:layout_height attribute
is defined to match_parent. You have to put
wrap_content
Follow This Code, I hope it resolves your Problem
<?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">
//Your Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100">
// First Sub Layout Under Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:weightSum="100" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TextView"
android:layout_weight="70" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30" />
</LinearLayout>// Finishing First Sub layout
// Second Sub Layout Under Main Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:weightSum="100" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="TextView"
android:layout_weight="70" />
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30" />
</LinearLayout>// Finishing Second Sub layout
similarly for 3rd,4rth,5th sub layouts and so on........
</LinearLayout> // Finishing Main Layout
</ScrollView> // Finishing ScrollView
you can go over the child views of the LinearLayout and set their size to a fixed size yourself in code using a ViewTreeObserver.OnGlobalLayoutListener.
the following code sets the sizes to not change, so you can use the android:fillViewport option with weights, and then essentially remove the weights, but keep the calculated sizes.
I used this code with some modifications to also make sure child views are distributed evenly in a LinearLayout that is inside a ScrollView, where the LinearLayout is too big for the android:fillViewport option to get the weights to actually work. this was done by doing 2 iterations over the children. first getting the max view's height, and then setting all child views to that max height.
private void distributeViewsEvenlyInLinearLayout() {
ViewTreeObserver observer = linearParentView.getViewTreeObserver();
final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressWarnings("deprecation")
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
linearParentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
linearParentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
for (int i = 0; i < linearParentView.getChildCount(); i++) {
View child = linearParentView.getChildAt(i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, child.getHeight());
child.setLayoutParams(layoutParams);
}
}
};
observer.addOnGlobalLayoutListener(globalLayoutListener);
}
I have a Scrollview inside that i have a LinearLayout. LinearLayout has some views.
For ex:- My LinearLayout height is 100.
After i removed all views from LinearLayout also i get the same height as 100.
How to update the current height. That is 0 when no views is present.
My code:
<ScrollView android:layout_weight="1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:fillViewport="true">
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:orientation="vertical" android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
Remove the android:layout_weight="1" from you LinearLayout if you want it to get 0 height if no views are visible.