I can't dig deep into how android implements its layer-list drawable. But I find it interesting and I can hardly know why this happens.
Here are some drawables:
the nine-patch xml
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/cam2tst_ripple_bg_img">
</nine-patch>
the shape xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding
android:left="#dimen/cam2tst_ripple_horizontal_padding"
android:top="#dimen/cam2tst_ripple_vertical_padding"
android:right="#dimen/cam2tst_ripple_horizontal_padding"
android:bottom="#dimen/cam2tst_ripple_vertical_padding" />
<solid android:color="#android:color/holo_green_dark" />
</shape>
the ripple xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#android:color/holo_green_light">
<item android:drawable="#drawable/cam2tst_ripple_shape"></item>
</ripple>
the layer-list containing all above
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/cam2tst_ripple_bg_img" />
<item android:drawable="#drawable/cam2tst_ripple_base" />
</layer-list>
Unfortainately I still can't get my screenshot thing work on my L preview, but I can describe it.
What I get is the shape (which i obviously didn't set its size explicitly) doesn't cover over the whole nine-patch! The un-streched part of the nine-patch is magically considered as some kind of "auto padding thing". What I was expecting (ok I was expecting exactly what android has done for me, I mean what I was... supposing...) is something not so positive: the not-particularly-sized shape drawable covering the entire nine-patch just as if the latter is a normal png.
But the shape does magically avoid the un-stretched part of the nine-patch and overlays only above the streched-part of the nine-patch.
This is awesome...but confusing, why? I may not able to dig that deep into the source but this do sounds anti-intuition (but nice). I want to know the reason though. So I post this here.
Since I tagged this as android-L because I am working on one. But I think this shall be working from something like gingerbread.(just to replace the ripple drawable with something else, maybe a inset drawable etc.)
This effect is caused by the combination of two things:
All nine-patch drawables have a padding area defined automatically from the edges of the content area. The content area can be defined either explicitly, using the right and bottom lines at the border, or implicitly from the stretchable area defined by the left and top lines.
Layer-list applies the padding on each layer cumulatively to the next layer by default*, effectively treating each layer as the content of the previous layer.
* Lollipop has introduced a new attribute for disabling this behavior.
Related
what is the essence of the question:
Why is the color that I write in solid in the shape not taken into account
Why do I have a white background on the back of the button now (attached a photo)? I don't understand where I can fix it
I found a video on YouTube and there was the following solution: Create an xml file in drawable and there I specified the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/teal_700"></solid>
</shape>
</item>
</selector>
The problem is that when the videos came out, there were no themes and everything worked out perfectly right away, and I tried to figure out why I didn't apply color to the button if I set it in the shape, but I never figured it out and it doesn't take into account my color that I set in solid :) I decided to change the primaryColor in the theme to the color I needed and then it applied.
And can you also throw, please, useful modern resources to the latest versions of android studio? I'm just learning, so I haven't found much yet and it would be interesting to see some examples for new updates, because all the videos are from earlier versions and some of them don't converge, as, for example, now in the question
If there are not enough details to solve the problem, then write to me
To make A Button Round Use MaterialButton
<com.google.android.material.button.MaterialButton
android:layout_width="100dp"
android:layout_height="50dp"
android:text="1"
app:cornerRadius="18dp" />
I was surfing the web to find a new style for the android layout and I find a pretty interesting one. Here is the Image.
I know more than basics about layout, but what I wanna is how can I give a 3d style look like in the above image? Especially that #7881 Code Box.
Here is Something that I have tried.
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:bottom="-25dp"
android:top="-25dp"
android:left="25dp"
android:right="25dp">
<rotate android:fromDegrees="20">
<shape android:shape="rectangle">
<size
android:width="50dp"
android:height="100dp"/>
<solid android:color="#E30B3E"/>
</shape>
</rotate>
</item>
</layer-list>
The output is this
Overall it gives the textview a 3d look but I want something like that in the image.
Any Suggestions?
Its pretty much simple just use android:rotationX property on the CardView
like this
<com.google.android.material.card.MaterialCardView
...
android:rotationX="20"
...
That's easy as long as the box should not be animated in its shape.
Go to https://www.figma.com/ and start a new project (it's free)
Export any shape you create there as svg
Import this svg into Android Studio using the Resource Manager
add your new drawable as background of the box.
Additional input, so your 50 points aren't wasted ;) :
You find several Android Presets for figma. Like UI Elements or buttons. For example here. For more just google for "Figma Android UI kit"
You can edit the generated SVG to your likes. Since it is only a text file, you can customize its colors (also using backgroundTint in xml), etc.
I'm working on the splash screen for my Flutter app, and in drawable folder I have to create a file called colors.xml in order to change background color for my splash screen. I'm finding it hard to make it a gradient color. My intention is to create a gradient background color that would start at the top left and end at bottom right, using two different colors. Does anyone have an example of how to do that in Flutter? Thanks!
P.S. An is there a difference in the process for android and ios?
1 In \android\app\src\main\res\drawable\launch_background.xml
change this :
<item android:drawable="#android:color/white" />
to :
<item android:drawable="#drawable/gradient_background" />
2 Create this file \android\app\src\main\res\values\colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gradientstart">#3587d0</color>
<color name="gradientend">#36f1d3</color>
</resources>
3 Finally, create this file \android\app\src\main\res\drawable\gradient_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/gradientstart"
android:endColor="#color/gradientend"
android:angle="90"/>
</shape>
For Android first define two color in your colors.xml:
<color name="gradientstart">#888888</color>
<color name="gradientend">#111111</color>
then in \android\app\src\main\res\drawable\launch_background.xml just replace this:
<item android:drawable="#color/background" />
to this:
<gradient android:startColor="#color/gradientstart" android:endColor="#color/gradientend" android:angle="315"/>
and for ios according to this question
If your gradient is a simple vertical or horizontal gradient, and
you’re really concerned about asset size, you can define a very narrow
image and then add an image view with “scale to fill” content mode.
But these images are so small anyway, the amount of space saved will
be negligible, so I’m not sure it’s worth worrying about.
I found use of flutter_native_splash much more easy and direct. Here's a link on the steps to creating one. First design your desired gradient as an image and instead of adding color, add a background_image on the pubspec.yaml file.
Something like:
flutter_native_splash:
image: ""
background_image: "assets/my_splash_background.png"
What is the difference between these three xml files in android?
xml file with selector as root element
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<Further Designing ... />
...
</shape>
</item>
</selector>
xml file with shape as root element
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<Further Designing ... />
...
</shape>
xml file with layer-list as root element
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<Further Designing ... />
...
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<Further Designing ... />
...
</shape>
</item>
</layer-list>
Which one is used in which case. A little examples will be more appreciated. Thanks!!!
Layer List
From docs:
A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index is be drawn on top. Creates a LayerDrawable.
This is used when you want to combine multiple drawables into one, for example you could use it to add a background to an icon.
Selector (StateList)
From docs:
A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the state of the object. For example, a Button widget can exist in one of several different states (pressed, focused, or neither) and, using a state list drawable, you can provide a different background image for each state.
It can used to show different drawables in different conditions.
Shape
From docs:
An XML file that defines a geometric shape, including colors and gradients. Creates a GradientDrawable.
This should be used when you want to show a single shape. It can be used inside a LayerList to add a shape to an image or it can be used with a selector to switch between shapes as well.
Fun Part(hopefully)
All 3 of these represent drawables so you can combine these to meet your needs. For example I could have selector as root element, which contains LayerList and each layer list can have shapes.
When working with these dynamically, all 3 are subclasses of Drawable class.
Check out documentation for more information:
https://developer.android.com/guide/topics/resources/drawable-resource
Selectors let you employ different drawables or shapes depending on state (like, pressed, checked, etc.). Use them, for example, when you want a pressed state and a non-pressed state for buttons.
Shape lets you draw simple shapes, and apply border radius, colors, gradients, etc. to them.
Layer-list lets you layer drawables on top of each other to create composite drawables. For example, a common way to create a bordered background is to create a layer-list with a rectangle as one item and a slightly smaller rectangle (with 1dp padding, say) inside of it. The larger rectangle is your border outline.
In my application there are more than one components with different height which do have same gradient background(I have used multiple shapes and gradient item in layer-list).
but in item tag i have to specify the height of shape in 50dip and i want to reuse it in more than one UI-Components with percentage measures.
I found that there is no weight attribute supported in the shape. so is there any other alternative to make it working in different sized views or any percentage supported way?
I want to get the solution in XML style.
the layer list looks like
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:drawable="#drawable/green_horizontal_gradient"
android:id="#+id/upper_gradient"
/>
<item
android:drawable="#drawable/half_overlay"
android:id="#+id/lower_overlay"
android:top="50dp"
/>
</layer-list>