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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:26:57+00:00 2026-06-04T15:26:57+00:00

This should be very easy to do, but somehow after some 15 minutes searching

  • 0

This should be very easy to do, but somehow after some 15 minutes searching I still can’t get the answer:

I want to make a custom Android View combining a TextView and a Button, plus some customized behaviors/methods, let’s say when I click the button it should change the TextView to “Hello, world!”.

I know that I’ll have to extends the View class, and design the layout in a XML, then do some magic to link the two. Could you tell me what the magic is? I know how to do this in an Activity, but not in a custom View.

EDITED
Ok, so I found out I need to use a Inflater to inflate my class with the child views defined in a layout. Here’s what I got:

public class MyView extends View  {

private TextView text;
private Button button;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    View.inflate(context, R.layout.myview, null);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    text = (TextView) findViewById(R.id.text);
    button = (Button) findViewById(R.id.button);
}
}

However, the text and button child views were null. Any idea? (The XML is very simple without any fancy edit, i just grabbed a TextView and a Button from the eclipse toolbar and throw in.)

  • 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-04T15:26:58+00:00Added an answer on June 4, 2026 at 3:26 pm

    Ok, so the answer to my own question is: (i) go get dinner, (ii) extends LinearLayout instead of View, this makes it a ViewGroup and thus can be passed into the inflate(...) method but don’t have to override the onLayout(...) method. The updated code would be:

    public class MyView extends LinearLayout  {
        private TextView text;
        private Button button;
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            View.inflate(context, R.layout.myview, this);
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            text = (TextView) findViewById(R.id.text);
            button = (Button) findViewById(R.id.button);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be very easy for you guys, but I still can't get it
This should be a very easy thing to do but I can't find a
This should be very easy but I don't know how to do it. I
This seems like it should be something very easy to do, but every time
This should be a very basic design question, but for some reason it doesn't
This should be simple, but I am still lost. There is a very similar
I think this should be a very easy one, but I cant find the
this should be a very easy thing to do, but it looks like it's
Ok this should be easy but I can NOT figure it out. I have
This sounds very easy, but i don't get the point. So what's the easiest

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.