Search code examples
androidandroid-custom-view

Creating custom components using regular android components


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.


Solution

  • 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);
        }
    
        //...
    
    }