Search code examples
androidcustom-widgets

Creating a custom widget with a group of existing widgets?


I've looked around on how to do this but I'm still not very sure, so I've decided to ask here.

I'm looking to create a list of widgets that will display artist names, genre, and the venue they will be playing at. Below is a picture of 4 widgets consisting of an imageView and 3 Plain textViews that I want to somehow group and create 1 custom widget, with all 4 components that I can call to, to display the relevant information.

I'm also looking to have this widget duplicate itself to a set limit, displaying different artists, genre, and venue one below the other.

I'm quite stuck on how to approach this, and any help would be great please! Thanks in advance!

Image:

http://puu.sh/hr9x4/b3bffdf263.jpg

edit: bump! any help would be appreciated!


Solution

  • Create a custom view.

    create a class Widget and extent it with linear layout. In the constructor inflate a layout containing your layout(image view and 3 text views).

    Add the inflated view to the class itself by calling addView

    write setter functions in which you will accept the object of event class that contains information related to event and that function simply set the values to the appropriate text view.

    Class Widget extends LinearLayout{
    private TextView artistName;
    public Widget(Context context) {
            super(context);
    
            View view =  LayoutInflater.from(getContext()).inflate(
                    R.layout.widget, null);
            artistName = view.findViewById(R.id.artistName);
            this.addView(view);
    
        }
    
    public void setName(Event event){
       artistName.setText(event.getArtistName());
    }
    }
    

    In your activity layout you can use it as

    <pkg_name.Widget android:id="@+id/widget1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </pkg_name.Widget>