Why did ConstraintLayout in RecyclerView change its width setting at runtime? [duplicate] - android

My RecyclerView and item has match_parent width but the result is :
<view
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
and items:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_itm"
android:orientation="horizontal"
android:layout_width="match_parent"
full:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/ll_itm"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:gravity="right"
>
<Button
android:layout_width="0dp"
android:layout_weight="15"
android:layout_height="fill_parent"
android:text="ملاحظات"
android:id="#+id/button" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="20"
android:gravity="center"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
fab:fab_plusIconColor="#ff56ff83"
fab:fab_colorNormal="#color/d_red"
fab:fab_colorPressed="#ff5c86ff"
fab:fab_size="mini"
fab:fab_icon="#drawable/ic_remove_white"
android:id="#+id/fab_rmv" />
<esfandune.ir.elmikarbordiardakan.other.CustomTxtView
android:layout_weight="25"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:gravity="right|center_vertical"
android:id="#+id/txt_takhir_itm" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
fab:fab_plusIconColor="#color/colorprimarylight"
fab:fab_colorNormal="#color/colorprimarydark"
fab:fab_colorPressed="#color/colorprimary"
fab:fab_size="mini"
fab:fab_icon="#drawable/ic_add_white"
android:id="#+id/fab_add" />
</LinearLayout>
</LinearLayout>
<Spinner
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="10"
android:id="#+id/sp_nomre_itm"
android:entries="#array/degrees"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="10"
android:gravity="center"
>
<!--LinearLayout baraye ine ke nameshod fab ro weight behosh dad-->
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="fill_parent"
android:layout_height="fill_parent"
fab:fab_plusIconColor="#ff56ff83"
fab:fab_colorNormal="#color/d_green"
fab:fab_colorPressed="#color/d_orange"
fab:fab_size="normal"
fab:fab_icon="#drawable/ic_done_white"
android:id="#+id/fab_hazr" />
</LinearLayout>
<esfandune.ir.elmikarbordiardakan.other.CustomTxtView
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="100"
android:gravity="right|center_vertical"
android:id="#+id/txt_ghybtNumber_itm" />
<esfandune.ir.elmikarbordiardakan.other.CustomTxtView
android:layout_weight="30"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="عباسعلی ملاحسینی اردکانی"
android:gravity="right|center_vertical"
android:id="#+id/txt_title_itm"
android:layout_marginRight="10dp"
/>
<view
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="10"
class="de.hdodenhof.circleimageview.CircleImageView"
android:id="#+id/view"
android:src="#drawable/mmrdf"
/>
</LinearLayout>

In your adapter where you are inflating the item in onCreateViewHolder, is the second parameter of the inflate call null?.
If so change it to parent which is the first parameter in the onCreateViewHolder function signature.
View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, parent, false);
If you need the second parameter to be null then when you get the view reference on inflating, do the following
View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);

