0

I am new to Android and I want to display image with gradient background. Is it possible to add an image into gradient.xml?

Below is the xml code for this.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <bitmap android:src="@drawable/bg"/>
    <shape>
        <gradient
            android:startColor="#631f68"
            android:endColor="#ff5555"
            android:angle="45"/>
    </shape>

</item>

2
  • As this is on UI, please attach a picture of what you would like to accomplish. Also maybe state your question clearly, because so far I read it as "is it possible to ..." and I would reply "try it out!" :)
    – Alessio
    Aug 31, 2018 at 6:07
  • you can create here image and than use in this selecter gradiend in layout file Aug 31, 2018 at 6:09

1 Answer 1

0

Solution:

In your main_activity.xml,

set the background image for your topmost layout: (Relative or Constraint whatever you are using)

Then take another layout and give height and width to match_parent and apply the below Gradient to the background of it.

Gradient file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="135"
        android:centerColor="@color/colorAccentSecondary"
        android:endColor="@color/colorPrimary"
        android:startColor="@color/colorAccent"
        android:type="linear" />

    <corners android:radius="0dp" />
</shape>

Hope it helps.

5
  • i tried it but still facing same issue not able to apply gradient to image. please look into my edited screenshot and code
    – santosh
    Aug 31, 2018 at 6:24
  • @santosh Can you post a screenshot of how you want it to be..? Aug 31, 2018 at 6:30
  • i have posted once check
    – santosh
    Aug 31, 2018 at 6:31
  • You mean the second answer? @santosh Aug 31, 2018 at 6:32
  • @santosh You want image like that screenshot? Aug 31, 2018 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.