Daily life with Jetpack Compose part 1

How to achieve simple tasks with Jetpack Compose series. Simple tasks with plain Android stayed mostly the same with minor API changes, but there are no official documentation on this subject.

How to display a web web in a Composable

In your code

import android.webkit.WebView
import android.webkit.WebViewClient

@Composable
fun createWebView(article: Post, modifier: Modifier) {
    val context = ContextAmbient.current
    val webViewClient = WebViewClient()
    AndroidView(
        viewBlock = {
            WebView(context).apply {
                this.webViewClient = webViewClient
                this.loadUrl("https://hanquoc.kr/${article.slug}")
            }
        },
        modifier = modifier
    )
}

Don’t use the androidx version – it’s useless

How to share a URL with Jetpack Compose

In your code

import android.content.Intent

@Composable
fun ArticleView(
    article: Post
) {
    val context = ContextAmbient.current
    val shareIntent = Intent(Intent.ACTION_SEND)
    shareIntent.type = "text/plain"
    shareIntent.putExtra(Intent.EXTRA_TEXT, "https://hanquoc.kr/${article.slug}")
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, article.title.rendered)
    startActivity(
        context,
        Intent.createChooser(shareIntent, null),
        null
    )
}

Broken Vietnamese on Android

I was using Liberty ROM (android 2.3.3) on my Droid X and Vietnamese looks broken (all the diacritical marks are mixed up: acute become grave the press goes above the character instead of below), while the same characters displayed just fine on another phone.

I tried to install the new Roboto font from Android 4.0 via ROM Toolbox but it doesn’t fix the problem.

So I tried to copy the fonts from my other phone running Android 2.3.5 and now everything’s fine. Extract the font file, mount / as writable in ES File Explorer, copy the files to /system/fonts and you are set 🙂

Here are the font files (I guess they are free to distribute since they comes from AOSP)

Resolving ClassCastException using ADT’s visual designer

Recently when maintaining an old android program I encountered a strange exception when attempting to view a layout in visual designer:

error!
ClassCastException: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
Exception details are logged in Window > Show View > Error Log
The following classes could not be found:
- TableLayout (Change to android.widget.TableLayout, Fix Build Path, Edit XML)

Searching stack overflow and Android developers, I found this could be caused by using an old version SDK for an application targeted to a newer version, with new elements. But TableLayout was available since API level 1, and I’m using the latest version of the development tools, so these could not be the problem. The temporary fix was to remove the table layout and replace it with something else.

Today I have the time to compare my code to Android’s example. It turned out to be one tiny little thing that messed the designer up:

<TableLayout

			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical"
			android:stretchColumns="">

There, one attribute doesn’t match the schema (it should be 0 or 1, not empty “”), and the designer will refuse to load. Worse still, the code will compile but will crash at run-time for the same reason.

My recommendation? Android team should note this case and provide better details on the loading exception in upcoming versions. “ClassCastException” is not really helpful!

 

Debugging the Galaxy Tab 10.1

When I first plug the tab in, my computer automatically install all the drivers, but it won’t show up in ADB and Eclipse. I did a little searching but people were pointing me to Samsung’s developer page with a USB driver for phones. Sadly that driver just isn’t for the tab, a search on their site found nothing interesting either. It looks like Samsung can’t keep information for developers updated while they are still battling legal issues around the tab.

Fortunately, I remembered I haven’t turned debugging mode on the tab on, so I went ahead and do it, this time… Success!

Where to turn on debugging

Device in Eclipse

Now I’m installing Android 3.1 platform and looking forward for a happy time debugging 🙂