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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:51:11+00:00 2026-06-02T21:51:11+00:00

I created a compound component Box which I want to add to the layout.

  • 0

I created a compound component Box which I want to add to the layout. Box xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutForBlock"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:background="@drawable/screen_background" android:layout_marginLeft="5dp" android:layout_marginTop="5dp">
        <ImageButton
            android:id="@+id/imageButtonContent"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@drawable/beach_bed" android:background="@drawable/buttonbackground" android:clickable="true" android:layout_margin="5dp" android:contentDescription="@string/sample_text"/>
        <TextView
            android:id="@+id/textViewContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/sample_text"
            android:textColor="@color/deep_blue" android:layout_margin="5dp"/>
    </LinearLayout>
</merge>

Box class:

public class Box extends LinearLayout  {

    private TextView textContent;
    private ImageView imageContent;

    public Box(Context context, AttributeSet attrs) {
        super(context, attrs);
        ((Activity)getContext()).getLayoutInflater().inflate(R.layout.box, this);
        setupViewItems();
    }

    private void setupViewItems() {
        textContent = (TextView) findViewById(R.id.textViewContent);
        imageContent = (ImageView) findViewById(R.id.imageButtonContent);
    }

    public void setTextContent(String text) {
        this.textContent.setText(text);
    }
    public void setImageContent(String tag) {
        this.imageContent.setContentDescription(tag);
    }
}

Everything works if I add the Box to the main xml file like:

<com.mypackage.alexey.Box
android:id="@+id/mybox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/> 

The problem is that I’d like to create many of boxes programmatically something like this: Box mybox= new Box();. How to do it?

  • 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-02T21:51:14+00:00Added an answer on June 2, 2026 at 9:51 pm

    I would suggest you also implement the constructors of the LinearLayout that takes only a Context:

    public Box(Context context) {
        super(context);
    }
    

    Then in your Activity, when you want to add a new Box, instantiate the Box class and add it to your layout:

    // I assumed you have a LinearLayout to which you want to add your Box
    LinearLayout parent = (LinearLayout) findViewById(R.id.parent_id);
    //create the Box and added to the parent above
    Box theBox = new Box(this); // if you are in an Activity
    //some LayoutParams to replicate your xml attributes
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    // set the Orientation for the Box
    theBox.setOrientation(LinearLayout.HORIZONTAL);
    //add the Box
    parent.addView(theBox);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a component that allows us to have compound keyboard shortcuts
I'm trying to create a custom Android compound view, here is the code: <?xml
I created a compound control using xml, and am able to re-use it by
I've created a compound unique index on my table: CREATE TABLE [dbo].[SearchIndexWord]( [ID] [int]
I am using Backbone.js to create compound form fields which hold a telephone number
I have to create a custom compound button bar control in Android like the
While researching how to create custom compound views in Android, I have come across
I would like to create a compound component that consists of a TextView (integer
I have created a compound control that I am reusing in multiple activities. This
I'm trying to create a compound key as my _id field, which takes in

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.