5

I've got an odd problem here. Basically I have a TextView with no default set text. I would've expected it to have a height of 0 since it hsas no content but there seems to be a gap between the elements above and below it. If I set the height to 0 in the XML and then try and change it through Java code then it does not reset the height.

How do I set the height to be 0 if the content is blank but then allow it to be changed programmatically?

Here is the code that I have:

<TextView 
    android:gravity="center_horizontal|center_vertical"
    android:id="@+id/connectionStatus"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:textSize="18px"
    android:textStyle="bold">
</TextView>

and the Java code is this:

    private void getConnectionStatus()
{
    if (hasConnection() == true)
    {
        //do something
    }
    else
    {
        connectionStatus.setHeight(48);
        connectionStatus.setText("No Internet Access");
    }   
}
Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
RichardG
  • 445
  • 3
  • 8
  • 17

3 Answers3

7

Use visibility "gone" inside of the xml layout. Then in the Java code call connectionStatus.setVisibility(View.VISIBLE);

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
2

Components may still display themselves even if they don't have content. For example, the may display a border or their viewable area. In order to make it not show up at all you need to use setVisibility(View.GONE).

Derlin
  • 9,572
  • 2
  • 32
  • 53
heneryville
  • 2,853
  • 1
  • 23
  • 30
1

I've often wondered if this behaviour is intuitive. If you want a TextView that has no height when the text is empty you can make one:

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);
        // ConstraintLayout totally ignores the new measured height after non-empty text is set.
        // A second call to requestLayout appears to work around the problem :(
        requestLayout();
    }
}
alexbirkett
  • 2,604
  • 2
  • 26
  • 30