AndroidのConstraintLayoutでカレンダーの曜日を表示する


#### 1. カレンダーのベースレイアウト

カレンダーのベースになるレイアウトを組みます。
ConstraintLayoutを利用するとこのような形になると思います。

```activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/sunday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sun"
            app:layout_constraintEnd_toStartOf="@id/monday"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/monday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mon"
            app:layout_constraintEnd_toEndOf="@id/tuesday"
            app:layout_constraintStart_toEndOf="@+id/sunday"
            app:layout_constraintTop_toTopOf="@+id/sunday" />

        <TextView
            android:id="@+id/tuesday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tue"
            app:layout_constraintEnd_toEndOf="@id/wednesday"
            app:layout_constraintStart_toEndOf="@+id/monday"
            app:layout_constraintTop_toTopOf="@+id/sunday" />

        <TextView
            android:id="@+id/wednesday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Wed"
            app:layout_constraintEnd_toEndOf="@id/thursday"
            app:layout_constraintStart_toEndOf="@+id/tuesday"
            app:layout_constraintTop_toTopOf="@+id/sunday" />

        <TextView
            android:id="@+id/thursday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Thu"
            app:layout_constraintEnd_toEndOf="@id/friday"
            app:layout_constraintStart_toEndOf="@+id/wednesday"
            app:layout_constraintTop_toTopOf="@+id/sunday" />

        <TextView
            android:id="@+id/friday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fri"
            app:layout_constraintEnd_toEndOf="@id/saturday"
            app:layout_constraintStart_toEndOf="@+id/thursday"
            app:layout_constraintTop_toTopOf="@+id/sunday" />

        <TextView
            android:id="@+id/saturday"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sat"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/friday"
            app:layout_constraintTop_toTopOf="@+id/sunday" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

```

#### 2. 起動するとエラーに...

```sh
06/19 21:13:34: Launching 'app' on Nexus 7 API 29.
$ adb shell am start -n "com.example.myfavoritecontentsmanage/com.example.myfavoritecontentsmanage.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Connecting to com.example.myfavoritecontentsmanage
Waiting for application to start debug server
Waiting for application to come online: com.example.myfavoritecontentsmanage | com.example.myfavoritecontentsmanage.test
Connecting to com.example.myfavoritecontentsmanage
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/System.out: Debugger has connected
    waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8621', transport: 'socket'
I/chatty: uid=10133(com.example.myfavoritecontentsmanage) identical 2 lines
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/chatty: uid=10133(com.example.myfavoritecontentsmanage) identical 2 lines
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1478)
W/RenderThread: type=1400 audit(0.0:61): avc: denied { write } for name="property_service" dev="tmpfs" ino=7222 scontext=u:r:untrusted_app:s0:c133,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.example.myfavoritecontentsmanage
D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
W/econtentsmanag: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
    Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myfavoritecontentsmanage, PID: 17479
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfavoritecontentsmanage/com.example.myfavoritecontentsmanage.MainActivity}: android.view.InflateException: Binary XML file line #2 in com.example.myfavoritecontentsmanage:layout/activity_main: Binary XML file line #2 in com.example.myfavoritecontentsmanage:layout/activity_main: Error inflating class layout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.view.InflateException: Binary XML file line #2 in com.example.myfavoritecontentsmanage:layout/activity_main: Binary XML file line #2 in com.example.myfavoritecontentsmanage:layout/activity_main: Error inflating class layout
     Caused by: android.view.InflateException: Binary XML file line #2 in com.example.myfavoritecontentsmanage:layout/activity_main: Error inflating class layout
     Caused by: java.lang.ClassNotFoundException: android.view.layout
        at java.lang.Class.classForName(Native Method)
        at java.lang.Class.forName(Class.java:454)
        at android.view.LayoutInflater.createView(LayoutInflater.java:815)
        at android.view.LayoutInflater.createView(LayoutInflater.java:776)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:913)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:930)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:950)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:659)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.myfavoritecontentsmanage.MainActivity.onCreate(MainActivity.kt:10)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.layout" on path: DexPathList[[zip file "/data/app/com.example.myfavoritecontentsmanage-hqrGh-NkgwYS5HW_DyEa5g==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.myfavoritecontentsmanage-hqrGh-NkgwYS5HW_DyEa5g==/lib/x86, /system/lib, /system/product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        	... 31 more

```
上記のようなエラーが出てしまいました....

#### 3. エラーに対する対応
app配下のbuild.gradleを下記のように修正します。

```build.gradle
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.myfavoritecontentsmanage"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    //　こちらを追加
    dataBinding {
        enabled = true
    }
}
```

#### 4. できた画面

![](https://i.imgur.com/ZpVNPYl.png)

曜日を表示することができました！

メモ
16日目