Android StrictMode for debugging

strictmode.png?w=2924

Introduction

Android provides a handy tool for developers to see what happened to their Apps. Sometimes, you just forgot to keep massive loading job off the main UI thread, e.g., network access, database query, or something you need much time to handle, in these cases, you usually got ANR dialogs to notify you: Your App is going to spend too much time on something in UI thread.

Read more »

1*5koxNC03BSLLt6Urass5yA.png

Small tip

You might wanna change the text alignment of the alert controller. Let’s say, change the alignment from center to right. Then the following is for you.

Just simply open a playground and run these code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import UIKit
import XCPlayground
import PlaygroundSupport

let alertView = UIAlertController(title: "Demo Alert", message: "", preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

let paragraphStyle = NSMutableParagraphStyle()
// Here is the key thing!
paragraphStyle.alignment = .left

let messageText = NSMutableAttributedString(
string: "Left Position, correct?",
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font : UIFont.preferredFont(forTextStyle: .body),
NSAttributedString.Key.foregroundColor : UIColor.black
]
)

alertView.setValue(messageText, forKey: "attributedMessage")

let viewController = UIViewController()
PlaygroundPage.current.liveView = viewController.view
viewController.present(alertView, animated: true, completion: nil)

The most important thing is the key attributedMessage. An alert controller can change its text alignment value by using this key.

[References]

Introduction to Key-Value Observing Programming Guide

mysql-logo.jpg?w=2924

What happened?

Alright, I don’t know what happened when trying to log in to MySQL. And it shows that message: MySQL - Error 1045 (28000) using password: YES. I am pretty sure that user/pwd pair is correct. After searching and studying some articles and forum discussions. I found it might be caused by broken SQL or reinstall SQL. The interesting is I can’t remember I had done that before.

Read more »

android_multi_screen.png?w=2924

Introduction

This is just a note for myself to remind me how to do dp-to-pixel translation and vice versa. Let’s see how to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static float convertDpToPixel(float dp, Context context) {	
float px = dp * getDensity(context);
return px;
}

public static float convertPixelToDp(float px, Context context) {
float dp = px / getDensity(context);
return dp;
}

public static float getDensity(Activity context) {
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return metrics.density;
}

It’s pretty simple, enjoy.
Happy Coding.

[Reference]

  1. DisplayMetrics | Android Developers
  2. Screen compatibility overview | Android Developers
  3. Pixplicity | DP/PX converter