Snippets

DeviantDev Android Wear: Scrollable Confirmation Activity with manually calculated inset/padding

Created by Hendrik Schuster last modified
package com.deviantdev.wear.configuration

import android.app.Activity
import android.content.res.Resources
import android.os.Bundle
import com.deviantdev.wear.R
import kotlinx.android.synthetic.main.activity_confirmation.*

class ConfirmationActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_confirmation)

        button_cancel.setOnClickListener {
            setResult(Activity.RESULT_CANCELED)
            finish()
        }

        button_ok.setOnClickListener {
            setResult(Activity.RESULT_OK)
            finish()
        }

        adjustInset()
    }

    private fun adjustInset() {
        if (applicationContext.resources.configuration.isScreenRound) {
            val inset = (FACTOR * Resources.getSystem().displayMetrics.widthPixels).toInt()
            layout_content.setPadding(inset, inset, inset, inset)
        }
    }

    companion object {
        val TAG: String = ConfigActivity::class.java.simpleName!!

        private const val FACTOR = 0.146467f // c = a * sqrt(2)
    }
}
<?xml version="1.0" encoding="utf-8"?>

<!-- Related blog post: https://www.journal.deviantdev.com/?p=917 -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:showDividers="middle">

        <TextView
            android:id="@+id/layout_question"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/short_text_question"
            android:textAlignment="center"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/text_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:text="@string/long_text_description"
            android:textAlignment="center"
            android:textSize="14sp" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp">

            <android.support.wearable.view.CircledImageView
                android:id="@+id/button_ok"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|bottom"
                android:src="@drawable/ic_check_24dp"
                app:circle_color="#0EB695"
                app:circle_radius="25dp"
                app:circle_radius_pressed="20dp" />

            <android.support.wearable.view.CircledImageView
                android:id="@+id/button_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|end"
                android:src="@drawable/ic_close_24dp"
                app:circle_color="#AFAFAF"
                app:circle_radius="25dp"
                app:circle_radius_pressed="20dp" />
        </FrameLayout>
    </LinearLayout>

</ScrollView>

Comments (0)