Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Why is the TextView shown as having a height?

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");
    }   
}
like image 418
RichardG Avatar asked Nov 16 '11 23:11

RichardG


People also ask

What is line height in TextView?

Line height usually means text size + "padding" top/bottom. So, if your designer write line height 19sp and text size 15sp, it means you need to have extra padding 4sp. 19sp - 15sp = 4sp. To implement it in your layout, use lineSpacingExtra attribute.

How do you fit text in TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

What is the difference between TextView and plain text in Android Studio?

Textview : should be used for uneditable text which user wants to read but not to manipulate. e.g. News, Articles, Blogs. Plain text/ Edittext : should be used for taking user input in alphanumeric form. e.g. UserId, Comments.

How do I reduce line spacing in TextView?

How do I reduce line spacing in TextView? Just we need to add an attribute with your TextView that give the spacing with lines. These are –android:lineSpacingExtra, android:lineSpacingMultiplier – use these two attributes with your TextView in XML file. or Programatically call setter method of that attribute textView.


1 Answers

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

like image 197
Vit Khudenko Avatar answered Nov 12 '22 18:11

Vit Khudenko