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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:47:12+00:00 2026-06-12T22:47:12+00:00

I want to define a custom widget, which contains some other controls and has

  • 0

I want to define a custom widget, which contains some other controls and has own logic. I want to use XML file to define the UI, and the java code to define the logic. But I don’t know how to do it.

I followed this article Building mix-up custom Android component/widget using Java class and XML layout, but don’t a working one. Following is my code.

XML:

<com.example.MyView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 1"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="custom button 2"/>

</com.example.MyView>

Java code:

public class MyView extends LinearLayout {

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

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);  // **line 32**
    }
}

And in the main layout, I invoke it like this:

<com.example.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
</com.example.MyView>

Unfortunately, it throws an exception when running:

05-26 14:11:25.199: ERROR/AndroidRuntime(15799): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MyActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at com.example.MyView.onFinishInflate(MyView.java:32)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:631)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    ....
  • 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-12T22:47:14+00:00Added an answer on June 12, 2026 at 10:47 pm

    You can’t declare the custom view like you did. If you use your custom view in the main layout the onFinishInflate method will be called to construct the custom view and in that method the layout above will be called. The problem is that you already have a custom view reference in that layout(which will be inflated again when the LayoutInflater encounters the custom view tag) so you’ll get in a circular inflating situation.

    You probably want something like this:

    <merge xmlns:android="http://schemas.android.com/apk/res/android>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="custom button 1"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="custom button 2"/>
    
    </merge>
    

    so when your custom view will be inflated from a layout file it will append the content of the xml layout above. The onFinishInflate method will remain the same:

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);  // **line 32**
    }
    

    Edit: It appears that inflating a content layout in the onFinishInflate method triggers the method again(at least on older versions). You could either modify the layout to use a LinearLayout like this:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="custom button 1"/>
    
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="custom button 2"/>
    
    </LinearLayout>
    

    and then in the onFinishInflate method:

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View v = LayoutInflater.from(getContext()).inflate(R.layout.aaaaaaaaaaa, this, false); 
        addView(v);
    }
    

    This will introduce another useless element in the layout hierarchy so you might simply inflate the layout with merge tag in the constructors of the custom view:

    public MyView(Context context) {
        super(context);
        LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);
    }
    
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);
    }
    
    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(getContext()).inflate(R.layout.test_view, this);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ps1 file in which I want to define custom functions. Imagine
I want to create my own custom collection type. I define my collection as:
I use emacs in ubuntu 12.04. i want define some background or foreground in
I want to define a custom exception that has two special properties: Field and
I am writing my android application, which I want to define a custom URI
I want to define a custom file format that my application can recognize. The
I have a listView with custom objects defined by the xml-layout below. I want
When I define custom propertie in my MXML component, I also want to define
I have a html file, it has good colors in vim. But I want
I'm trying to define a custom layout for my report for which I'm using

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.