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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:59:18+00:00 2026-06-19T03:59:18+00:00

I have a custom view class extended from view and I want to use

  • 0

I have a custom view class extended from view and I want to use it in an activity but it doesn’t show anything. there are no errors it runs but black screen no output. My code is as follow:

public class MoveableMenuLayout extends View {


    public MoveableMenuLayout(Context context, AttributeSet attrs) {

        super(context, attrs);
        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        ll.addView(tv);

        this.invalidate();
        this.requestLayout();
    }

    public MoveableMenuLayout(Context context) {
        super(context);

        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        ll.addView(tv);

        this.invalidate();
        this.requestLayout();

    }
}

CALLING ACTIVITY

public class TestActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MoveableMenuLayout layout = new MoveableMenuLayout(this);   
        setContentView(layout);
    }
}

I have tried it by including it in xml layout but still no output. I am not sure what I am doing wrong. I dont want to change the style I want it to be a separate class, I know I can use xml layout and set it directly in Activity with setcontentview but I don’t want to do that way. Thanks

  • 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-19T03:59:19+00:00Added an answer on June 19, 2026 at 3:59 am

    Why would it- you have no data. You created a linear layout and text view, but they aren’t your children. So you have no data to draw (and haven’t overridden onDraw anyway).

    If you want to create a custom view that holds a set of other views, you need to derive from ViewGroup, not View. You also need to add the top level subview(s) as children, not just hold them as data members. You’re probably even better off deriving directly from LienarLayout or RelativeLayout, and inflating your children directly from xml.

    public class MoveableMenuLayout extends ViewGroup {
    
    
    public MoveableMenuLayout(Context context, AttributeSet attrs) {
    
        super(context, attrs);
        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        ll.addView(tv);
        addView(ll);
    }
    
    public MoveableMenuLayout(Context context) {
        super(context);
    
        LinearLayout ll = new LinearLayout(context);
        ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        ll.addView(tv);
        addView(ll);
    
    }
    

    }

    Will probably work. You can upgrade that to definitely by deriving from LinearLayout instead. And then you wouldn’t need to have an internal layout. Like this:

    public class MoveableMenuLayout extends LinearLayout {
    
    
    public MoveableMenuLayout(Context context, AttributeSet attrs) {
    
        super(context, attrs);
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        addView(tv);
    }
    
    public MoveableMenuLayout(Context context) {
        super(context);
    
        TextView tv = new TextView(context);
        tv.setText("TES_______________");   
        addView(tv);
    
    }
    

    }

    An even better way is to do it via xml

    public class MoveableMenuLayout extends LinearLayout {
    
    
    public MoveableMenuLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate( ourlayoutid,  this );
    }
    
    public MoveableMenuLayout(Context context) {
        super(context);
    
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate( ourlayoutid,  this );
    
    }
    

    }

    and then put the children you want in an xml file. In this case, the top level tag in the xml file should be a tag, not a layout

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

Sidebar

Related Questions

If I have a custom view controller class that I want to reuse but
I have an activity and an extended View class,that both use the same xml
I have a created custom table view cell class named CustomCell which inherits from
I have a custom view class called TextFieldView and it's initialized from a nib...
I have an extended from BaseAdapter class which used as a custom Adapter for
I have a custom view class (a canvas for drawing). When I want it
I have a custom view: public class Loading extends View { private long movieStart;
I have a custom-made view that extends the View class. I would like 2
I have a class for my custom view. One of methods is @Override public
I have a custom view (inherited from UIView ) in my app. The custom

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.