Inflating a xml layout in a custom View class

You can’t add views to the View class instead you should use ViewGroup or one of its subclasses(like Linearlayout, RelativeLayout etc). Then your code will be like this:

    public class View1 extends LinearLayout {

        View view;
        String[] countries = new String[] {"India", "USA", "Canada"};

        public View1( Context context) {
            super(context);
            inflate(context, R.layout.test2, this);
        }
    }

Leave a Comment