Android Shape Rectangle with 2 colors - java

I need to make a custom rectangle that should use hex colors from #bff54a up to #88c010.
Actually I have this xml but I can only use one of those colors. How can I make the gradient possible? I already searched and I didn't found anything like this.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#color/gray_light" />
<solid android:color="#88c010" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
Thanks to all!

Use Gradient as the color of the Drawable :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#color/gray_light" />
<gradient
android:type="linear"
android:centerX="0"
android:centerY="1"
android:startColor="#bff54a"
android:endColor="#88c010" />
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
</shape>
See Drawable Resources

You can create file shape and implement this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangule"> <!-- oval -->
<gradient
android:startColor="#388E3C"
android:endColor="#448AFF"
android:centerColor="#color/colorPrimary"
/>
</shape>
</item>
</selector>
Result

Related

Java .xml creating a drawable button background

New to programming, 3 weeks beginner.
I am making a button with stacked rectangles through drawable xml.
This is my button .xml code, very straight forward:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D5A04F"
tools:context=".MainActivity">
<android.support.design.widget.NavigationView
<Button
android:id="#+id/leftnavbar"
android:layout_width="212dp"
android:layout_height="279dp"
android:layout_marginTop="16sp"
android:layout_marginBottom="500sp"
android:background="#drawable/rounded_rectangle"
/>
</android.support.design.widget.NavigationView>
</android.support.constraint.ConstraintLayout>
and this is my drawable .xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="#dimen/mr_dialog_fixed_width_minor"
android:paddingTop="#dimen/mr_dialog_fixed_width_major"
>
<item
android:top="5sp"
android:left="5sp"
android:right="5sp"
android:bottom="50sp">
<shape android:shape="rectangle">
<solid android:color="#D3AE71" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="30sp"
android:left="30sp"
android:right="30sp"
android:bottom="600sp">s
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="160sp"
android:left="30sp"
android:right="30sp"
android:bottom="470sp">
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="290sp"
android:left="30sp"
android:right="30sp"
android:bottom="340sp">
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="420sp"
android:left="30sp"
android:right="30sp"
android:bottom="210sp"
>
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
<item
android:top="550sp"
android:left="30sp"
android:right="30sp"
android:bottom="80sp">
<shape android:shape="rectangle">
<solid android:color="#E6E5E5" />
<corners android:radius="20sp"/>
</shape>
</item>
</layer-list>
The only item that is shown in drawable is the first, the rest of the rectangles fail to show.
Suggestions or pointers to the reason will be much apreciated. thank you for your time and attention...
First of all please do not use "sp" for things other than text font size.
Second, pls look into excessive size of your android:bottom, like 600sp for example - they are leaving no space for respective shapes.
Your button's height is too short,chang android:layout_height="279dp" to android:layout_height="700dp"

How to customize TabLayout tabs separately?

This is my TabLayout xml code:
<android.support.design.widget.TabLayout
app:tabBackground="#drawable/profile_tab_selector"
android:id="#+id/profileTabLayout"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorPrimary"
app:tabTextColor="#color/white">
</android.support.design.widget.TabLayout>
And this one is for tabBackground="#drawable/profile_tab_selector":
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape>
<stroke android:width="1dp" android:color="#color/white"/>
<solid android:color="#color/white"/>
</shape>
</item>
<item>
<shape>
<stroke android:width="1dp" android:color="#color/white"/>
<solid android:color="#color/blue"/>
</shape>
</item>
</selector>
I want to design it like this, with rounded cornenrs:
The problem is that in selector I can not detect the first and the last tabs to modify them. And this is the result:
You have given the shape in the drawable file , but you didn't give corner radius.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
<stroke
android:width="10dp"
android:color="#android:color/transparent"></stroke>
<solid android:color="#23cf5a" />
</shape>
And you can design something like this too
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="16dp"
android:bottomRightRadius="16dp"
android:radius="32dp"
android:topLeftRadius="16dp"
android:topRightRadius="16dp" />
<solid android:color="#color/tab_color" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
<size
android:height="32dp" />
</shape>
Your Solution is here
Don't Use : app:tabBackground="#drawable/profile_tab_selector"
Use : android:background="#drawable/profile_tab_selector"
You also try it.

why doesn't drawable apears on Button?

I am a newbie in Java-Android developing.
I want to add this drawable on a view like button's background
but it doesn't draw anything.
This is my background drawable_shining.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<gradient
android:endColor="#000"
android:centerColor="#111"
android:startColor="#000"
android:angle="45"
android:type="linear" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#000"
android:gradientRadius="100"
android:startColor="#555"
android:type="radial" />
</shape>
</item>
</layer-list>
[here is code and layout][1]
according to documentation layer-list is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Each drawable is represented by an element inside a single element.
in your code, write each <shape> in <item> tag. Your final code will be look like this,
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape>
<gradient
android:endColor="#000"
android:gradientRadius="100"
android:startColor="#333"
android:type="radial" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#000"
android:gradientRadius="100"
android:startColor="#333"
android:type="radial" />
</shape>
</item>
You can use same format like below code :
Reference : angrytools
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#47A891"
android:startColor="#E8E8E8"
android:endColor="#000000"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#878787"
/>
</shape>

How to add both png file and border in android button?

Here, for the mixed button I have used
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/stripes"
android:tileMode="repeat"
android:dither="true"
android:antialias="true" />
</item>
</layer-list>
And for other buttons I have used
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
Now I want to add that black border around the mixed button how to do?
Use shape in layer list under item in xml file like below
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/stripes"
android:tileMode="repeat"
android:dither="true"
android:antialias="true" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</item>
</layer-list>
It'll help you..
reference How overlay bitmap and shape in layer-list properly
You are super close, you just need to add the Shape (for your border) to the <layer-list>
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/stripes"
android:tileMode="repeat"
android:dither="true"
android:antialias="true" />
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
</item>
</layer-list>

Create own styles in Android

Every time I try to customize the style of a view I have the same problem.
What parameters can I change; and how to override the system defaults? For example:
How to change the highlighted color of a element of a ListView when clicked
How to change the default frame of a DialogFragment? I mean the frame that is removed with the Style DialogFragment.STYLE_NO_FRAME.
Where can I check what are the values I should play with for achieve my custom layouts?
Thanks
Well you can change the look by creating a new xml file in the drawable folder but dont make a layout xml jus pick andrid xml from the wizard and it will give u a list of components you can modify including colour.
<?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="#android:color/black" />
</shape>
</item>
<item android:top="1dp" android:left="2dp" android:right="2dp" android:bottom="25dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/holo_purple" />
</shape>
</item>
<item android:top="1dp" android:left="400dp" android:right="2dp" android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/holo_purple" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</item>
<item android:top="1dp" android:left="2dp" android:right="400dp" android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/holo_purple" />
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp" android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
</item>
</layer-list>
then you can just change whats required!
But provider some code and i will be able to answer better..

Categories

Resources