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

  • Home
  • SEARCH
  • 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 1104157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:26:42+00:00 2026-05-17T01:26:42+00:00

I am having a class that extends View. I have another class that extends

  • 0

I am having a class that extends View. I have another class that extends activity and I want to add the first class to be loaded inside the activity class.
I tried the following code

package Test2.pack;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

public class Test2 extends Activity {
    /** Called when the activity is first created. */

    static view v;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        try{
            v = (view) View.inflate(Test2.this, R.layout.main2, null);
        }catch(Exception e){
            System.out.println(" ERR " + e.getMessage()+e.toString());
        }       
    }
}

class view extends View{
    public view(Context context) {
        super(context);     
    }   
}
  • 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-17T01:26:42+00:00Added an answer on May 17, 2026 at 1:26 am

    Ok tried this, and realized that it doesn’t work. The problem is, that the View class does not have methods for adding child views. Child views should only be added to ViewGroups. Layouts, such as LinearLayout, extend ViewGroup. So instead of extending View, you need to extend for example LinearLayout.

    Then, in your XML, reference to the layout with:

    <my.package.MyView
        android:id="@+id/CompId"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
    

    Then in your custom class, inflate and add:

    public class MyView extends LinearLayout {
    
        public MyView(Context context) {
            super(context);
            this.initComponent(context);
        }
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.initComponent(context);
        }
    
    
        private void initComponent(Context context) {
    
             LayoutInflater inflater = LayoutInflater.from(context);
             View v = inflater.inflate(R.layout.foobar, null, false);
             this.addView(v);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom view that extends LinearLayout, with these constructors: public class CustomView
I have following goal: From a list in main activity that extends ListActivity, I
I am having trouble positioning view elements on a custom ViewGroup that I have
I have a voice input class that works absolutely great. However, I want the
CommandBuilder is a very handy class that helps us avoid having to write INSERT,UPDATE
Recently I was having some issues with a singelton class that was lazy initializing
I'm curious about the possibility of having a .NET class library that provides a
Veterans please forgive me for asking silly question. I understand that a class having
motivation: I would like to create a utility class so that instead of having
I am having a class Student. I want to store the object of class

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.