Menu

A tiny template language for widget data, and the Android regex trap that bit me
πŸ“°

A tiny template language for widget data, and the Android regex trap that bit me

DEV Community: androidΒ·Mohammad DallahΒ·2 days ago
#fXm0kqU0
#devtoandroid#devto#dev#each#end
Reading 0:00
15s threshold

An Android widget cannot render JSON. RemoteViews takes a TextView , and a TextView takes a string. So somewhere between "here is a nested object" and "here is a 60-character line", you need a formatting step.

Most widget apps solve this with a visual editor, or with code, or with Tasker. I wanted the third option nobody offers: a format string you can type on a phone keyboard in ten seconds.

This post is the design of that little language, and the two mistakes I made building it.

The constraint

The format has to do five things, and nothing else:

Pick a value out of a nested document: {main.temp}

Project a list onto lines: {stations.*.brand}

Repeat a row per item: #each orders … #end

Do one arithmetic operation, because APIs return cents: {price / 1000}

Lay out multiple lines: \n , \t

Everything else β€” loops with accumulators, functions, conditionals over arrays β€” is out of scope.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.