Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:16:49+00:00 2026-05-25T14:16:49+00:00

Why do some views in a linearlayout within a scrollview fill the parent when

  • 0

Why do some views in a linearlayout within a scrollview fill the parent when fill_parent is set when inflated from xml directly but don’t when added via a custom component that inflates the same xml?

The result of inflating the same xml in two different places:

http://imgur.com/DF6kl

The top item of the two is as a result of inflating the xml in a class that extends linearLayout and then adding that class to the linearLayout inside the scroll view. This view isn’t filling the parent and I can’t work out why.

The bottom item is the result of inflating the xml directly in the activity that this is all contained within. It appears as I expected.

This is the code that adds the two views in the activity:

scrollList.addView(new CommunityTaskListItem(this, null));
scrollList.addView(View.inflate(getBaseContext(), R.layout.communitytask, null));

This is the code that inflates the activity within a custom component class “CommunityTaskListItem”:

View communityTask = View.inflate(context, R.layout.communitytask, null);
addView(communityTask);

I assume the xml is okay because it works fine when inflated directly in the activity. My question is why the fill_parent property seems to be lost when the xml is inflated in a custom component’s class?

For good measure, here is the xml being inflated: EDIT: I gave you the wrong one before!!! Here’s the right one:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
        <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
    </LinearLayout>

</LinearLayout>

I’d like to keep the custom component and not inflate the xml within my activity if I could.

EDIT: Code where CommunityTaskListItem is inflated:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommunityTaskListItem extends LinearLayout {

    TextView textViewCommunityTaskTimes;
    TextView textViewCommunityTaskDescription;

    public CommunityTaskListItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        View communityTask = View.inflate(context, R.layout.communitytask, null);
        addView(communityTask);

        textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
        textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);

    }
...

EDIT: All working now! Here’s the final code for anyone else incase they have a similar issue:
Contructor for the custom object:

    public CommunityTaskListItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOrientation(VERTICAL);
        addView(inflate(context, R.layout.communitytask, null));

        textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
        textViewCommunityTaskDescription =     (TextView)findViewById(R.id.textViewCommunityTaskDescription);
    }

Custom object xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
        <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
    </LinearLayout>

</LinearLayout>

To be honest I don’t know exactly what the mistake I was making was so if someone could make that clear I’d be very grateful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:16:50+00:00Added an answer on May 25, 2026 at 2:16 pm

    The first line should be:

    scrollList.addView(new CommunityTaskListItem(this, null)
        new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    

    You call inflate() inside CommunityTaskListItem class and inflated view becomes a child view of the CommunityTaskListItem view. But you don’t set width and height for this view so it doesn’t match its parent’s width.

    EDIT: I think your XML should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <packagename.CommunityTaskListItem
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
            <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
            <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
        </LinearLayout>
    
    </packagename.CommunityTaskListItem>
    

    where packagename is the name of the package of the CommunityTaskListItem class.

    In this case you should change the CommunityTaskListItem:

    public class CommunityTaskListItem extends LinearLayout {
    
        TextView textViewCommunityTaskTimes;
        TextView textViewCommunityTaskDescription;
    
        public CommunityTaskListItem(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onFinishInflate() {
            textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
            textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
        }
    
        // ....
    }
    

    This class cannot be created using its constructor. It can be only inflated.

    The second way is to inflate children in the constructor like you do. But the XML should differ:

    <?xml version="1.0" encoding="utf-8"?>
    <merge
      xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
            <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
            <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
        </LinearLayout>
    
    </merge>
    

    The CommunityTaskListItem will be:
    public class CommunityTaskListItem extends LinearLayout {

        TextView textViewCommunityTaskTimes;
        TextView textViewCommunityTaskDescription;
    
        public CommunityTaskListItem(Context context) {
            this(context, null);
        }
    
        public CommunityTaskListItem(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            setOrientation(VERTICAL);
            inflate(context, R.layout.communitytask);
    
            textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
            textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
        }
    
        // ....
    }
    

    This class can be created using its contructor and inflated from an XML. But if you want to inflate it, you should create a separate XML file. Let’s call it task.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <packagename.CommunityTaskListItem
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    

    And that’s how you can use it:

    scrollList.addView(new CommunityTaskListItem(this),
        new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    scrollList.addView(View.inflate(getBaseContext(), R.layout.task, null));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi friends i have set my layout thru TabLayout within LinearLayout but still my
Simple problem: With the XML layout shown below, I have some views wrapped in
I was using SimpleCursorAdapter with an xml file with some views defined in it:
I read some of the answers on here re: testing views and controllers, and
I'm looking for some Best Practices for automating the deployment of Stored Procedures/Views/Functions/Table changes
I use Template Toolkit to generate views of pages in Catalyst. To do some
I want to load some data from mysql into my cocoa application view before
Here is the xml file of my Android widget: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
I am attempting to pull some information from my tnsnames file using regex. I
I'm in the process of porting some code from Linux to Mac OS X.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.