Search code examples
androidandroid-studioandroid-fragmentsstyles

What can i make a colored background in my Fragment for my Android App?


I'm starting to make my first Android-Apps and I have got some trouble to do basic stuff :-) . I would like to make a fragment, that holds the a screen with a headline and some long text. Additionally the background should be colored.

So i simply put a textform for the header, a scrollpane and a text inside the scrollpane as shown below. The problem is, that the background is only colored in the middle of the screen. On the top and on the end the color is white

Here is the Layout for my fragment:

<?xml version="1.0" encoding="utf-8"?>

    <androidx.constraintlayout.widget.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:layout_height="match_parent"
        android:background="@color/buttonBackground">


        <TextView
        android:id="@+id/about_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
            android:fontFamily="@font/damion"
        android:text="@string/about"
        android:textAlignment="center"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="55sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/about_text">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="80dp"
            android:text="@string/content"
            android:textColor="@color/colorPrimaryDark"
            app:layout_constraintBottom_toBottomOf="@+id/imageView"
            app:layout_constraintEnd_toEndOf="@+id/imageView"
            app:layout_constraintStart_toStartOf="@+id/imageView"
            app:layout_constraintTop_toBottomOf="@+id/about_text"
            app:layout_constraintVertical_bias="0.0" />
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

The activity_main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    tools:context=".MainActivity"
    >

    <fragment
        android:id="@+id/myNavHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation"
        />
</LinearLayout>

The Screen looks like this: Image of the Screen

Sorry for the beginners question :-). Thanks for your help


Solution

  • In the linear layout change android:layout_height="wrap_content" to android:layout_height="match_parent"