I often wondered if this behavior is intuitive. If you want the TextView not have height when the text is empty, you can do the following:
import android.content.Context; import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.AttributeSet; public class NoHeightWhenEmptyTextView extends android.support.v7.widget.AppCompatTextView { public NoHeightWhenEmptyTextView(Context context) { super(context); } public NoHeightWhenEmptyTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public NoHeightWhenEmptyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int newHeightMeasureSpec = heightMeasureSpec; if (TextUtils.isEmpty(getText())) { newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, newHeightMeasureSpec); } @Override public void setText(CharSequence text, BufferType type) { super.setText(text, type);
source share