Filling a Blank Canvas – 100 Days of Code…
When working with drawables, don’t forget the most basic rule of all.
I tried adding an image as a background for a small project. Not only was it invisible in the Design view, testing it crashed my virtual device! Turns out I forgot one simple, obvious step.
For more on this, watch the video below. You can find my source code for the app here.
I tried adding an image to my HappyBirthday project. I ended up with errors about the image’s source name and the image wouldn’t render in the Design view. Additionally, I could see the outline, but it didn’t render a preview for the actual image file.
I set constraints on the image to 0dp. This forced the constraint to match the parent. In this case, the image would connect with the top and bottom of the layout.
The resolution didn’t match the phone so the image was forced to sit in the middle of the screen touching the left and right sides of the layout. I set the Scaletype to CenterCrop to crop the center of the androidparty image. This would use the cropped part as the full background for the layout. Once
Still didn’t work
I decided to delete the drawable and re-import it. When I browsed the project files, I saw the androidparty.png file wasn’t even in the same directory.
It was STILL in my Downloads folder.
After moving it to the project root, I re-imported the drawable and the image FINALLY rendered as expected.
Last step was to create custom Strings to use for the image description and both TextViews. I stored these in the res/values/strings.xml file.
It’s cool Android Studio lets you import drawables from outside the project root. However, it causes unexpected issues later down the line. Moving forward, I’ll be sure to include any and ALL project resources in the project directory first. THEN I can import drawables and anything else I need without error.
For more on this, watch the video below.
Join the mailing list to see updates like this every week!
Coding Fanatic
Sources: