I have the following drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#2F2F2F"
android:endColor="#5A5A5A"
android:angle="90"
android:dither="true"
/>
</shape>
Is there anyway to start the startColor at 50% or the endColor at 50%?
Are there any links that show me all of the attributes I can apply to a gradient?
These pages show all the attributes you can apply to a gradient.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html
To do something in the middle like you are asking, try experimenting with the android:centerColor attribute.
Are you talking about transparency? I'm fairly certain you can just use a 8-digit color code rather than a 6-digit, the first 2 of which constitute the alpha value. So #AARRGGBB, or something like #882F2F2F.
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 struck at this problem of creating the UI in android. I need a xml-based solution to this problem. I don't want to use any SVG or PNG image to achieve the solution. Is there any way to achieve this of view in android.
You should be using a <layer-list> element of xml to draw such a UI.
Following code will be used to create oval shape:
<shape android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#AAAAAA"></solid>
<size android:height="100dp"
android:width="60dp">
</size>
This leads to the following shape:
Once you know how to draw an oval shape using xml. Now next step for you is to learn how to use <layer-list> to achieve desired output. To learn using <layer-list> you can follow this tutorial.
By creating a new drawable file which will describe the look of the widget you want to draw,more or less.You can then set it as background for which widget you want to change the original look of. I would recommend starting with rectangle if you want to achieve the shape shown in your question. the drawable file will look something like this:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#000"/>
<corners android:radius="150dp"/> <!-- The value here should specify How much ovally you want shape be -->
</shape>
and then assigning the background of the widget to this drawable should do the trick, something like this:
<LinearLayout
android:background="#drawable/oval">
Hope this was helpful :D
I've been following this tutorial here Medium - Diagonal Cut View to get that diagonal view effect
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary" />
<item>
<bitmap
android:src="#drawable/bebe"
android:gravity="center"
android:alpha="0.1" />
</item>
<item android:top="260dp"
android:bottom="-100dp"
android:left="0dp"
android:right="-260dp">
<rotate
android:fromDegrees="-10"
android:pivotX="0%"
android:pivotY="100%">
<shape
android:shape="rectangle">
<solid
android:color="#android:color/white"/>
</shape>
</rotate>
</item>
</layer-list>
So far the code is the almost the same and the effect is achieved, but only works on Lollipop+, I have searched but cannot find how to have a color overlay on top of an drawable to achieve this same effect and all my tries has being in vain.
The drawable goes in the background property of a RelativeLayout. I have tried to make it in 2 separated ImageView, one for the background image and one for the color overlay, but that doesn't apply the diagonal style right.
How can one achieve this effect for pre-lollipop versions?
Drawable background = relativeLayout.getBackground();
background.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.SRC_IN);
you can also try SRC_ATOPor MULTIPLY depending on desired effect.
========= EDIT ========================
Ok, I think I now better understand what you are asking. It wasn't entirely clear at first.
You aren't asking about a color overlay per-say, or rather, that is not what your problem is. Your problem lies in your reliance on the alpha attribute.
Do this, I have reordered your elements, so that the colored shape goes on top of the image, and instead of making the image transparent, we make the colored shape's color have an specified alpha byte. You can change the color and alpha as you like.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:gravity="center"
android:src="#drawable/muse15fence_750"/>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#cc3F51B5"/>
</shape>
</item>
<item
android:bottom="-100dp"
android:left="0dp"
android:right="-260dp"
android:top="260dp">
<rotate
android:fromDegrees="-10"
android:pivotX="0%"
android:pivotY="100%">
<shape
android:shape="rectangle">
<solid
android:color="#android:color/white"/>
</shape>
</rotate>
</item>
</layer-list>
This is what it looks like in Jelly Bean.
I was wondering, what would be the best way to make a gradient background for a LinearLayout in java ( not xml ) ?
Any ideas?
Thanks!
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#006499"
android:endColor="#0093d7"
android:angle="90" />
</shape>
Set here startColor and endColor as your requirement and save this file in drawable folder
and in LinearLayout you can set this as setBackground="#drawable/your gradient filename"
Using java code you can do same thing using GradientDrawable
Besides xml you can also use GradientDrawable it has corresponding methods for all xml attributes
I try to use the GradientDrawable to set a gradient to some backgrounds and buttons.
Sadly the documentation is not very detailed.
What are the main attributes to configure the gradient? I understand start and endcolor but some of the other attributes could need some explanation.
At the moment I used images as the background for the buttons but a drawable defined in XML would be much nicer.
I try to get a look like this (It is a very light gradient): alt text http://janusz.de/~janusz/RedButton.png
use this xml as background to the imageview.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#7c0000" android:endColor="#A71C1C"/>
</shape>
thats it.
I will give the same answer as Praveen, but will try to explain the settings as well.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="#7c0000"
android:endColor="#A71C1C" />
</shape>
android:type
There are 3 types of gradients, the default and the one for this question is "linear". The other 2 are "radial" and "sweep".
android:angle
Counter-clockwise rotation of the gradient, where 0 is | start color --> end color | (horizontally).
android:startColor
Color the gradient starts from, start is defined by the rotation.
android:endColor
Color the gradient ends with, end is defined by the rotation.
android:centerColor
There can also be a color in between the start and end color, if desired.
Code
I originally found this question because I wanted to do it in code. Here is how to do it programmatically.
int startColor = 0xfff6ee19; // yellow
int endColor = 0xff115ede; // blue
GradientDrawable gradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
new int[] {startColor, endColor});
View myView = findViewById(R.id.my_view);
myView.setBackgroundDrawable(gradientDrawable);
The different orientations in the image at the top can be achieved by changing the Orientation in the constructor.
XML
As already answered, this is how you do it in xml.
my_gradient_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#f6ee19"
android:endColor="#115ede" />
</shape>
You set it to the background of some view. For example:
<View
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#drawable/my_gradient_drawable"/>
See also
How to make gradient background in android
GradientDrawable documentation