Saturday, February 21, 2015

Android Creating a CustomView from Layout XML file

This is easy as declaring the ViewGroup in the layout XML file and then creating a class and inflating this layout values.

The inflation code is like below.

ublic class FloatingView extends RelativeLayout {
    private ImageView launcherButtonImgView;
    private ImageView closeButtonImgView;

    public FloatingView(Context context) {
        super(context);
        init();
    }

    public FloatingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FloatingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.floating_view_layout, this);
        this.launcherButtonImgView = (ImageView)findViewById(R.id.thumbnail);
        this.closeButtonImgView = (ImageView)findViewById(R.id.thumbnail2);
    }
}



The entire source code is available at

https://drive.google.com/folderview?id=0B0ZgwdHsnw1bfllsWjZNZWcwTC13Vmk3T09tbHRSd1hUXzFST1Q1cFptRmk1ZjR5eWdvYk0&usp=sharing

References:
http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/
http://stackoverflow.com/questions/16022615/how-to-load-programmatically-a-layout-xml-file-in-android

No comments:

Post a Comment