[Free] Just Density

A get density block is available in many extensions, but I made this really for personal use for when I only needed to get the density of a screen.

Why do you need this? See this example of setting image size in the native listview. The image component image is sized in pixels, the listview image is sized in dpi. If you have the density you can easily set your listview images using pixel size. You may find the dpi sizing is also used in other places, especially in some image/graphical extension.

  • Latest version : 1.0
  • Tested : tested on App Inventor 2 (n198), in companion (2.72u) and compiled on Android 13 and 14.
  • Released : 2024-10-29T00:00:00Z
  • Last updated : 2024-10-29T00:00:00Z
  • Built : using the RUSH Extension Builder by @shreyash

BLOCK

image

Example Usage:

VIDEO

BLOCKS

AIX
uk.co.metricrat.density.aix (3.9 KB)

SOURCE
package uk.co.metricrat.density;

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import android.content.Context;


public class Density extends AndroidNonvisibleComponent {

  private Context context;

  public Density(ComponentContainer container) {
    super(container.$form());
    this.context = container.$context();
  }

  @SimpleProperty (description = "returns screen / device density")
  public float Density() {
    return this.context.getResources().getDisplayMetrics().density;
  }

}
8 Likes