-1

Here's my class that extends AppCompatActivity:

class MainActivity : AppCompatActivity() {

    private var TAG: String = this.toString()

I've didn't add it entirely because it has no regions that tweaks the theme or anything, it's just simple coding like findViewById.

Here's the XML that it uses:

<layout>

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:theme="@style/Theme.AppCompat"
    >

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </com.google.android.material.appbar.AppBarLayout>


    <!-- Main content -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/effect_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floating_add_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        app:srcCompat="@drawable/ic_add" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

As you can see, I already added an AppCompat theme. I tried many themes from the many questions and answers here on StackOverflow. If I extend Activity() instead of AppCompatActivity it works but its not the intended solution.

Update:

a solution was to change

    <activity
        android:name="com.github.cythara.MainActivity"
        android:label="Main">
    </activity>

to include a theme:

    <activity
        android:name="com.github.cythara.MainActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar"
        android:label="Main">
    </activity>

why it works?

PPP
  • 1,279
  • 1
  • 28
  • 71
  • if you are using alert dialog, don't pass getApplicationContext as context. I had a similar issue and it was because of using getApplicationContext in dialogs – Amirhossein Apr 30 '20 at 00:41
  • Does this answer your question? [You need to use a Theme.AppCompat theme (or descendant) with this activity](https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity) (see the [second answer](https://stackoverflow.com/a/25663447/208273)) – Ryan M Apr 30 '20 at 01:25
  • @RyanM thanks Ryan but it does not help, I tried all solutions there. In fact my question has one of the solutions – PPP Apr 30 '20 at 04:05
  • @HotJava thanks but I'm not using dialogs – PPP Apr 30 '20 at 04:05
  • @LucasZanella what is the theme of your activity, can you paste your manifest ? – Android_id Apr 30 '20 at 05:26
  • @Android_id there's no theme on this activity on AndroidManifest.xml – PPP May 02 '20 at 22:30
  • What version of `AppCompatActivity` are you using? You're mixing AppCompat "old/support" themes, with Material/Android X themes. You need to use the Bridges or change your AppCompat to use the newer versions (that are AndroidX driven) – Martin Marconcini May 06 '20 at 11:57

2 Answers2

4

If you want to use Material Components with AppCompatActivity you should use the Bridge versions of the MaterialComponents theme e.g Theme.MaterialComponents.Light.NoActionBar.Bridge

Ismail Shaikh
  • 482
  • 4
  • 13
1

I had a similar problem when I tested my fragments with Espresso. I resolved my problem by adding the theme into the test scenario, like this:

val testedScenario = launchFragmentInContainer(themeResId = R.style.Theme_Time_Sheet)
MeLean
  • 3,092
  • 6
  • 29
  • 43