Im trying to make transparent Buttons and editTexts, I'm using a custom xml file, it works on Android 5 as expected (all fields are indeed transparent), but when running the same app on the emulator using Android 4.1.1, these fields appear black.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:background="#android:color/transparent">
<stroke android:width="3px" android:color="#FFF" />
</shape>
How can the transparency effect be achieved on older android OS's?
Setting background as transparent is working api level 4.0 onwards add below line in your xml
android:background="#android:color/transparent"
or Try some thing 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="#android:color/transparent" />
<stroke android:width="3px" android:color="#FFF" />
</shape>
Add a color property to your shape tag:
<solid android:color="#android:color/transparent" />
Try defining the transparent colour yourself, just to be sure it's not a framework thing.
<color name="transparent">#00000000</color>
Transparency definitely works on Jelly Bean, so I suspect it could be the emulator.
Related
I wonder how is it possible to make an elevation of a CardView / or EditText in android Studio like the picture below - with the same color for the shadow (I'm searching for the same result) elevation and cornerRadius
<?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="#color/view"/>
<corners android:radius="#dimen/_90sdp"
/>
</shape>
</item>
<item
android:left="#dimen/_1sdp"
android:right="#dimen/_40sdp"
android:top="#dimen/_1sdp"
android:bottom="#dimen/_40sdp"
>
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="#dimen/_90sdp"/>
</shape>
</item>
Add the following dependency to the build.gradle file.
implementation 'com.google.android.material:material:1.1.0'
Add the following code to your xml file to create a MaterialCardView. Modify the values of cardCornerRadius and cardElevation till you get desired result.
<com.google.android.material.card.MaterialCardView
...
app:cardCornerRadius="4dp"
app:cardElevation="10dp"
...
>
...
</com.google.android.material.card.MaterialCardView>
As for shadow, there is no implicit support within the framework to change the shadow color.
Check out this Twitter thread by Nick Butcher where shares a prototype implementation to achieve something similar. He has also mentioned that it is doubtful that the feature will be added to the support library.
I have tried to search this a lot however I was not able to make the same buttons as in the picture for android.
I have been trying to use the following code but the button is not transparent and it doesn't have that nice rounding on the sides:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<corners android:radius="15dp" />
</shape>
Currently it looks like this
I want the EditTexts and the buttons to be transparent with a white border
It would be a great great help if someone could assist me on this.
Defining shape is proper approach. If you want it to be transparent with white border you need to avoid using solid tag and use stroke (that is the border).
So for example you will have this shape (let's call it bg_button.xml under drawable folder):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#android:color/white" android:width="1dp" />
<corners android:radius="15dp" />
</shape>
Apply then this to your views as background. This is an example:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:background="#drawable/bg_button"/>
Please try this not just on Android Studio editor but also on your emulator / device. Rendering of rounded corners on Android Studio editor can create problems.
Say you have a xml drawable called rounded_button
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#6E6E6E"/>
<corners
android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
</shape>
You then set the background of the button to the #drawable resource you created.
This is the complete xml :
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/pink" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<stroke
android:width="#dimen/border_size"
android:color="#android:color/white" />
</shape>
</item>
What it did is simply have a pink background color with white outline. It works perfectly when i test it using android 4.2.2.
However, the pink background color always become the black color in android 4.1.2 (the outline is still white - which is working fine).
Note that i use real device (not a fan of emulator)
Please kindly help me, Thanks for your help.
Note : I've tried to clean the project, uninstall the apps.
Thanks to #Der Golem, this is the working - simplified code :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/pink" />
<stroke
android:width="#dimen/border_size"
android:color="#android:color/white" />
</shape>
Do you really need 2 overlayed shapes?
One is just enough.
Simply, set the solid (fill) color and the stroke (border) color on the same shape.
I would like to create a border around my RableRow.
I am using the following shape as background.
bg_stroke_dotted_right
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-4dp"
android:left="-2dp"
android:bottom="-4dp"
android:drawable="#drawable/stroke_dotted">
</item>
</layer-list>
stroked_dotted drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#000000" />
<stroke
android:dashGap="4dp"
android:dashWidth="2dp"
android:width="2dp"
android:color="#color/greyDark" />
</shape>
I do not understand why using "bg_stroke_dotted_right" as bg - files the background with black color.
As soon as I add "bg_stroke_dotted_right" as bg - the bg turns to black.
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_stroke_dotted_right"
>
in the shape try this :
<solid android:color="#00000000" />
in this case the first 2 "00" are the alpha of the colors, and "00" meand transparent.
enjoy!
The accepted answer suggests to use transparent in the solid element. Where as this is the reason for the bug (missing the solid element falls back to black on android <4.2) the solution is not the most optimal.
For starters instead of hard code the colour you can use #android:color/transparent however it's potentially best to use #null so that it doesn't overdraw. Something like this:
<solid android:color="#null" />
I need to create black iphone like gradient in my android application. Please view black gradient at the top in the image below. How to do it? Thanks
Something like this, perhaps?
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#FF000000"
android:endColor="#FF8E8E8E"
android:angle="270"/>
</shape>
I've posted a blog about an Iphone look an Android:
http://www.dibbus.com/2011/06/android-gradient-toolbar/
You can find some examples of gradients and what is possible using xml and/or 9-pacth images.
You can create gradients in XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:type="radial" android:gradientRadius="400"
android:startColor="#88cce7" android:endColor="#075e81"/>
</shape>
This example is a round gradient but by changing type param you can create others.
Include this code in a xml file in your drawable folder and you can refer to that file when you set a background. ie. android:background="your drawable file"
I know this is an old question, but I was messing around to get this same effect for a customer who wants a copy of his iPad app on Android, this is what I came up with:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:centerColor="#FF333333"
android:centerY="2%"
android:endColor="#FF000000"
android:startColor="#FF8E8E8E" />
<corners android:radius="5dp" />
</shape>