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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:52:49+00:00 2026-05-26T08:52:49+00:00

basically I want to encapsulate a simple component from code that I already have.

  • 0

basically I want to encapsulate a simple component from code that I already have.

Basically it’s a LinearLayout with buttons inside. These buttons will make changes to a ListView, and there is also some other small stuff that it will do.

Currently I have a XML layout with those, and I programmatically setup everything else: the buttons, the interaction between the list and the other small stuff.

Obviously I thought to myself, let’s encapsulate this.

I started out trying to extend the LinearLayout and adding the buttons.
Already I have no idea how to inflate the buttons to add to the view
What method do I override to create this buttons just before the view gets created without messing with the measures and inflations, etc.

I’ve looked around but the custom components I see are either completely new components or components that simply add small functionality to the custom ones.

Is there some guidelines for doing this?
Good tutorials/examples?

Any help is appreciated. Thanks !

EDIT:

Okay, here is a little more specific stuff.

Basically I want to create a View that holds filter buttons for a ListView. This will be used in different places with different filters, so I need flexibility for the buttons.

Basically I’d like to do something like this:

CustomView view = new CustomView(activity);
view.addButton("Lala", new OnFilterClickListener { 
    onClick(ListView list, View v) {
      // Do the filtering
    }
});

mListView.addHeaderView(view);

I want the view to adapt it’s weights for showing the buttons, show the user which filter is active, stuff like that.

But I still don’t really know how to make those dynamically added buttons appear, where do I generate them, how to inflate them and stuff like that.

  • 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-26T08:52:49+00:00Added an answer on May 26, 2026 at 8:52 am
    public class myLayout extends LinearLayout {
    
        //...
    
        public void addButton(String text, OnClickListener listener) {
            Button newButton = new Button(mContext);
            newButton.setText(text);
            newButton.setOnClickListener(listener);
            //Say we want the weights to be equal
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
            addView(newButton, params);
        }
    
        //...
    
    }
    

    You can even do something to the view before dispatching the click like this:

    public class myLayout extends LinearLayout {
    
        //...
    
        public void addButton(String text, final OnClickListener listener) {
            Button newButton = new Button(mContext);
            newButton.setText(text);
            newButton.setOnClickListener(new OnClickListener(){
                public void onClick(View v) {
                    //do whatever you want
                    //like change background of button or something
                    //finally
                    listener.onClick(v);
                }
            });
            //Say we want the weights to be equal
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
            addView(newButton, params);
        }
    
        //...
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
So I basically want to have a static method in my AR class to
What i basically want to do is to get content from a website and
I have a 2-dimensional array of objects and I basically want to databind each
I have an SPListItemCollection. I basically want to get one of the items (randomly)
I basically want to show some php if it's inside the category work, I
I am in a situation where I basically want to be able to have
I have an iframe setup within a page and basically want to know whether
I basically want to produce the following: from int 67 to 1 minute 7
I basically want to take two images taken from the camera on the iPhone

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.