Inside onCreateViewHolder(...) method of adapter where you are inflating the view.. you have to define the ViewGroup as the parent.This you will get from the 1st parameter of onCreateViewHolder(...) method.
see the below line in the second parameter i'm passing the ViewGroup. This will automatically match the view to its parent:
rowView=inflater.inflate(R.layout.home_custom_list, parent,false);
///the complete code is below
public View onCreateViewHolder(ViewGroup parent, int position) {
// TODO Auto-generated method stub
View rowView;
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView=inflater.inflate(R.layout.home_custom_list, parent,false);

I was using a FrameLayout with MATCH_PARENT for width and was seeing the same behavior with a RecyclerView + LinearLayoutManager. None of the above changes worked for me until I did the following in the onCreateViewHolder callback:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_layout, parent, false);
v.setLayoutParams(new RecyclerView.LayoutParams(
((RecyclerView) parent).getLayoutManager().getWidth(),
context.getResources()
.getDimensionPixelSize(R.dimen.note_item_height)));
return new ViewHolder(v);
}
Clearly looks like a bug in (I'm guessing) the RecyclerView implementation.

try this when you set layout params for your item in adapter.
View viewHolder= LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
viewHolder.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
ViewOffersHolder viewOffersHolder = new ViewOffersHolder(viewHolder);
return viewOffersHolder;

I had done fix like this. In my case problem with activity layout file because i am using ConstraintLayout as root activity layout.Might be case for you too.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<include
android:id="#+id/toolBar"
layout="#layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/accent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/toolBar" />
</android.support.constraint.ConstraintLayout>

This worked for me.
replace this
View view = View.inflate(parent.getContext(), R.layout.row_timeline, null);
return new TimeLineViewHolder(view, viewType);
by this
View rootView = LayoutInflater.from(context).inflate(R.layout.row_timeline, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new TimeLineViewHolder(rootView, viewType);

In my case, the problem was in RecyclerView XML declaration, the layout_width was 0dp which means match_constraints, when I changed it to match_parent, items started to fill all RecyclerView width:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp" <-- changed this to "match_parent"
android:layout_height="0dp"
android:layout_marginBottom="45dp"
android:background="#android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="360dp"
app:layout_constraintHeight_min="60dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/header"/>

Simply adding a dummy View with 0 height and full width in root worked for me.
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".screen.gallery.GalleryFragment">
<!----DUMMY VIEW----->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/navigationList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>

I solved this with:
myInflatedRowLayout.getLayoutParams().width = vg.getWidth();
It is replacing the MATCH_PARENT with the actual width of the RecyclerView.

I was facing the same problem when using Linear layout and constraint layout for my compound view used in recycler view, but it worked when I change to relative layout for those compound views.

I've been stuck with this problem for a while, and the solution that worked for me was placing 2 views with match_parent, one inside the other.
If I did this on my list item's layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#F00"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
</RelativeLayout>
although it is a relative layout as others have mentioned, and I am passing the parent view but false in the inflater, it would simply not show up at all (red background to check).
But, when I did this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:background="#0F0"
android:layout_height="match_parent"/>
</RelativeLayout>
The green layout showed up and took up the whole space.
So just having a child view inside the main one with match_parent solves the problem, no idea why.

Can't see your full code, but can guess, that some of the views inside your LinearLayout are 'wrap_content'. You need to make one or some of them expand to the full width by using 'android:layout_weight="1"'
update:
you have a lot of redundant layout_weight's. Make them all 'wrap_content' and for only one of them add layout_weight=1 - for the last CustomTextView. This way, it will occupy all the blank space.

Related

Inflating ConstraintLayout in another ConstraintLayout

I want to put a ConstraintLayout programmaticly in another ConstraintLayout. But it does not show up, if I inflate it the way I did before with a RelativeLayout.
Here's the code inner file inner_area.xml:
<android.support.constraint.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:id="#+id/innerArea"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/grade_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/onegreen"
app:layout_constraintBottom_toTopOf="#+id/h1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/v1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_conversion_absoluteHeight="20dp"
tools:layout_conversion_absoluteWidth="30dp"
tools:layout_conversion_absoluteX="0dp"
tools:layout_conversion_absoluteY="0dp" />
<ImageView
android:id="#+id/grade_three"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/threelila"
app:layout_constraintBottom_toTopOf="#+id/h2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/v1"
app:layout_constraintTop_toTopOf="#+id/h1"
app:layout_constraintVertical_bias="0.0"
tools:layout_conversion_absoluteHeight="20dp"
tools:layout_conversion_absoluteWidth="30dp"
tools:layout_conversion_absoluteX="0dp"
tools:layout_conversion_absoluteY="20dp" />
.....
</android.support.constraint.ConstraintLayout>
Outer LayoutFile parent.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:orientation="vertical"
android:id="#+id/parentView"
android:background="#color/primary">
</android.support.constraint.ConstraintLayout>
Javacode:
ViewGroup parentView = rootView.findViewById(R.id.parentView);
final ConstraintLayout innerArea = (ConstraintLayout) inflater.inflate(R.layout.inner_area, container, false);
parentView.addView(innerArea);
But when I change in the inner.xml the layout_width or layout_height of the innerArea to wrap_content or match_parent, nothing happens. But when I change to 2000dp the inner layout shows up.
What am I doing wrong or missing?
Thanks to Cheticamp for pointing me in the right direction. I was inflating the ConstraintLayout with the root layout and not with the Constraint Layout it is supposed to be in. So the correct Javacode is:
parentView = rootView.findViewById(R.id.parentView);
ViewGroup parentView = rootView.findViewById(R.id.parentView);
final ConstraintLayout innerArea = (ConstraintLayout)
inflater.inflate(R.layout.inner_area, parentView, false);
parentView.addView(innerArea);
I seem to be very late to the party but I tried lots of things to solve this problem but nothing worked.
I ended up just putting the inner constraint layout inside another layout (a relative layout in my case) which solved the problem for me.

Increase the height of recyclerview dynamically on item add

Display pic
In my app, i want to increase the height of recycler view dynamically. As i add new item under complaints, the height of recycler view should increase. Is there a way to do it? Please help me with this. Thanks in advance.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/complaint_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background_line"
android:padding="#dimen/layout_margin"
android:text="Complaints"
android:textColor="#color/textColorFullBlack" />
<LinearLayout
android:id="#+id/complaints_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="#dimen/layout_margin"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="#+id/complaint_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/layout_margin"
android:divider="#color/toolBarBackground"
android:groupIndicator="#android:color/transparent" />
</LinearLayout>
<TextView
android:id="#+id/instructions_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background_line"
android:padding="#dimen/layout_margin"
android:text="Instructions"
android:textColor="#color/textColorFullBlack" />
<FrameLayout
android:id="#+id/instructions_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="#dimen/layout_margin"
android:layout_weight="1">
<android.support.v7.widget.RecyclerView
android:id="#+id/instructions_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/layout_margin"
android:divider="#color/toolBarBackground"
android:groupIndicator="#android:color/transparent" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorPrimary"
android:orientation="horizontal">
<in.palmpower.videocon.uicommon.widget.EditText
android:id="#+id/search_text_field"
style="#style/FormEditText"
android:layout_width="0dp"
android:layout_gravity="center_vertical"
android:layout_margin="#dimen/one_dp"
android:layout_weight="1"
android:background="#android:color/white"
android:inputType="textFilter"
android:padding="#dimen/search_edit_text_padding"
android:privateImeOptions="nm" />
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="?attr/selectableItemBackground"
android:contentDescription="#string/edittext_hint_search"
android:padding="#dimen/button_padding"
android:text="#string/add_button_string"
android:textColor="#color/textColorWhite" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Got stuck on same issue, i was using RecyclerView with wrap_content inside LinearLayout, i change LinearLayout to RelativeLayout and it set RecyclerView Height Based On Children.
This will work for you
RecyclerView mRecyclerView
= (RecyclerView) findViewById(R.id.complaint_recycler_view);
int originalItemSize = iteList.size();
ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();
if (itemList.size() > originalItemSize ){
params.height = params.height + 100;
originalItemSize = itemList.size();
mRecyclerView.setLayoutParams(params);
}
i suggest you to put teh recyclerview in any other layout (Relative layout is preferable). Then change recyclerview's height/width as match parent to that layout and set teh parent layout's height/width as wrap content. it works for me
WRAP_CONTENT will not work if you want recyclerView to take part of screen.
If you want other content to be there below recyclerView, you can put recyclerView inside LinearLayout and use the following code to change the height dynamically.
I assume you are using a list(complaint_list) for storing complaints.Adjust the complaint_height accordingly
ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();
int complaint_height=400;
if (complaints_list.size() > 2) {
params.height = ((complaint_list.size() - 1) * 100) + complaint_height;
} else {
params.height = complaint_height+100;
}
mRecyclerView.setLayoutParams(params);

How to make template xml

I want to make XML template to reuse the code. The problem is that one of the template has several linearlayouts. However, when I run the app, it only shows one linearLayout. (I can only see id: manage_services. not manage_view neither manage_shows).
Here is my code. I hope anyone can figure out the problem.
drawable_manage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/manage_services"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_services"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
<View
android:id="#+id/manage_view"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/menu_divider" />
<LinearLayout
android:id="#+id/manage_shows"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/slide_menu_header" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:drawableLeft="#drawable/menu_settings"
android:text="#string/title_manage_shows"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
MainMenuActivity.java
private void addPanels(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
android.support.v4.widget.DrawerLayout parent = (android.support.v4.widget.DrawerLayout) inflater.inflate(R.layout.activity_main_menu,null);
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
leftLinearLayout.addView(layout_names);
leftLinearLayout.addView(layout_emails);
leftLinearLayout.addView(layout_manage);
setContentView(parent);
}
Change your parent LinearLayout's orientation from horizontal to vertical.
android:orientation="vertical"
Are you wanting to reuse your drawable_manage.xml linearlayouts in these (i.e. #+id/manage_services, #+id/manage_shows) or do you have separate layout files?
View layout_manage = inflater.inflate(R.layout.drawable_layout_manage, null);
View layout_names = inflater.inflate(R.layout.drawable_layout_names, null);
View layout_emails = inflater.inflate(R.layout.drawable_layout_email, null);
LinearLayout leftLinearLayout = (LinearLayout)parent.findViewById(R.id.left_drawer);
Try as others suggested and change the orientation, and make sure you are trying to inflate the correct layout. Make sure you are inflating drawable_manage in the correct location and calling to the child linearlayouts by the correct ids.

Include same layout multiple times

I have a common layout (common.xml) which I want to include many times in another layout (layout_a.xml). But it only shows me just one time. Why?
common.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:drawable/alert_light_frame">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="#+id/imageView"
android:src="#drawable/test"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/imageView"
android:id="#+id/textView"
android:text="test"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" />
</RelativeLayout>
</merge>
layout_a.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/common" />
<include layout="#layout/common" />
</LinearLayout>
The ids when defined in XML must be unique. You are including two layouts that have views that contain the same id.
Here is how you would go about fixing it.
p.s. Unless there is more code that you are not including in your first layout file, that merge tag is useless.
As btse said, the ids in the XML must be unique.
It can be achieved in this way:
<include android:id="#+id/common1"
layout="#layout/common" />
<include android:id="#+id/common2"
layout="#layout/common" />
For information about how to access the elements inside those two included views, you can check out this blog post.
That's what I had done in my Project with Inflater.
test1is just a Layout made with a LinearLayout(Vertical) with text and a button and mainofferslayout in that case, the main layout with an image.
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_offers_display, container, false);
View inflatedLayout = inflater.inflate(R.layout.test1, (ViewGroup) view, false);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.mainofferslayout);
ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
I fixed by setting the layout_height of the RelativeLayout to 250dp since they are overlapped.

RelativeLayout and ViewStub inflation

I have the following layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/list_item_bottom">
<TextView android:id="#+id/list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub android:id="#+id/stub_for_alt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/list_item_name"
android:layout="#layout/text_view_alt_name"/>
<ImageView android:id="#+id/list_item_dopinfo1_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/stub_for_alt_name"
android:src="#drawable/ic_released"/>
</RelativeLayout>
I want ViewStub to be below TextView and ImageView to be below ViewStub.
Elements with ViewStub inflated are shown as I expect.
But elements without ViewStub have TextView overlapped with ImageView.
What's wrong with my layout?
UPDATE:
The only solution I've found is to give ViewStub and related TextView the same android:id value.
I know it's a bit of an old question but the trick in there is to set the inflatedId parameter to the same as the actual viewStub itself, like this:
<ViewStub android:id="#+id/stub_for_alt_name"
android:inflatedId="#id/stub_for_alt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/list_item_name"
android:layout="#layout/text_view_alt_name"/>
The only solution I've found is to give ViewStub and related TextView the same android:id value.
It's an old question, but I've got a useful addition
1) Yes, like others have posted - use same id and inflatedId:
<ViewStub
android:id="#+id/view_stub_id"
android:inflatedId="#id/view_stub_id"
...
/>
2) When you need to inflate view or refresh its content it's convenient to act like this:
View v = parentView.findViewById(R.id.view_stub_id);
if (v instanceof ViewStub)
v = ((ViewStub) v).inflate();
// here v is always the inflated view
// ...
Hi you can try this one.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/list_item_bottom">
//you can add another relative layout containing your textview and imageview
<RelativeLayout
android:id="#+id/layout_content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView android:id="#+id/list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView android:id="#+id/list_item_dopinfo1_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/stub_for_alt_name"
android:src="#drawable/ic_released"
android:layout_below="#+id/list_item_name" />
</RelativeLayout>
<ViewStub android:id="#+id/stub_for_alt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/list_item_name"
android:layout="#layout/text_view_alt_name"
android:layout_below="#+id/layout_content" />
</RelativeLayout>

Categories

Resources