Search code examples
androidxmlandroid-fragmentsmaster-detail

How do I add background gradients to a fragment.xml class


Adding background gradients is a very basic thing I am aware but I am being thrown "the Markup in the document following the root element must be well formed"

I am certain I am doing something that is very obvious to many of you but if I can be shown the error of my ways here I will be grateful.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/articlelist"
android:name="com.MSmith.test.ArticleListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ArticleListActivity"
tools:layout="@android:layout/list_content"/>


<style android:background="#A9A9A9"/>

Tried adding the android:background="" into the but as much as no errors were shown, the emulator/graphic showed no change.


Solution

  • Define a drawable like this:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <gradient
            android:angle="315"
            android:endColor="@color/mainMenuGradient_endColor"
            android:startColor="@color/mainMenuGradient_startColor" />
    
    </shape>
    

    and set this drawable as background in your fragments root layout, and you will have a gradient.

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/articlelist"
    android:name="com.MSmith.test.ArticleListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    tools:context=".ArticleListActivity"
    tools:layout="@android:layout/list_content"
    android:background="@drawable/yourdrawablehere"/> <----- this might work
    

    or add it directly in the xml-layout of the Fragment