Page 106 - Adding an icon to the NEXT button not working for me

Hi, i am reading the book for a course and the step “Adding an icon to the NEXT button” is not working for me. Here is my code straight from the book.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="24dp"
        tools:text="@string/question_australia" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />
        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />
    </LinearLayout>
    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next_button"
        app:icon="@drawable/baseline_arrow_forward_24"
        app:iconGravity="end" />
</LinearLayout>

But something is hapenning with that new “app” namespace i get the error;

Android resource linking failed
com.bignerdranch.android.geoquiz.app-mergeDebugResources-40:/layout/activity_main.xml:37: error: attribute icon (aka com.bignerdranch.android.geoquiz:icon) not found.
com.bignerdranch.android.geoquiz.app-mergeDebugResources-40:/layout/activity_main.xml:37: error: attribute iconGravity (aka com.bignerdranch.android.geoquiz:iconGravity) not found.
error: failed linking file resources.

I have the latest android studio version (Flamingo 2022.2.1 Patch 1), has something change in 2023 that make this code not work anymore?

This is referring to attributes coming from the Material Design library. I want to make sure your project has that library included in it. Can you share the contents of the dependencies section of the app/build.gradle file?

dependencies {

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.5.1'
    implementation platform('androidx.compose:compose-bom:2022.10.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    implementation 'com.google.android.material:material:1.9.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'
}

Yeah… you ran into an issue that many other readers have run into. You created your project with a template that included Jetpack Compose and didn’t include some of the libraries that you need for GeoQuiz. Android Studio switched the names of some templates (you want to use the template named “Empty Views Activity”) and that has been causing issues for us.

I recommend recreating the project from scratch (using the “Empty Views Activity” template) and going from there.

That is what i did, start from an empty views activity. I found something online saying that “…you are trying to add attribute iconGravity on Button which doesn’t support it . iconGravity is supported by com.google.android.material.button.MaterialButton so if you want to use iconGravity , you have to use MaterialButton
Could that be right?

You do not need to use MaterialButton because once that library is set up, the library handles that work automatically for you.

However, you do need to set up the Material Design library for your project. It is not currently configured. Some good documentation is here: Material Design