Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView offset downwards

I have an activity with two Buttons and a TextView in a LinearLayout. My TextView is offset downwards and the text doesn't fit inside the box. Can you explain what is happening? I think it is related to padding, and I've read several discussions about the perils of TextView padding, but that doesn't explain why the text is cut off at the bottom.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#800080">

    <Button
        android:text="This"
        android:background="@drawable/button_red" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <Button
        android:text="That"
        android:background="@drawable/button_green" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
    <TextView 
        android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons.  Why it does this I can't imagine.  I only hope someone can help me with this." 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#533f93"
    />
</LinearLayout>

This code produces this display:

User interface

The purple is the LinearLayout, the blue is the TextView. As you can see, the TextView's top is below those of the buttons and its bottom is below the bottom of the LinearLayout. As I add text to the TextView, the LinearLayout increases its height appropriately, but because the TextView is offset, I always lose the bottom of the last line.

I ran Hierarchy Viewer and it gave me this wireframe:

Wireframe image from Hierarchy Viewer

Which shows the vertical offset at the top, but misses the bottom of the TextView. The same wireframe with the LinearLayout selected looks like this:

Wireframe image from Hierarchy Viewer - LinearLayout selected

According to Hierarchy Viewer, the top of the buttons is at 0, but the top of the TextView is at 7. I've tried various fixes, mostly culled from this site:

    android:paddingTop="0dp"
    android:background="@null"
    android:includeFontPadding="false"

None of these fixed my issue.

like image 758
TomDestry Avatar asked May 16 '12 03:05

TomDestry


People also ask

How to edit text in textview in Android?

A TextView is a complete text editor, however the basic class is configured to not allow editing. Following are the important attributes related to TextView control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.

How to create shadowed text view in Android?

Text Shadow Shadow for the text can also be given in Android. The attributes required for the shadowed text view are: android:shadowDx=”integer_value” -> which decides the distance of text from its shadow with respect to x axis, if the integer_value is positive the shadow is on positive of the x axis and vice versa.

How to remove white spaces for textview in Android?

This example demonstrates How to remove white spaces for textview in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

What does textview's input method do?

If set to true, specifies that this TextView has an input method. Font family (named by string) for the text. Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.


1 Answers

Set android:baselineAligned property of your LinearLayout to false.

From documentation:

When set to false, prevents the layout from aligning its children's baselines. This attribute is particularly useful when the children use different values for gravity. The default value is true.

like image 186
StenaviN Avatar answered Oct 02 '22 19:10

StenaviN