App Development

Android Development Tidbits // No. 3

android-development-tidbits-no2 blog-featured-image cf-510x296

Hello again and welcome to the third post in the Android Development Tidbits series. We’re glad so many of you have found the series interesting so far, and thank you for expressing your support through comments and emails to us! For anyone that’s here for the first time, we (the Android team) have been sharing tidbits we learn throughout the week with each other internally for quite some time. Recently, we began sharing our tidbits with the rest of the development community on the blog. You can find our two earlier posts in the series here and here. Perhaps one of these weeks we’ll share some of our earliest tidbits for a few laughs! Anywho, without further ado, here are this week’s tidbits.

Tidbit One If you’re using QRCodeWriter by ZXing to generate qr codes, it’s rather slow to generate large image sizes. Instead, you can pass a size of 0x0 and it will then return a BitMatrix of the minimum possible size (each block is 1 pixel). Then you can write that matrix into a BitmapDrawable and set it as the background on some view. Make sure you call setFilterBitmap(false) on the drawable first, though, so it doesn’t blur when scaling.

BitMatrix matrix = new QRCodeWriter().encode("content here", BarcodeFormat.QR_CODE, 0, 0);
int height = matrix.getHeight();
int width = matrix.getWidth();
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
for (int x = 0; x < width; x++) {
for (int y = 0; y < width; y++) {
bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.TRANSPARENT);
}
}
BitmapDrawable qrCodeDrawable = new BitmapDrawable(getResources(), bmp);
qrCodeDrawable.setFilterBitmap(false);
imgQrCode.setBackground(qrCodeDrawable);

I used Bitmap.Config.ARGB_4444 because I needed the background of the QR code to be transparent. If you just want black and white, you could use Bitmap.Config.RGB_565. And if you want to be safe, you can change the creation of the QR code to BitMatrix matrix = new QRCodeWriter().encode("content here", BarcodeFormat.QR_CODE, 10, 10); just in case the lib stops accepting oxo in the future, as I don’t believe a QR code will ever be smaller than 10x10. - Tidbit contributor, James Sun

Tidbit Two Next time you go to use “adb shell” type “adb hell” instead. Trust me. - Tidbit contributor, Tyler Romeo

Tidbit Three TextUtils.concat() will give you a CharSequence concatenating the input CharSequences, retaining their spans. - Tidbit contributor, Walker Hannan

Tidbit Four “Leaf” views get first dibs on touch events, but “root” views get first dibs on intercept touch events, so if you need to use a touch event that a child view is using, use the intercept event. And if that child view is calling setRequestDisallowInterceptTouchEvent and you don’t want it to do that, just override it and don’t do anything. - Tidbit contributor, Frank Doyle

Tidbit Five When doing bulk operations you can use SqliteDatabase beginTransaction() and endTransaction() methods, but make sure to call setTransactionSuccessful() or else the changes will be rolled back on endTransaction(). - Tidbit contributor, Walker Hannan

Tidbit Six If you’re doing tests, you should probably be wary of using static methods! Why? Well among other reasons, methods provided by the Android framework won’t work in tests, and since they’re static methods you can’t mock them out. - Tidbit contributor, Frank Doyle

Tidbit Seven If you’re doing tests, you should try out Mockito. It lets you replace your complicated object dependencies with super-simple replacements. - Tidbit contributor, Frank Doyle

Quickstart-Guide-to-Kotlin-Multiplatform

A Quick Start Guide to Kotlin Multiplatform

Kotlin Multiplatform, though still experimental, is a great up-and-coming solution...

Read the article