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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:22:09+00:00 2026-05-23T15:22:09+00:00

Trying to create a custom component that gets it’s layout from an XML file

  • 0

Trying to create a custom component that gets it’s layout from an XML file (box.xml). I have gone through a couple of tutorials but just can’t seem to get anything to display. Below is the constructors for my custom component and the code does get executed without errors.

public class MyView extends LinearLayout {
    //Constructor required for inflation from resource file
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);        
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate (R.layout.box, this);                        
        Log.d("CONSTRUCTOR 2", "TESTER");        
    }
}

I add the component to a layout with this:

<com.mysample.MyView android:layout_width="50dp" android:layout_height="38dp" android:background="#FF000000" />

The black block does appear on the screen but not with the layout of the xml file I inflate it with.

  • 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-23T15:22:10+00:00Added an answer on May 23, 2026 at 3:22 pm

    You will need to add the created view to the linear layout.

        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            View view = inflate(context, R.layout.box, null);
            addView(view);
        }
    

    This should work. Do you see the Log in your Logcat by the way?

    Here is the box.xml (Sample one). You need to use the fully qualified class name of the MyView wherever you want to use it.

    <?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="fill_parent">
    <com.beanie.samples.drawing.MyView
    android:id="@+id/whiteboardView1" android:layout_width="fill_parent"
    android:layout_height="fill_parent"></com.beanie.samples.drawing.MyView>
    </LinearLayout>
    

    Check this sample project here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create with Delphi a component inherited from TLabel, with some custom
I am trying to create a custom component in flex that consists of a
I am trying to create custom resolver in my ivysettings.xml file: <ivysettings> <settings defaultResolver=default/>
I'm trying to create a custom JSP tag that would take an array object
I am trying to create a custom accordion for my page to that display
I'm trying to create a custom control - a button - which will have
I've been trying to create a custom control that works exactly like the Panel
I have a custom content type with custom fields. I'm trying to create a
I'm trying to create a custom tab navigator that has to look like this
I'm trying to create a custom registration component for Joomla, and I was wondering

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.