Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Preview Stops Working When Adding Tab Item to Tab Layout

I've noticed the preview crashing on many PCs not just mine, but the last problem I'm experiencing is when I add a TabItem inside a TabLayout, and I can't find any solution. I've tried the following:

  • Clean Project, Rebuild Project
  • Invalidate caches and restart
  • Restarting my laptop
  • Checking for any errors in that layout (there is none, just warnings about extracting strings)
  • Checking the material components version (I have the current latest which is 1.8.0)

It might be something dumb that I forgot but right now I can't think of anything. Here's my XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".ui.AdminActivity">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/adminTab"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorColor="@color/befc_blue"
        app:tabSelectedTextColor="@color/befc_blue">


        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/chart"
            android:text="Reports" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/croissant"
            android:text="Hot Potatoes" />
    </com.google.android.material.tabs.TabLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

These are the errors I get: Errors

First Error

Second Error

Third Error

I've tried to add the default material design style to the tab layout but it didn't work

like image 294
Charbel983 Avatar asked Sep 16 '25 21:09

Charbel983


1 Answers

I experienced this too, it seems like it's a known issue with the recommendation to downgrade to 1.17.0. Render Problems with TabLayout and ViewHolder

I did find a workaround, it's not the best solution but I was able to work around the preview breaking by removing the tabs from XML, then adding them in programatically.

 tabLayout.addTab(tabLayout.newTab().setText("Tab1"))
 tabLayout.addTab(tabLayout.newTab().setText("Tab2"))

It made it so I could still see the rest of my preview, which unblocked me. I also think Android Studio focuses on Compose layout fixes vs XML. So not sure if this will get fixed.

like image 190
LeaveItToGrever Avatar answered Sep 19 '25 14:09

LeaveItToGrever