How Android Handles Image Sizes for Different Screen Densities

When making images for Android apps, it's important to make sure they look good on all types of screens. Android achieves this by creating different versions of the same image that fit various screen types.

Here's how it works

1. Base Image Size (mdpi): You start with a base image size, typically known as mdpi (medium-density pixel). This serves as the reference point for other pixel densities.

2. Scaling Factors: Android uses scaling factors to determine the size of an image for each pixel density. For instance, hdpi (high-density pixel) images are 1.5 times larger than the mdpi base, xhdpi images are twice as large, xxhdpi images are three times larger, and xxxhdpi images are four times larger.

3. Calculating Image Sizes: Using the scaling factors, you can calculate the dimensions of images for various pixel densities. For example, if your base image is 48x48 pixels (mdpi), the sizes for different densities will be:

  • mdpi: 48x48 pixels (base size)
  • hdpi: 72x72 pixels (48 * 1.5)
  • xhdpi: 96x96 pixels (48 * 2)
  • xxhdpi: 144x144 pixels (48 * 3)
  • xxxhdpi: 192x192 pixels (48 * 4)

By providing images in different sizes for different pixel densities, Android ensures that your app's visuals look crisp and well-suited for various devices, whether they have standard or high-resolution screens.

#AndroidDevelopment #ImageAdaptability #UserExperience #AndroidImageAssets

How to use API KEY in the Adnroid app

How to Hide & Protect API Keys in Your Android App.

  1. Get a Maps API key.
  2. Create a file in the root directory of your project called secure.properties (this file should NOT be under version control to protect your API key)
  3. Add a single line to secure.properties that looks like: API_KEY=YOUR_API_KEY, where YOUR_API_KEY is the API key you obtained in the first step.
  4. Put following code into your module build.gradle file:
    android {
      ...
      defaultConfig {
        ...
          def secureProps = new Properties()
          def securePropsFile = rootProject.file("secure.properties")
          if (securePropsFile.exists()) secureProps.load(new FileInputStream(securePropsFile))
          resValue "string", "api_key", (secureProps.getProperty("API_KEY") ?: "")
        ...
  5. Use api_key in your Manifest:
    <meta-data
      android:name="com.google.android.geo.API_KEY"
      android:value="@string/api_key" />
  6. Your API KEY can be accessed at runtime using the statement: getString(R.string.api_key)
  7. Build and run

Adnroid: How switch between two activites by screen orientation

There are two activities: MainActivity and SecondActivity.

  • MainActivity should be displayed in the portrait orientation
  • SecondActivity should be displayed in the landscape orientation

We need such attributes in Manifest:

<activity
        android:name=".MainActivity"
        android:screenOrientation="fullSensor"
        android:configChanges="orientation|screenSize"
...
<activity
        android:name=".SecondActivity"
        android:screenOrientation="fullSensor"
...

MainActivity starts SecondActivity when the screen is rotated into LANDSCAPE:

import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
...
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
  super.onConfigurationChanged(newConfig);

  if (ORIENTATION_LANDSCAPE == newConfig.orientation) {
    startActivity(new Intent(this, SecondActivity.class));
  }
}

SecondActivity checks on create if current orientation is LANDSCAPE, otherwise it finishes:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (ORIENTATION_PORTRAIT == getResources().getConfiguration().orientation) {
      finish();
      return;
    }

    setContentView(R.layout.second_activity);
    ...
  }

When attribute configChanges="orientation|screenSize" is not present - activity will be recreated on each screen rotation, otherwise - method onConfigurationChanged is called.

Example on Github