Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to find style 'floatingActionButtonStyle' in current theme

I'm newbie to android development so i start the simple project that came with android studio "hello world " but it give this problem :

Failed to find style 'floatingActionButtonStyle' in current theme I have android studio 3.1.3 x86

also my SDK Version from the build.gradle file is 28

and this is the activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

and this is my content_main.xml:

        <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MainActivity"
        tools:showIn="@layout/activity_main">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

and this is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
like image 322
zerocool Avatar asked Jun 26 '18 16:06

zerocool


2 Answers

Checking the source of FloatingActionButton one can see that the constructor has been changed to

public FloatingActionButton(Context context, AttributeSet attrs) {
    this(context, attrs, R.attr.floatingActionButtonStyle);
}

There's new floatingActionButtonStyle which seems not included by default in 'com.android.support:appcompat-v7:28.0.0-alpha1'

The best workaround I found (based on similar case here) is to define the attribute in your theme (maybe only in debug version), this way the error is gone as @style/Widget.Design.FloatingActionButton is already defined in the support library so you don't need to create the style, just reference it inside your AppTheme style in your values/styles.xml file.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="floatingActionButtonStyle">@style/Widget.Design.FloatingActionButton</item>
</style>

Be sure to use AppTheme in the layout preview and clean build.

like image 139
Rami Alloush Avatar answered Oct 16 '22 07:10

Rami Alloush


It could be a "false" error about the style: check if you the setOnClickListener writed for the correct activity where you have this issue. Maybe you've writed the listener for this button to the wrong activity. You can easily test it by comment temporarely the code about setOnClickListener and re-launching the build..

like image 1
Alessandro Ornano Avatar answered Oct 16 '22 06:10

Alessandro Ornano