Development of a mobile application is impossible without high-quality visual design, and it is graphic resources that make the interface lively and understandable for the user. Developers are often faced with the need to integrate logos, icons or background images into their project using the environment Android Studio. Correct placement of these files in the folder res/drawable is a critical step on which the correct display of content on devices with different screen characteristics depends.

There are several ways to import graphics, each of which has its own characteristics and is suitable for certain types of tasks. In this guide, we'll take a deep dive into the process of adding raster and vector images, explain the differences between the formats, and show you how to avoid common mistakes when working with assets. You will learn not just to copy files, but to manage them professionally, ensuring high performance of your application.

Preparing graphic files before importing

Before transferring images to the project, you need to make sure that they meet the technical requirements of the platform Android. Files must have valid names consisting only of lowercase Latin letters, numbers and underscores, since using capital letters or spaces will result in a compilation error. Recommended formats for raster graphics include PNG and WebP, which provide good quality with a relatively small file size.

For icons and simple illustrations, it is strongly recommended to use the vector format SVG or its native analogue VectorDrawable. Vector graphics are scaled without loss of quality and take up significantly less space in the device memory compared to raster images. If you have sources in PSD or AIformat, you should first export them to a compatible view, optimizing the layers and removing unnecessary metadata.

โš ๏ธ Attention: Never use file names that match the system's reserved keywords or class names, as this will cause a name conflict and the inability to compile project.

It is also important to consider the screen pixel density for which the image is intended so that it looks clear on all devices. Although modern tools allow you to automatically scale graphics, manually preparing sets for different densities (mdpi, hdpi, xhdpi) gives better control over the result. Check the resolution of your files: they should not be excessively large so as not to increase the size of the final APK application package.

๐Ÿ’ก

Use online tools to compress PNGs without losing quality before adding them to the project, this will reduce the weight of the application by 10-20%.

Drawable folder structure and resource types

In the development environment, Android Studio folder drawable serves as a central repository for all graphic resources used in layouts and code. Understanding the structure of this directory helps you organize your files properly and avoid confusion when maintaining your project in the long run. The system automatically generates identifiers for each file, allowing you to access them through a class R.drawable in Java or Kotlin code.

There are several options for folders for placing graphics, depending on the API version and screen orientation. Base folder drawable is used for default resources, while specific versions like drawable-v24 are intended for functions available only in new versions of the operating system. Resource sharing allows you to flexibly manage the appearance of the application on old and new devices without writing additional code.

  • ๐Ÿ“ drawable โ€” the main folder for images used in all device configurations.
  • ๐Ÿ“ drawable-nodpi โ€” a folder for images that should not be scaled by the system regardless of screen density.
  • ๐Ÿ“ drawable-anydpi โ€” a special directory for vector graphics, available on all versions of Android through the library support.

When working with adaptive icons, a structure is often used where the main image is in mipmap, and auxiliary elements are in drawable. However, for ordinary interface images, such as button backgrounds or illustrations in lists, the folder drawable is the de facto standard. Proper distribution of files across these directories makes it easier for other team members to navigate the project.

๐Ÿ“Š What graphics format do you use most often?
PNG
JPEG
WebP
VectorDrawable (SVG)

Methods of adding images to a project

There are several ways to add a picture to a project, and the choice of method depends on your preferences and the specific task. The simplest option is to directly copy the file from the operating system explorer to the corresponding project folder through the file manager Android Studio. To do this, you need to switch to the project view Project, find the folder res/drawable and drag the prepared image file there.

A more professional and recommended way is to use the built-in import wizard, which will automatically optimize the image and create the necessary versions for different screen densities. To use this tool, right-click on the folder drawable, select New and then Image Asset. This method is especially useful when working with application icons, where strict design guidelines are required.

Menu path: File โ†’ New โ†’ Image Asset

The wizard allows you to configure the cropping, dimensions and background layer of the image directly during the import process, saving time on pre-processing in graphic editors. Once the process is complete, the file will appear in Resources and you can immediately use it in your layouts without worrying about compatibility. Using built-in development environment tools reduces the risk of human error and guarantees compliance with platform standards.

โ˜‘๏ธ Check before import

Done: 0 / 4

Importing vector graphics and SVG

Vector graphics have become a standard in modern software development Android, allowing you to create clear images of any size without adding weight applications. Android Studio supports direct import of format files SVG, automatically converting them into a native format VectorDrawable, understandable to the system. This process preserves all the paths and colors of the original file, making it editable right inside the development environment.

To import a vector image, follow the same steps as when working with a raster: right-click on the folder drawable and select New โ†’ Vector Asset. In the window that opens, you can select a local SVG file or use the library of built-in icons provided by Google. The conversion occurs instantly, and as a result you receive an XML file describing the geometry of the image. Material Icons, provided by Google. The conversion occurs instantly, and as a result you receive an XML file describing the geometry of the image.

Parameter Raster image (PNG) Vector image (XML)
Scalability Loses quality when zoomed in Ideal quality at any zoom
File size Depends on resolution (can be large) Usually very small (several KB)
Editable Requires a graphic editor You can change colors and paths in the code
API support All versions of Android API 21+ (or through the support library)

