Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using int value 0 as "null"-equivalent for R.drawable

I'm using an intuitive but perhaps unorthodox shortcut, and I quite simply want to know if it may cause problems, is bad form, or if there is a different, more accepted way of doing it.

The shortcut is: in a function which expects an int which is an R.drawable constant, I sometimes use a 0 to act as an equivalent to passing "null".

Here's a sort of template of how I use this:

int someDrawable = isSomeCondition() ? R.drawable.somedrawable_identifier : 0;
mHandler.post(new UpdateSomeUI(someDrawable));
    
//and, elsewhere in the application,
private class UpdateSomeUI implements Runnable {
   private final int someDrawable;

   public UpdateSomeUI(int someDrawable) {
      this.someDrawable = someDrawable;
   }

   public void run() {
      mSomeImageView.setImageResource(someDrawable);
   }
}

It seems to work - that is, so far it's been doing what I want, and so far I have observed no adverse effects like crashes or the creation of quantum singularities within the device.

Is this safe to do? Is this bad form? I searched for some official integer value that would be recognized as "null" in this sort of circumstance, but came up empty. Does anyone know if such a value exists?

like image 934
Cephron Avatar asked Oct 17 '25 01:10

Cephron


1 Answers

Found this on android documentation for Resource ID, sub-section constants, hope it helps.

Resource ID > Constants

Constants ID_NULL Added in API level 29 static val ID_NULL: Int The null resource ID. This denotes an invalid resource ID that is returned by the system when a resource is not found or the value is set to @null in XML.

Value: 0

like image 178
Arshdeep Singh Avatar answered Oct 19 '25 15:10

Arshdeep Singh