7

Is it possible to create a custom view such that it could be refer to by

<components.layouts.CustomView
    android:text="@string/sign_in_options" />

without explicitly stating the layout_width and layout_height in xml since this is already defined in the CustomView class as such

public class CustomView extends TextView {

    public CustomView(Context context) {
        super(context);
        setup();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setup();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setup();
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setup();
    }

    private void setup() {
        setLayoutParams(new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT));

        setBackgroundColor(getResources().getColor(R.color.background));
        setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
        setAllCaps(true);

        int i = (int)getResources().getDimension(R.dimen.inner_space);
        setPadding(i, i, i, i);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        ViewGroup.MarginLayoutParams margins = ViewGroup.MarginLayoutParams.class.cast(getLayoutParams());
        int h = (int)getResources().getDimension(R.dimen.horizontal_space);
        margins.setMargins(0, h, 0, h);
        setLayoutParams(margins);
    }
}

does anyone know if there's a way to do it?

DarkPhoton
  • 154
  • 2
  • 8

2 Answers2

1

try this:

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    measureChild(yourChild,parentWidthMeasureSpec,parentHeightMeasureSpec);
}
naser khsoravi
  • 473
  • 7
  • 16
  • doesn't seem to work, the exception I'm getting when executing (`java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.`) makes me think that there won't be an easy solution to this. – DarkPhoton Jul 01 '17 at 17:29
0

you can try something like below. I use this method to insert a view programmatically. U can do based upon your requirement.

View insertPhoto(String path) {
    Bitmap bm = ImageUtils.decodeSampledBitmapFromUri(path, 100, 100);

    // Main Layout(Linear)
    layout = new LinearLayout(getApplicationContext());
    layout.setLayoutParams(new ViewGroup.LayoutParams(170, ViewGroup.LayoutParams.MATCH_PARENT));
    layout.setGravity(Gravity.CENTER);
    layout.setOrientation(LinearLayout.VERTICAL);

    // Sub Layout(Relative)
    relativeLayout = new RelativeLayout(getApplicationContext());
    relativeLayout.setLayoutParams(new ViewGroup.LayoutParams(150, 150));

    // Photo (ImageView)
    RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    imgParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

    imageView = new ImageView(getApplicationContext());
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setBackground(getResources().getDrawable(R.drawable.img_bg));
    imageView.setImageBitmap(bm);
    imageView.setBackgroundColor(ImageUtils.getDominantColor(bm));
    //Log.e("", imageView.getId() + "     +++++");

    // Sync Icon (ImageView)
    RelativeLayout.LayoutParams iconParams = new RelativeLayout.LayoutParams(50, 50);
    iconParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    iconParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    syncIcon = new ImageView(getApplicationContext());

    //syncIcon.setTag(imageName);
    //Log.e("tag", syncIcon.getTag() + "    +++++");
    //syncIcon.setImageResource(R.drawable.ic_cloud_done);
    //syncIcon.setImageResource(R.drawable.ic_cloud_off);
    syncIcon.setImageResource(R.drawable.ic_cloud_queue);
    //syncIcon.setImageResource(R.drawable.ic_cloud_upload);
    //syncIcon.setImageResource(R.drawable.ic_sync);
    //syncIcon.setImageResource(R.drawable.ic_alert);
    //syncIcon.setImageResource(R.drawable.ic_action_tick);

    //imageView.setLayoutParams(imgParams);
    //syncIcon.setLayoutParams(iconParams);

    // Placing both ImageViews inside Relative Layout
    relativeLayout.addView(imageView, imgParams);
    relativeLayout.addView(syncIcon, iconParams);

    // Placing Relative Layout finally inside Linear Layout
    layout.addView(relativeLayout);

    return layout;
}
vss
  • 1,093
  • 1
  • 20
  • 33