I'm defining a ScaleDrawable in XML according to the Android developers site example:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/sign_ui_text_line"
android:scaleGravity="center_vertical|center_horizontal"
android:scaleHeight="80%"
android:scaleWidth="80%" />
I define this as the background of an EditText
for some reason the drawable isn't displayed (but the EditText view changes it's size according to the definitions).
why doesn't it display ?
how can I scale a drawable to be used as a background ?
ScaleDrawables seem to require some workarounds in order to work properly, from the sounds of things.
See Zeh's answer to a similar question.
Related
I have a drawable which I am using in some scrolling fragment views. This is an image which should be sticky at the bottom of the page, should not scroll with the scroll view and should not stretch.
The drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/ic_emptystate_buildings"
android:gravity="bottom|center_horizontal" />
</layer-list>
Setting this drawable as background like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_buildings_grey"
android:orientation="vertical">
works exactly as I want on newer devices, but when I tried on a Samsung S4 with Android 5.0.1, the drawable background streches all the way to the top. The image ('ic_emptystate_buildings' - an imported vector drawable) is only maybe 100dp tall and should not be stretched and should stick to the bottom.
The reason I did not create an ImageView was because its the background of a ScrollView and the XML got messy when I tried to 'hack' it to work. I also want this behaviour in multiple views, so this solution seemed the best because of no duplicate code and simple, clean XML.
Does anyone know why its not working on older versions, and perfectly on newer? And more importantly; is there any nice way to fix this?
Question: Is there a way to dynamically set the "src" attribute on an XML "bitmap" in an Android XML file?
I have a custom XML drawable named "repeat_background.xml" defined as such:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" />
I use this drawable to "tile" a 1x1 jpg (named background.jpg) as the background for all the pages in my app, and it works great - here's an example setting it as the background of a LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/repeat_background">
However, I want the actual jpg (in this case "background.jpg" from #drawable/background) to be based on the user's preference - so I'll have a list that allows the user to pick red, blue, orange, etc. so the user can override the background color shown in the app, and I'll have a 1x1 jpg corresponding to each available color in my resource bundle - but how do I show the preferred jpg as the background?
I don't want to manually have to call some code in every Fragment or Activity, I want something that will respect the user's preferences and react accordingly.
I've tried to extend the BitmapDrawable class, but didn't get very far. Any suggestions for how I can accomplish this are greatly appreciated.
You can do it in java code, using setBackgroundDrawable(new ColorDrawable(color)). It can't be done in xml.
Ok, so I'm working on a very simple Android homescreen widget that fetches the latest Image from an imageboard and displays it in a imageview.
I'd like to have the option to Style this widget, to be in keeping with Googles Material design guidelines, however, I hit the following problem whilst developing and testing on my Nexus 5 running Lollipop 5.1 -
I can't seem to use a cardview in a widget. It gives me a class not found exception however I do not get such exceptions previewing the layout and my gradle has the right dependancies so I suspect it is more to do with widget compatibility?
So I did some further reading and on the Google API page about widgets it has a very specific list of views that widgets support, which did not, to my surprise include cardviews. So I've gone back to my plain old Imageview
I guess my question then sits with, how would I get this imageview to look like a material design card? EG, rounded corners, the subtle 3d effect, drop shadow, ETC. I do not wish to give the imageview a border, the edge of the image should extend to the edges and get clipped by the rounded corners.
I have tried by simply setting the style of the Imageview to be that of a cardview, but that seems to have done absolutely nada :)
Anyone has some ideas? Thank you!
you might want to have a look at the source of the google i/o app : https://github.com/google/iosched/blob/master/android/src/main/res/layout/widget.xml
This is from a long time ago, but it's very doable to make a simple card background in XML.
The way to do this is to create an cardview-esque .xml file in your drawables app resources folder.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners android:radius="4dp" />
</shape>
and set the background view of your Widget's XML layout as
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/widget_card_background"
android:orientation="vertical">
<.../>
</LinearLayout>
You are correct in that CardView is not an available View type for Android Widgets, but this should create the functionality you were looking for 2 years ago
I'm trying to set a background via a XML file that has to contain various shapes, I don't whant them to overlap (i've already played with that) but I want them to be one below the other.
I already have a layer-list defined in one xml, and now I want to have a shape under that one.
How do I accomplish that using only xml?
Thanks, I've been looking arround but all I find it's information about layer-list and not this particular situation. I'm sure there's a post about it arround here, but I can't seem to find it. Sorry in advance.
PD: Since I'm asking, any way to accomplish a blur effect on a shape?
Edit: Another way to ask the same question: I have a rectangle.xml and a circle.xml, how to I put one below the other for it to be used in a background.
There are two obvious options here:
Split the layers out into separate drawables and assign them to different views in e.g. a vertically orientation LinearLayout.
Specify an appropriate value for the android:top attribute to offset the second (and 3rd an 4th etc) drawable.
Example for 1:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/drawable1" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/drawable2" />
</LinearLayout>
If your current LayerDrawable depicts some sort of state, you may also want to look into StateListDrawable or LevelListDrawable.
Example for 2:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/drawable1" />
<item android:drawable="#drawable/drawable1" android:top="50dp" />
</layer-list>
The second option requires some a priori knowledge about the size of the first item. If you define the drawable using xml, you'll have to set the at appropriate offset value at design time.
I have custom EditTexts and I noticed their backgrounds draw wrong when they leave their parents. So I used android:clipChildren="false" on their parent. That works fine. They draw correctly when partially out of their parent now.
This gave me a new problem though. On older devices (< Android 2.3? Not confirmed what the max version is for this issue), the background doesn't get clipped to it's padding. The EditText backgrounds are now drawing to the full height/width of the screen. This only happens on the initial layout.
Has anyone experienced this before? It's really weird. I don't get why the background only draws wrong when using android:clipChildren="false" and only on some devices. I need that though since my EditTexts can be dragged around and need to keep drawing outside their parent container.
I just ran across the same problem. It was caused by having ColorDrawables as background (a StateListDrawable (<selector>) containing several #color/... items, to be exact).
It looks like this was fixed in Android 3.2.4-r1 (commit 95930e1).
Before that, the class comment used to say:
Note that a ColorDrawable [...] ignores the Bounds, meaning it will draw everywhere in the current clip even if setBounds(...) was called with a smaller area.
This was removed, and the draw(Canvas) method changed to respect the bounds.
As a workaround, if you need to support older Android versions, you can use a ShapeDrawable with a solid color to get the same behaviour:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/your_background_color"/>
</shape>
We had a 2.3.4 device where this issue was causing an ImageView to cover everything above it in the LinearLayout it was contained in.
The fix was to create the ShapeDrawable mentioned above and use that as the background to the image.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/your_background_color"/>
</shape>
<ImageView
android:id="#+id/this_is"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="#drawable/a_rectangle_shape_drawable"
android:contentDescription="#string/an_image_view"
android:scaleType="centerInside"
android:src="#drawable/an_icon" />