I have the following contents in a drawable called gray_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="300dp"
android:height="300dp" />
<solid android:color="#666"/>
</shape>
</selector>
In my app's main layout, I have the following section
<ImageButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/gray_button"
android:layout_margin="160dp"
/>
Unfortunately, the button displays as a small rectangle instead of an oval.
I am fairly certain I have done something stupid.
How can I make my button display the drawable properly?
Update: Based on the given answer I've switched the drawable to the following but now it just displays as a large rectangle instead of an oval.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="300dp"
android:height="300dp" />
<solid android:color="#000"/>
</shape>
The problem is your shape, you are using a selector with a duplicated URI, so remove the selector and use only the shape:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="300dp"
android:height="300dp" />
<solid android:color="#666"/>
</shape>
With this approach you will see the rectangle because is the background button and your drawable as image.
Try to remove selector and margin android:layout_margin="160dp"
This should work:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:width="300dp"
android:height="300dp" />
<solid android:color="#666"/>
</shape>
<ImageButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gray_button"/>
I would recommend to try it on simple Button instead of ImageButton. If it still doesn't works, then probably the problem is in gray_button.xml file otherwise fine.
Related
I am trying to add a linear layout with rounded corners and blurred background in my app. So far ive managed to give the layout rounded corners but i cant figure out how to give it a blurred background.Ive tried setting an alpha attribute in the xml file but it dosent work.
Heres what i want to achieve:
The Drawable resource file:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<stroke android:width="0.5dp" android:color="#B1BCBE" />
<corners android:radius="160dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
The Xml code of the layout:
<LinearLayout
android:id="#+id/team_lay"
android:layout_width="match_parent"
android:layout_height="95dp"
android:layout_marginTop="40dp"
android:background="#drawable/layout_bg"
android:orientation="horizontal">
</LinearLayout>
What i have achieved so far:
How can i achieve the desired results?
TIA..!!!
You can achieve it by create a blur background as a xml file and then set background of your container view to it.
Try my example:
blur_background.xml
<?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="#4CAF50" />
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<solid android:color="#AA000000" />
</shape>
</item>
</layer-list>
And your container layout:
<LinearLayout
android:id="#+id/team_lay"
android:layout_width="match_parent"
android:layout_height="95dp"
android:layout_marginTop="40dp"
android:background="#drawable/blur_background"
android:orientation="horizontal">
</LinearLayout>
Hope it help. ^^
So i fount the solution myself. I changed the transparency of the solid color in the drawable resouce file(as mentioned here https://stackoverflow.com/a/41865518/8175719 ) and it worked.
Hello I have this triangle that I did with xml on Android Studio :
<item>
<rotate
android:fromDegrees="135"
android:pivotX="7%"
android:pivotY="65%" >
<shape
android:shape="rectangle" >
<solid
android:color="#color/blue" />
</shape>
</rotate>
</item>
And what I want is to put a letter at the right of the triangle. How can I do this ?
Thank you very much !
I don't think you can put text in the drawable itself.
What you could do is have your text as a text view and set its background as the drawable.
Or create a button and set its background to your drawable and set the text with the setText() method.
Something like this:
Make a triangle.xml and put it into your drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="135"
android:pivotX="7%"
android:pivotY="65%">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
</shape>
</rotate>
</item>
And then make an imageView, put the triangle as a background and a textView on its side all wrapped inside a linearLayout with orientation horizontal.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/triangle"/>
<TextView
android:text="A"
android:textSize="100dp"
android:includeFontPadding="false"
android:padding="0dp"
android:layout_width="100dp"
android:layout_height="100dp" />
I need to make the below shape using xml.
I have created an oval shape using below xml code.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item android:height="400dp"><shape
android:shape="oval">
<solid android:color="#000000" />
</shape></item>
</layer-list>
But I am not able to draw a line and circle together. Please help me to make the above shape using xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/comment_background"/>
<stroke android:color="#color/gray" android:width="0.5dp"/>
</shape>
This is Code for Creating Circle shape in android
Finally I have come with a solution after little googling.
Followed steps:-
1.Created a xml file called dot.xml in drawable folder to draw a dot or filled circle.
//Dot.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/colorTimeline" />
</shape>
2.Inserted below code into layout file.
<View
android:id="#+id/v1"
android:layout_width="1dp"
android:layout_height="50dp"
android:background="#color/colorTimeline"
android:contentDescription="ad"
android:layout_marginStart="10dp"/>
<View
android:id="#+id/v2"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="#drawable/dot"
android:contentDescription="f"
android:layout_marginStart="3dp"
android:layout_below="#id/v1"
android:layout_marginEnd="24dp"/>
<View
android:id="#+id/v3"
android:layout_width="1dp"
android:layout_height="50dp"
android:background="#color/colorTimeline"
android:contentDescription="ad"
android:layout_marginStart="10dp"
android:layout_below="#id/v2"/>
Final Output :-
Output Screenshot
My eyes bleeding, when i see your solution, sorry. I had to create and show you this one. Remember this example works only for known size of view. If you want to use dynamic values create my example drawable programatically.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- VERTICAL LINE -->
<item
android:left="9dp"
android:right="9dp">
<shape
android:shape="rectangle">
<solid
android:color="#FF0000" />
<size
android:width="2dp"
android:height="200dp" />
</shape>
</item>
<!-- OVAL -->
<item
android:top="90dp"
android:bottom="90dp">
<shape
android:shape="oval">
<solid
android:color="#FF0000" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>
If you need any explanation just let me know.
I am trying to create a simple layer-list drawable for my Android app. When I set the drawable as src of an ImageView it is not drawn correctly:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="20dp">
<shape android:shape="oval">
<solid android:color="#color/dark_gray" />
</shape>
</item>
<item android:left="20dp">
<shape android:shape="oval">
<solid android:color="#color/dark_gray" />
</shape>
</item>
</layer-list>
Just two ovals with a little offset to each other. Nothing special (just a test) but it does not work.
It seems that android:left|right|bottom|top is the problem. If I use only of of these commands the drawable is drawn correctly. If two or more are used the ImageView stays empty.
Works:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="20dp">
<shape android:shape="oval">
<solid android:color="#color/dark_gray" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/dark_gray" />
</shape>
</item>
</layer-list>
Does NOT work (like the first example):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="..." >
<item android:top="20dp" android:left="20dp">
<shape android:shape="oval">
<solid android:color="#color/dark_gray" />
</shape>
</item>
</layer-list>
What is wrong here?
EDIT: # Rod_Algonquin
I use a quite simple layout to test the drawable:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#aaa" >
<ImageView
android:id="#+id/image_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/testDrawable" >
</ImageView>
</LinearLayout>
It seems that the number of android:left|right|bottom|top arguments is not the source of the problem but the concrete value being used.
This drawable works fine and:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="2dp">
<shape android:shape="oval">
<solid android:color="#0f0" />
</shape>
</item>
</layer-list>
This one does not draw anything (the ImageView is just transparent):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="1dp">
<shape android:shape="oval">
<solid android:color="#0f0" />
</shape>
</item>
</layer-list>
The only difference here is the value of android:top. 1dp does not work. 2dp works without any problem.
Any idea?
The problem is that you are using the layer-list drawable as an image resource of the ImageView. android:src will only accept an image not a drawable thus giving you nothing in there.
solution:
Instead of putting it in the android:src put it in the background where you can freely use the drawable.
<ImageView
android:id="#+id/image_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/testDrawable" >
</ImageView>
I need to design button for android app to be rectangle, and to be half one color, half other. In XML of course.
I have tried this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:left="7dp"
android:top="15dp"
android:right="7dp"
android:bottom="15dp" />
<gradient
android:startColor="#a241cb"
android:endColor="#a241cb"
android:type="linear"
android:angle="90"/>
<stroke>
android:width="1dip"
android:color="#a241cb" />
<corners android:radius="0dp" />
</shape>
Any help is appreciated
Using a 9 patch like this (save it in your drawable folder as bw_bg.9.png):
You can get a result like this:
Example layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00f"
>
<Button
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="#drawable/bw_bg"
/>
</RelativeLayout>
Don't mind the size (it's running on a Froyo 2.8 inch screen).
Also don't mind the notification (it's from another running app).
Try this:
frist create background.xml in drwable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#4A225E"
android:endColor="#4C4064"
android:centerColor="#B4AFC5"/>
</shape>
and then set button backgrond its work for me
you can achieve this by using gradients as background. use this
create a new XML file under res/drawable folder and name it as button_bg.xml.
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#D5DDE0"
android:centerColor="#e7e7e8"
android:endColor="#CFCFCF"
android:angle="270" />
</shape>
now use it for your Button like this:
android:background="#drawable/button_bg"
Hope it helps. :)