Search code examples
androidcustom-componentseekbar

Add any android-view to custom view


I have a simple View inside CustomDialog:

public class ColorPickerDialog extends Dialog 
{
    private static class ColorPickerView extends View
    {
        ColorPickerView(Context c, int color) 
        {
             super(c);
             //...
        }
        @Override 
        protected void onDraw(Canvas canvas) {
            //...
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
        }
    }

    public ColorPickerDialog(Context context,
                             int initialColor) {
        super(context);
        mInitialColor = initialColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new ColorPickerView(getContext(), mInitialColor));
    }
}

How to add to this view SeekBar?


Solution

  • You could create a container class for your color picker and the seekbar.

    For instance, if you want to layout them vertically:

    private static class ColorPickerContainer extends LinearLayout {
    
        private ColorPickerView colorPicker;
        private SeekBar seekBar;
    
        public ColorPickerContainer(Context context, int initialColor) {
            super(context);
            setOrientation(LinearLayout.VERTICAL);
    
            colorPicker = new ColorPickerView(context, initialColor);
            addView(colorPicker);
    
            seekBar = new SeekBar(context);
            addView(seekBar);
        }
    
        public ColorPickerView getColorPicker() {
            return colorPicker;
        }
    
        public SeekBar getSeekBar() {
            return seekBar;
        }
    }