Android: Is it possible to scale a xml drawable? - android

Here is what I want to do: I have this scale xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/mydrawable"
android:scaleGravity="bottom"
android:scaleHeight="50%" />
And I want to use it with this drawable xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFFFF"
android:endColor="#FFFF0000"
android:angle="90" />
</shape>
</item>
</layer-list>
Then I set the scale xml as a background for a LinearLayout, but I don't see anything. Am I missing something or is it not possible to do this (with XML)?

This is an issue with the ScaleDrawable object which cannot be resolved in XML.
But it is easy, just do the following:
// Get the scale drawable from your resources
ScaleDrawable scaleDraw = (ScaleDrawable)getResources().getDrawable(R.drawable.background_scale);
// set the Level to 1 (or anything higher than 0)
scaleDraw.setLevel(1);
// Now assign the drawable where you need it
layout.setBackgroundDrawable(scaleDraw);
I tested your code using this solution and it worked fine.

Related

How to auto mirror a shape in Android XML

I have a custom card like shape as below.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorListItemBackground" />
<corners
android:bottomLeftRadius="16dp"
android:topLeftRadius="16dp" />
</shape>
My app also supports RTL languages, thus changing to RTL, this shape does not change(Auto Mirror).
What changes can I do to make it an auto mirror?
Notice the rounded corners near the circle.
LTR_Image
The rounded corners still in the same place.
RTL_Image
You can have define custom shape inside
res/
drawable
drawable-ldltr
drawable-ldrtl
Now put the mirrored custom shape inside drawable-ldrtl and it should work.
Your custom shape xml should be exactly opposite i.e.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorListItemBackground" />
<corners
android:bottomRightRadius="16dp"
android:topRightRadius="16dp" />
</shape>
You can alternatively add android:autoMirrored="true" to your vector drawable which should auto mirror it. But this requires you to have a vector image as per documentation : https://developer.android.com/reference/android/graphics/drawable/VectorDrawable
Hence a shortcut is to wrap your shape around with a vector drawable
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/your_shape"
android:autoMirrored="true"/>
Let me know if this works for you.

Creating android shape that keeps proportions

How do I create an XML ressource shape that looks like a perfect circle no matter which object it is applied to? When I try to add it as a background to a button, it becomes a oval in stead of a circle.
try this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:innerRadiusRatio="5"
android:visible="true" android:thickness="4dp"
>
<stroke android:color="#654321"
android:width="2dp"/>
<solid android:color="#123456"/>
</shape>

Custom background with layer-list to show diagonal lines?

I just want to create a custom background but I'm not getting how to do that with xml not with image.
Here is the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#layout/ui_shape"
tools:context="${relativePackage}.${activityClass}" >
</RelativeLayout>
and I want like this. Is it possible to create xml like the required image?
here is ui_shape
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<rotate
android:fromDegrees="45">
<shape
android:shape="line" >
<stroke android:color="#color/ui" android:width="1dp" />
<solid
android:color="#color/ui" />
</shape>
</rotate>
</item>
</layer-list>
and color
<color name="ui">#0095A0</color>
here is what i got
anybody any idea?
I know I am late, but this is my solution if anyone gets here:
1) create a single small diagonal line in photoshop or any other program, it should look like this
*
*
*
*
2) Create the following XML drawable in android studio where "#drawable/diagonal_line" is the previous mentioned image:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/diagonal_line"
android:tileMode="repeat"
android:dither="true"
>
</bitmap>
what this bitmap drawable will do is take that single diagonal line and repeat it all over the selected view.
3) Now use that XML drawable as your view background, as an example, this is how I used it, where "#drawable/tiled_background" is the XML in step 2:
<View
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/tiled_background"/>
and here is the result:
I hope this helps anyone. Happy Coding!
Use
android:background="#drawable/yourcustombackground"
Define your custom background inside drawable (something like below)
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<stroke android:width="1dp" android:color="#555555"/>
<solid android:color="#111111"/>
</shape>
Update: To display image at background
android:background="#drawable/ic_image"
ic_image is the desired image
Or you can use gradient also
No. it is not possible in android to set diagonal line .You have to use image for it for the diagonal lines

Making circular FrameLayout

I'm working on making Framelayout circular. When I google I got some suggestion of making xml file of it. I did it and in background to FrameLayout as my xml file. But it has no effect. My xml file for ring shape is
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="15dp"
android:thickness="10dp"
android:useLevel="false">
<solid android:color="#ababf2" />
</shape>
I tried all ring and oval shape. None worked. Please help me on this.
New drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#6a000000" />
<size android:height="25dp"
android:width="25dp"/>
</shape>
In your frame layout:
<FrameLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/your_new_drawable" />
Have you actually set the background attribute in your layout, just making the shape is not enough, you have to apply it to the FrameLayout as well using,
android:background=""

android holo theme background

I would like to know, if I can get the background that is displayed when using android's holo theme, as a drawable? (you know, the dark black and blue background that is displayed for 3.0+) I need to use it as a drawable, so if someone knows if this can be done, please let me know..
Sorry, but I forgot to mention that this is displayed on a tablet device, not sure about phones.
Location of the files
Holo-Dark:
/path_to_sdk/platforms/android-version/data/res/drawable/background_holo_dark.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ff000000"
android:endColor="#ff272d33"
android:angle="270" />
</shape>
Holo-Light:
/path_to_sdk/platforms/android-version/data/res/drawable/background_holo_light.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffe8e8e8"
android:endColor="#ffffffff"
android:angle="270" />
</shape>
Make your own
Put the following code in res/drawable/background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
>
<gradient
android:angle="270"
android:startColor="#ff020202"
android:endColor="#ff272D33"
android:type="linear" />
</shape>
Add the background reference to your parent viewgroup
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background">
Play around with the values until you get the desired effect.
If it is possible, your best bet is to look in the android .jar for 3.0 (which can be downloaded using ADT in Eclipse)
The image may be found in /drawable/drawable-hdpi or possibly somewhere within /drawable
If not, it likely is only possible to find in the source.
Here, perhaps this would help: http://developer.android.com/guide/topics/ui/themes.html

Categories

Resources