1

I'm a beginner on Android, but my question require a little bit knowledge in UI and not in programming. I have an xml code, i have their some views but for this question only two components are relevant. The component is a ScrollView "father" that has only one "son" that is a TextView. When setting padding in ScrollView, if the text inside the TextView is long, first line of TextView is a little bit truncated (although i am using padding). But on the other hand, when using margin_layout text is seen fine. I read a lot on the difference between margin and padding and from what i have understand setting padding inside father should give the same affect as setting margin inside son. I emphasize that i'm using only one son. Code with margin (where text is fine):

  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="8dp"
        android:fontFamily="@font/chewy"
        android:text="@string/title"
        android:textSize="36sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="8dp"
        app:cardCornerRadius="5dp"
        app:cardElevation="7dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        <TextView
            android:id="@+id/question_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="8dp"
            android:text="Hello There this is where we are going to add something..."
            android:textSize="18sp" />
        </ScrollView>
    </android.support.v7.widget.CardView>

    <Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorAccent"
        android:text="@string/true_title"
        app:layout_constraintStart_toEndOf="@+id/prev_button"
        app:layout_constraintTop_toBottomOf="@+id/cardView" />

    <Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@color/colorAccent"
        android:text="@string/false_title"
        app:layout_constraintEnd_toStartOf="@+id/next_button"
        app:layout_constraintStart_toEndOf="@+id/true_button"
        app:layout_constraintTop_toBottomOf="@+id/cardView" />

    <ImageButton
        android:id="@+id/prev_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorAccent"
        android:contentDescription="@string/previous_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        app:srcCompat="@android:drawable/ic_media_previous" />

    <ImageButton
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@color/colorAccent"
        android:contentDescription="@string/next_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        app:srcCompat="@android:drawable/ic_media_next" />

    <TextView
        android:id="@+id/counter_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:fontFamily="@font/chewy"
        android:text="@string/counter_questions"
        app:layout_constraintBottom_toTopOf="@+id/cardView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />


</android.support.constraint.ConstraintLayout>

Code with padding (where first line is truncated)

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="8dp"
        android:fontFamily="@font/chewy"
        android:text="@string/title"
        android:textSize="36sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="300dp"
        android:layout_height="80dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="8dp"
        app:cardCornerRadius="5dp"
        app:cardElevation="7dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="8dp"
            >
        <TextView
            android:id="@+id/question_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Hello There this is where we are going to add something..."
            android:textSize="18sp" />
        </ScrollView>
    </android.support.v7.widget.CardView>

    <Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorAccent"
        android:text="@string/true_title"
        app:layout_constraintStart_toEndOf="@+id/prev_button"
        app:layout_constraintTop_toBottomOf="@+id/cardView" />

    <Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@color/colorAccent"
        android:text="@string/false_title"
        app:layout_constraintEnd_toStartOf="@+id/next_button"
        app:layout_constraintStart_toEndOf="@+id/true_button"
        app:layout_constraintTop_toBottomOf="@+id/cardView" />

    <ImageButton
        android:id="@+id/prev_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorAccent"
        android:contentDescription="@string/previous_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        app:srcCompat="@android:drawable/ic_media_previous" />

    <ImageButton
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@color/colorAccent"
        android:contentDescription="@string/next_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        app:srcCompat="@android:drawable/ic_media_next" />

    <TextView
        android:id="@+id/counter_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:fontFamily="@font/chewy"
        android:text="@string/counter_questions"
        app:layout_constraintBottom_toTopOf="@+id/cardView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />


</android.support.constraint.ConstraintLayout>

In addition, putting padding inside CardLayout make thins to be even bad than when adding padding inside the ScrollView

Can someone explain me the reason?

DorVak
  • 297
  • 2
  • 9
  • Did i make something wrong or the level of android experts is low? – DorVak Jan 08 '20 at 22:31
  • @DorBak, why are you putting that `TextView` inside a `ScrollView`? It somehow looks like you don't need to. – theThapa Jan 09 '20 at 18:31
  • It's a trivia game :) , sometimes question(string) are too long and bigger from the TextView. Why should i do instead? – DorVak Jan 10 '20 at 11:21

1 Answers1

0

It seems that the difference related to ScrollView odd behavior. I fixed the issue by setting android:fillViewport="true", and removing padding and margin_layout

DorVak
  • 297
  • 2
  • 9