CN

Android Tips Round-Up, Part 1

Word count: 826Reading time: 5 min
2014/05/28
loading

Reviewed 2026-10-13: This 2014 list focuses on Android’s View system. Most APIs remain valid. The old Html.fromHtml(String) overload was deprecated in API 24; the notes below add its replacement and correct the descriptions of cache directories, layout inflation, and reverse playback.

For the first Chinese post in this series, I translated Dan Lew’s Android Tips Round-Up. You may already know some of these small SDK tools; I’m writing them down in case a few are new to you. It isn’t a word-for-word translation. I use some of these classes myself, so I’ve added my own notes and explanations where I thought they would help. This English edition includes the API updates below.

Activity.startActivities() builds a launch path in the order specified by an Intent array, useful when entering the middle of a flow while retaining a back path. Usually only the final Activity is created immediately; earlier ones are created when visited by navigating back. Launch modes and Intent flags still affect task behavior.

TextUtils.isEmpty() returns true for null or a zero-length character sequence. A string containing only spaces is not empty by this test.

Html.fromHtml() parses HTML into styled Spanned text for widgets such as TextView. It neither generates HTML nor implements a full browser. From Android 7.0 (API 24), use fromHtml(source, flags); HtmlCompat.fromHtml, added in AndroidX Core 1.1.0, provides a compatibility option, but ignores flags below API 24. For a simple bold span, constructing a Spannable directly avoids HTML parsing. Whether parsing is a bottleneck depends on text size and call frequency.

TextView.setError() displays an error icon and message after input validation fails. Pass null to clear it. The application still supplies the validation rules.

Build.VERSION_CODES defines named API-level constants. Read Build.VERSION.SDK_INT for the device’s running API level, then compare it with a constant such as Build.VERSION_CODES.N before calling an API 24 method. The constant table itself is not the current OS version.

Log.getStackTraceString() converts a throwable’s stack trace to a string for custom logging. It returns an empty string if an UnknownHostException appears in the cause chain, so an empty result does not establish that no exception occurred.

LayoutInflater.from() will be familiar if you write Adapters often. It takes a Context and returns its LayoutInflater; the subsequent inflate(layoutId, parent, attachToRoot) call creates the View. Supplying the actual parent in an Adapter gives the inflated View appropriate LayoutParams.

ViewConfiguration.getScaledTouchSlop() returns the movement distance, in pixels, tolerated before a touch is considered a scroll. Obtain the configuration through ViewConfiguration.get(context) instead of hard-coding one threshold across display densities. Gesture code still performs the displacement comparison.

PhoneNumberUtils.convertKeypadLettersToDigits() maps letters in a phone number to telephone-keypad digits: 1-800-GOOG-411 becomes 1-800-4664-411. It does not provide T9 word prediction or validate whether a number is usable.

Context.getCacheDir() returns the app’s internal cache directory. The external Android/data/<package>/cache/ directory comes from getExternalCacheDir(), which can return null. Cached files may be removed under storage pressure and are removed on uninstall. Handle cache misses and keep persistent data elsewhere; obtain paths from the API instead of constructing them manually.

ArgbEvaluator, available from API 11, interpolates ARGB colors. I hadn’t used this class when I translated the post, so I made a note to come back to it. Its evaluate() interface uses object arguments and returns an object. The original concern about boxing is worth measuring before deciding to write an implementation yourself. From API 21, ValueAnimator.ofArgb() provides a direct color-animation factory; it does not promise boxing-free execution.

ContextThemeWrapper supplies a different theme when creating a View or Dialog. Set the theme before constructing those objects; wrapping a Context does not automatically restyle an existing View tree.

Space, introduced in Android 4.0 (API 14), is a lightweight layout placeholder. Its draw() produces no content, but it still participates in measurement and layout.

ValueAnimator.reverse() plays an animation backward, from its current position if running or from the end if stopped. It does not cancel the animation. Use cancel() for an immediate stop, on the thread running the animation.

More

  1. Part 1
  2. Part 2
  3. Part 3
  4. Part 4
  5. Part 5

Original Article: https://blog.danlew.net/2014/03/30/android-tips-round-up-part-1/

About Me && Blog

Below is my personal intro and related links. I look forward to exchanging ideas with fellow professionals. “When three walk together, one can always be my teacher!”

  1. Blogger Intro
  2. Blog Content Navigation: A guide for my blog content.
  3. Curated Excellent Blog Articles - Android Performance Optimization Must-Knows
  4. Android Performance Optimization Knowledge Planet: The paid community stopped operating in 2026 and no longer accepts new members; this link preserves its history.

One walks faster alone, but a group walks further together.

Scan WeChat QR Code

CATALOG
  1. 1. More
  • About Me && Blog