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 8572237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:57:50+00:00 2026-06-11T18:57:50+00:00

I have a custom LinearLayout . Inside that layout I want to place a

  • 0

I have a custom LinearLayout. Inside that layout I want to place a couple widgets. I could create the widgets at runtime, but would rather load them from XML.

What I have below works, but I believe it is creating two LinearLayouts, one inside the other. I’d like to simply, in this example, create the Button and EditText inside the CustomLayout. Using XML, how can I do this?

Here is a bit more detail (edit: below this example, I’ve included a corrected version):

<com.example.test.MyActivity
  ... />
  <LinearLayout
    ... />
    <com.example.test.CustomLayout  ***** this is the custom linear layout
      android:id="@+id/custom"
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />
    <View
      ... other stuff ... />
</com.example.test.MyActivity>

And here’s the XML for the CustomLayout contents:

<LinearLayout
  ... />
  <Button
    ... />
  <EditText
    ... />
</LinearLayout>

And finally, the code for LinearLayout:

public class CustomLayout extends LinearLayout
{
  public CustomLayout (Context context, AttributeSet attrs)
  {
    super(context, attrs);
    Activity act = (Activity)context;
    LayoutInflater inflater = act.getLayoutInflater();
    View view = inflater.inflate (R.layout.custom_layout, this, false);
    addView (view);
  }
  ...
}

Corrected Version

<com.example.test.MyActivity
  ... />
  <LinearLayout
   android:id="@+id/outer_layout
   ... />

    ... other stuff ... 
  </LinearLayout>
</com.example.test.MyActivity>

And here’s the XML for the CustomLayout contents:

<merge
  ... />
  <Button
    ... />
  <EditText
    ... />
</merge>

The code to instantiate CustomLayout

public void addCustomLayout()
{
  LinearLayout outerLayout = (LinearLayout) findViewById (R.id.outer_layout);
  CustomLayout customLayout = new CustomLayout (getContext(), null);
  outerLayout.addView (customLayout, 0);
}

And finally, the code for CustomLayout:

public class CustomLayout extends LinearLayout
{
  public CustomLayout (Context context, AttributeSet attrs)
  {
    super(context, attrs);
    Activity act = (Activity)context;
    LayoutInflater inflater = act.getLayoutInflater();
    View view = inflater.inflate (R.layout.custom_layout, this, true);
  }
  ...
}
  • 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-06-11T18:57:51+00:00Added an answer on June 11, 2026 at 6:57 pm

    What I have below works, but I believe it is creating two
    LinearLayouts, one inside the other.

    Indeed you’ll have an extra LinearLayout(from the xml layout which contains the Button and the EditText) in CustomLayout. To avoid this you have at your disposal the merge tag. You’ll use it like this:

    <merge
      ... />
      <Button
        ... />
      <EditText
        ... />
    </merge>
    

    Then in your CustomLayout constructor:

    super(context, attrs);
    Activity act = (Activity)context;
    LayoutInflater inflater = act.getLayoutInflater();
    View view = inflater.inflate (R.layout.custom_layout, this, true); // addView is not needed anymore
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom component that I can place in my layout file (XML) for
I have a custom RelativeLayout and inside it i have a function that create
I have a layout that has a ViewPager inside of a custom ScrollView and
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have custom gallery. Gallery represents items that are frame layout. There are one
I have a Custom ListView that contains a vertical LinearLayout of horizontal LinearLayouts for
I have a custom notification that uses a LinearLayout that contains an ImageView and
I have a Tab that has a custom layout which contains a TextView .
I have made custom .xml layout for ListView. Now it looks: But it isn't
Okay so I have a custom view inside a dialog but its onKeyDown never

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.