1

I am trying to add image and background to a floating action button in the coordinator layout but I am not getting any image preview and background-color

fragment_task.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view_tasks"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_add"
        app:tint="@color/teal_200"

        android:contentDescription="@string/todo" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Here is my build.gradle maybe this can help identify the problem

build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.codinginflow.mvvmtodo"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
        freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
    }
}

dependencies {
    // Default dependencies
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    implementation "androidx.core:core-ktx:$ktxVersion"
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "com.google.android.material:material:$materialVersion"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$testExtJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espressoVersion"

    // Fragment
    implementation "androidx.fragment:fragment-ktx:$fragmentVersion"

    // Lifecycle + ViewModel & LiveData
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
    implementation "android.arch.lifecycle:common-java8:$lifecycleVersion"

    // Navigation Component
    implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
    implementation "androidx.navigation:navigation-ui-ktx:$navigationVersion"

    // Room
    implementation "androidx.room:room-runtime:$roomVersion"
    kapt "androidx.room:room-compiler:$roomVersion"
    implementation "androidx.room:room-ktx:$roomVersion"

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"

    // Dagger Hilt
    implementation "com.google.dagger:hilt-android:$hiltVersion"
    kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hiltAndroidXVersion"
    kapt "androidx.hilt:hilt-compiler:$hiltAndroidXVersion"

    // DataStore
    implementation "androidx.datastore:datastore-preferences:$dataStoreVersion"

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
}

kapt {
    correctErrorTypes true
}

XML preview

Floating button is at the bottom end

Tell me which other files do you need and I will edit the question.

batman_x
  • 23
  • 6

4 Answers4

2

try app:srcCompat for image and app:backgroundTint for background colour

Muhammad Ahmed
  • 1,038
  • 1
  • 7
  • 7
2

Have you tried app:icon, at least it works with ExtendedFloatingActionButton

J.Stange
  • 478
  • 3
  • 8
1
    android:backgroundTint="@color/design_default_color_error"
    app:srcCompat="@drawable/ic_baseline_add_24"
    app:tint="@color/teal_200"

You need to add this namespace in your parent layout.

 xmlns:app="http://schemas.android.com/apk/res-auto"

enter image description here

Note: if you choose a black background and don't change the colour of the icon using tint it will be black by default and you will not able to see the icon

Dinkar Kumar
  • 2,175
  • 2
  • 12
  • 22
  • It didn't work, I uploaded an image preview take a look, please. – batman_x May 17 '21 at 08:39
  • 1
    can you also please provide the xml which rendered your new image – Dinkar Kumar May 17 '21 at 08:43
  • 1
    you uploded the image where your floating icon is showing white squared view , so you must have changed something in XML, so can you share the updated XML as well – Dinkar Kumar May 17 '21 at 08:47
  • I think it's a bug bcoz when I tried to add a hint in editText it didn't show anything but running fine in the emulator. – batman_x May 17 '21 at 10:42
  • 1
    the solution I provided is working fine when you run on the device? it's only failing in android studio preview? – Dinkar Kumar May 17 '21 at 10:54
  • Yup the solution even I made is running fine in the emulator I found the solution I will update it it's a bug in the material library. – batman_x May 17 '21 at 10:57
  • 1
    are your Android studio and all the dependencies are up to date, because if that is an bug in material design it should have happened to me as well, my Android studio and all the libraries were to the latest version and those are working fine to me as per the solution I shared – Dinkar Kumar May 17 '21 at 10:59
  • Do you have this version of the material library - ```implementation 'com.google.android.material:material:1.3.0'``` ?? – batman_x May 17 '21 at 11:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232511/discussion-between-batman-x-and-dinkar-kumar). – batman_x May 17 '21 at 11:03
  • 1
    yes , implementation 'com.google.android.material:material:1.3.0' – Dinkar Kumar May 17 '21 at 11:07
  • `alpha` versions are most unstable and then comes `beta` which is more stable than `alpha` still a lot of potential bug and issues are there then finally comes the `stable` version. always try to use a stable version till the time you really want to use some feature that is yet in either the `alpha` or `beta` stage only. – Dinkar Kumar May 17 '21 at 11:10
0

The problem was occurred because of a bug and the error was Path.op is not supported

So the solution is to just update the Gradle file with

implementation 'com.google.android.material:material:1.2.0-alpha02'

batman_x
  • 23
  • 6
  • 1
    `alpha` versions are most unstable and then comes `beta` which is more stable than `alpha` still a lot of potential bug and issues are there then finally comes the `stable` version. always try to use a stable version till the time you really want to use some feature that is yet in either the `alpha` or `beta` stage only. Did you tried `implementation 'com.google.android.material:material:1.3.0'` ? – Dinkar Kumar May 17 '21 at 11:11
  • Yes, you are right but it worked for me . – batman_x May 17 '21 at 11:23
  • 1
    and this `implementation 'com.google.android.material:material:1.3.0'` ? – Dinkar Kumar May 17 '21 at 11:24
  • 1
    but apart from the material design version, my answer did helped you right to choose the correct parameters like `tint` `srcCompat` and `backgroundTint` right ? – Dinkar Kumar May 17 '21 at 11:42