The use of vectors is especially important for navigation icons, logos and simple illustrations where photorealism is not required. However, it is worth remembering that complex vector images with thousands of paths can negatively affect interface rendering performance. In such cases, it is more advisable to use pre-rasterized high-resolution images.

โš ๏ธ Attention: When converting complex SVG files, some effects (gradients, shadows, filters) may be lost or displayed incorrectly, so always check the result on the emulator.

What to do if SVG is not importing?

If the import wizard gives an error when loading SVG, try opening the file in a vector editor (for example, Inkscape or Illustrator) and simplify the paths structure by combining complex groups and removing invalid attributes.

Setting up display in XML layouts

After successfully adding an image to the assets folder, the next step is to integrate it into the user interface via XML layouts. To display a picture, a widget ImageViewis used, which must be given an attribute android:src or app:srcCompat with a link to the file name without extension. Correctly setting the scaling parameters ensures that the image will occupy its allocated space correctly.

The attribute android:scaleType plays a key role in how the image will behave inside the widget container. Value centerCrop fills the entire space, cutting off unnecessary parts, which is ideal for profile pictures and background images. At the same time, the value fitCenter scales the image so that it fits completely into the widget, maintaining proportions and leaving empty space if necessary.

<ImageView

android:id="@+id/my_image"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/logo"

android:scaleType="fitCenter" />

When working with vector graphics on devices with Android versions below 5.0, you must use the attribute app:srcCompat instead of the standard one android:src. This requires connecting the library AndroidX AppCompat depending on the project, but ensures full vector compatibility with older versions of the operating system. Ignoring this rule will cause the application to crash on devices with outdated software.

๐Ÿ’ก

Always use app:srcCompat for vector graphics to ensure support for Android 4.x and 5.x devices without additional effort.

Eliminating common errors and problems

When working with graphics Developers often encounter compilation errors related to incorrect file naming or location. The most common problem is the "Invalid file name" error, which occurs when you use capital letters, hyphens, or spaces in the file name. The Android resource system is extremely sensitive to naming syntax, and any deviation from the โ€œlowercase Latin letters and numbers onlyโ€ rule blocks the project from being built.

Another common situation is the absence of an image in the mockup preview or on a real device, despite the fact that the code is written correctly. This may be due to the fact that the file was added to the wrong folder, for example, to mipmap instead of drawable, or the development environment cache was not updated. In such cases, it helps to clean the project through the menu Build โ†’ Clean Project and subsequent reassembly Rebuild Project.

  • ๐Ÿšซ Name error: The file is called MyImage.png instead of my_image.png.
  • ๐Ÿšซ Wrong path: The link in the code points to a non-existent resource or typo in the title.
  • ๐Ÿšซ IDE cache: Android Studio does not see the new file until the project is fully synchronized with the Gradle files.

If you are using complex vector drawables, make sure that they do not contain attributes that are not supported by the current version of the support library. Sometimes manual editing of a vector XML file can lead to syntax errors that are difficult to notice visually. Carefully check the tag structure path and the correctness of closing brackets in the resource code.

โš ๏ธ Attention: The Android Studio interface and project structure may change with the release of new versions of the development environment, so check the current menu paths in the official Google documentation if standard actions do not work.

๐Ÿ’ก

If strange errors occur with resources, try Invalidate Caches / Restart from the File menu - this solves 90% of problems with displaying new graphics.

Sizing optimization and performance

Graphics resources often make up a significant part of the final application size, Therefore, their optimization is an important task for the developer. Using the format WebP instead of PNG allows you to reduce file size by 25-35% without any visible loss of quality, which is especially important for applications with a large number of illustrations. Android Studio has built-in support for converting images to WebP directly through the file context menu.

For vector graphics, optimization consists of simplifying paths and removing hidden layers that are not visible to the user, but increase the size of the XML file. The fewer nodes and commands in the vector description, the faster the system will draw it on the screen, which is critical for animated interfaces and lists with a large number of elements. Regular auditing of used resources helps keep the application in good shape.

Don't forget to remove unused images from the project, as they will still be included in the final build, taking up space on the user's device. Tool Lint As part of Android Studio, it automatically finds such resources and warns about them, offering safe deletion. Following these guidelines makes your application easier and faster.

How to convert PNG to WebP in Android Studio?

Right click on the PNG file in the drawable folder, select "Convert to WebP". In the window that opens, configure the compression quality (recommended 80-90%) and click OK. The file will be replaced with an optimized version.

Why is the image displayed as a black square?

This most often happens when trying to display a vector image on older devices without using app:srcCompat. Also check if the image itself is black on a black background or if the file is corrupted.

Is it possible to use JPEG in drawable?

Yes, JPEG is supported, but it does not support transparency. Use it only for photographs and complex images where a transparent background is not required to save space.

Where are the generated resource IDs stored?

All IDs are generated automatically in the R class (R.java file), which is generated by the build system. You don't need to edit it manually, just access the resources via R.drawable.filename.

How to add a picture programmatically in Java/Kotlin code?

Use the setImageResource(R.drawable.name) methods for ImageView or ContextCompat.getDrawable(context, R.drawable.name) to obtain a Drawable object in code.