techker
techker

Reputation: 199

Android Tiles for wear unclear debug preview

I started to update our wear module to be up to date with latest guidelines of Google Play. Since I'm doing so, I decided to add the Tiles API to the app. When ready for public release it will already be implemented. Doc: https://developer.android.com/training/articles/wear-tiles#preview

Very unclear... Does anybody understand this?

The wear-tiles-renderer library provides a way to preview Tiles in an activity within your app. To preview your Tile, create an activity that uses the renderer library to render the Tile. Add this activity in src/debug instead of src/main, as you’ll use this activity only for debugging purposes. See the following code sample for an example:

I tried adding the example code to the debug folder manually since I can't add it from Android Studio.

Added the xml file in the main and also added in debug folder to test.

When I load the app it opens my main file of folder src/main but freezes.

Do I need to add any code to load the example if in debug?

Upvotes: 4

Views: 564

Answers (3)

Yuri Schimke
Yuri Schimke

Reputation: 13488

If you split your TileService and Tile layout code using Horologist

You can use Android Studio previews for this

@WearSmallRoundDevicePreview
@WearLargeRoundDevicePreview
@Composable
fun Run() {
    val context = LocalContext.current
    LayoutRootPreview(
        Run.layout(
            context,
            context.deviceParams(),
            lastRunText = "2 days ago",
            startRunClickable = emptyClickable,
            moreChipClickable = emptyClickable
        )
    )
}

See the example here https://github.com/android/wear-os-samples/blob/main/WearTilesKotlin/app/src/debug/java/com/example/wear/tiles/golden/GoldenTilesPreviewsRow1.kt

Upvotes: 1

Yuri Schimke
Yuri Schimke

Reputation: 13488

In Android Studio Dolphin you can now create a Launch configuration without the preview

enter image description here

Upvotes: 2

TofferJ
TofferJ

Reputation: 4784

What the quote from the documentation is trying to explain is that you need a module that looks something like this:

folder structure

Your actual tile, and everything it needs, goes in the main folder. Then in the debug folder you need the following three things:

Manifest - Pretty standard

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tiles">

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:label="@string/tiles_app_name">

        <activity
            android:name=".MainActivity"
            android:label="@string/tiles_app_name"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Activity - The important thing here is the TileManager

class MainActivity : ComponentActivity() {
    private lateinit var tileManager: TileManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.tile_test)
        val rootLayout = findViewById<FrameLayout>(R.id.tile_container)
        tileManager = TileManager(
            context = this,
            component = ComponentName(this, SampleTile::class.java),
            parentView = rootLayout
        )
        tileManager.create()
    }

    override fun onDestroy() {
        super.onDestroy()
        tileManager.close()
    }
}

Layout - Also pretty standard

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tile_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

If you have all of this set up and it's still freezing, you probably have a bug in your Tile class. See if there are any error messages in Logcat that can help you understand what is going wrong.

Upvotes: 2

Related Questions