Since i updated my device to android 10 im facing a weird problem with one of my background gradients.
I have a RelativeLayout with a gradient background drawable looking like this.
<RelativeLayout
android:id="#+id/layoutSpeedLegend"
android:layout_width="match_parent"
android:layout_height="18dp"
android:background="#drawable/background"/>
My background.xml is looking like this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:centerColor="#color/yellow"
android:endColor="#color/red"
android:startColor="#color/darkgreen"
android:centerX="50%"/>
<corners android:radius="15dp" />
</shape>
</item>
In the editors preview, everything looks fine
But on my real test device is looks like this.
Also adding android:angle="90" to my background.xml doesn't work.
Does anybody have an idea why this happens?
It's a bug reported here. And may not be resolved yet.
The workaround is set angle="0" as you mentioned in question's comment.
Related
I am trying to achieve this layout. The upper part is a bit dark and it decreases as we move down making it complete transparent. I tried a couple of gradient variations but didn't get the desired results. Does anybody have idea of how to achieve this. Is it gradient or shadow ?
Create a gradient file and use this code you can increase or decrease transparency as you want
layout-->new file-->layout resource file--> give any name
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:startColor="#B71A1A1A"
android:angle="270"
android:centerColor="#00FFFFFF"
android:endColor="#00FFFFFF"/>
</shape>
</item>
</selector>
I am trying to get a glow effect like the background for edittext
I tried doing it but the background is not as much effective as the image
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#68F30606"/>
<corners android:radius="10dp" />
</shape>
</item>
<item
android:left="1.7dp"
android:right="1.7dp"
android:top="1.7dp"
android:bottom="1.7dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="10dp" />
</shape>
</item>
</layer-list>
Can someone help me to solve this mystery?
Then you will need to use the old-technique(kind of) of 9-Patch Drawing. I used to do it too. Examples are scarce because they are big, but there is documentation.
Documentation: https://developer.android.com/studio/write/draw9patch.
Also, if this helps you can check this too: Outer glow in edittext
To use the 9-Patch images in Xml to this (remember this is after you have created the 9-Patch Images):
Reference the drawable with the name but don't include .9.png (auto-complete in eclipse will take care of this)
Make sure you only have 1 image under the main /drawable folder (not a version for each dpi folder)
The image must be specified using :background, not :src (this got me stuck for a while)
android:background="#drawable/splash_logo"
Make sure the image and layout that contains it are using:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Credit To: Creating & Using 9-patch images in Android
Also, check this website out, it contains a lot of useful examples that the documentation doesn't provide:
https://tekeye.uk/android/examples/ui/android-9-patch-image-files
I'm making a very basic test app in Android Studio. I am creating an xml file for a button pressed event, but for some reason it is not highlighting any keywords. I have updated android studio. I am at a loss for how to solve this, as it's a new process to me.
The message the lightbulb gives me is "Unexpected text found in layout file" when I try to define these attributes for the bt_pressed XML.
Make sure your attribute is nested properly in the tag. Like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/some_color" />
</shape>
(hint: look at the > character in your provided snippet)
Incorrectly placed ">" in line 2.
I made the rectangle below for a project recently and it worked just fine. Nest it in the in between your first shape tag.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<shape android:shape="rectangle">
<corners android:radius="3dip" />
<solid android:color="#color/colorTwo" />
</shape>
</shape>
Alternatively, don't nest anything and change your opening shape tag to this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
i am try to use shape for background the View but my app crashed.
my code is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="?attr/colorPrimary"
android:angle="270" />
</shape>
if replace ?attr/colorPrimary with correct color code like #9e9e9e
this code and shape word properly but when i use ?attr/ donot work and force close.how van i fix this problem?
Recently I've discovered that I can define shapes in an xml file. Until now I was using nine patch images to create custom buttons etc. When I tried with an xml defined shape it didn't worked. None of the shaps, also from stackoverflow which were provided as examples didn't show up. I've seen that someone has already had a similar problem, however eventually it worked on an actual device. Well in my case it didn't.
Here's the navigation_bar.xml I've tried:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
android:angle="270"/>
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="8dp" />
and then used in:
<TextView
android:id="#+id/main_navigation_bar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#drawable/navigation_bar"/>
The background stays as default/black, nothing is shown.
Am I missing something here? I've also looked at ApiDemos, official documentation and didn't find anything to solve my problem.
Is it better to stay with nine patch images?
Thanks for the replies.
When tweaking a little with code, I've noticed strange behaviour. My IDE is IntelliJ.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true"
android:shape="rectangle">
<gradient
android:startColor="#FF89B4EE"
android:endColor="#FF5791E6"
android:type="linear"
android:angle="90"/>
<size
android:height="fill_parent"
android:dfv="fill_parent" />
I made an intentional typo and code compiled. It looks like shape is being ignored or something. Anyone noticed anything similar?
did you try putting the size of the shape? maybe is a dumb question but works for me, i also write the shapes without size and without "shape rectangle"...
something like this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FF89B4EE"
android:endColor="#FF5791E6"
android:type="linear"
android:angle="90"/>
</shape>
maybe you can read here a full documentation of android xml shape elements.
cheers
Does your navigation_bar.xml have your xml version header at the top? That is:
<?xml version ="1.0" encoding="utf-8"?>
I'm sure you probably do; I don't know about IntelliJ, but Eclipse won't build without it. Never hurts to check the simple solutions, though. :)