sirmagid
sirmagid

Reputation: 1130

android custom view onmeasure TextView

I tried to do custom component. I extended View class and do some TextView in method override onMeasure? I've seen couple tutorials, but each one is a little been different. Sometimes they call super.onMeasure at the end, sometimes they use setMeasuredDimension and didn't call it. Where is a difference image and textview in onMeasure size ?

my cod for onmeasure ImageView for SquareImageView

public class SquareImageView extends ImageView {
    public SquareImageView(Context context) {
       super(context);
    }

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

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

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
       //Snap to width
   }
}

How can I set dynamic height in TextView?

    <com.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"/>

Upvotes: 0

Views: 1204

Answers (2)

Johnny
Johnny

Reputation: 282

Edit

 super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());

To

super.onMeasure(widthMeasureSpec, widthMeasureSpec);

Upvotes: 1

vjsantojaca
vjsantojaca

Reputation: 325

This's a ImageView or TextView?

When you create a Object in your fragment or activity, you can put width, weight and height as you want.

Something like that:

SquareImageView squareImageView = findViewById(R.id.picture);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
squareImageView.setLayaoutParams(layoutParams);

Upvotes: 0

Related Questions