How can I add image from src attribute to gradient.xml file in Android?
How can I add image from src attribute to gradient.xml file in Android?
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>
you can create here image and than use in this selecter gradiend in layout file
– Ahsan Malik
Aug 31 at 6:09
1 Answer
1
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.
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 at 6:24
@santosh Can you post a screenshot of how you want it to be..?
– Ümañg ßürmån
Aug 31 at 6:30
i have posted once check
– santosh
Aug 31 at 6:31
You mean the second answer? @santosh
– Ümañg ßürmån
Aug 31 at 6:32
yes the second answer
– santosh
Aug 31 at 6:34
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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 at 6:07