Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify a null as an XML attribute for Android?

Tags:

android

What do I need to put in for an attribute if I want to put a null there?

For example, this piece of code:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/ef_gradient_blue" />
    <item android:drawable="@null" />

</transition>

But this won't work, as my app crashes when it tries to load this drawable. I need it to be null, so it can make a transition from fully opaque to completely see through.

In my Java, I load it like this:

mTransitionBlue = (TransitionDrawable)mProjectResources.getDrawable(R.drawable.transition_blue);

But I get a runtime exception from this, saying it can't find the transition_blue.xml, which is the XML specified before. So how do I achieve this?

like image 827
ThaMe90 Avatar asked May 18 '11 09:05

ThaMe90


People also ask

How do you give a null in XML?

In an XML document, the usual way to represent a null value is to leave the element or attribute empty. Some business messages use a special value to represent a null value: <price>-999</price> . This style of null representation is supported by the DFDL and MRM parsers.

What are attributes in Android?

Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources).

What is view tag in Android XML?

A View occupies a rectangular area on the screen and is responsible for drawing and event handling. Views are used for Drawing Shapes like Circles,Rectangles,Ovals etc . Just Use View with background and apply a Shape using Custom Drawable.


1 Answers

You should use a transparent drawable there (android lib has it predefined):

<transition xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/ef_gradient_blue" />
    <item android:drawable="@android:color/transparent" />
</transition>

the @android:color/transparent's value is #00000000. It's important to have the first two digits 0, since they define the alpha value of the color (the rest is red, green and blue).
So you can use

<item android:drawable="#00000000" />

as well, or redefine it among your colors.

For reference you could take a look at the Android Developers 2D Graphics and Transition Drawables articles.

like image 130
rekaszeru Avatar answered Oct 24 '22 23:10

rekaszeru