Home » Android » android – No MaterialCardView background in darkview-Exceptionshub

android – No MaterialCardView background in darkview-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have recently moved from material-1.0 to 1.2 (the problem is in place with 1.1 too!)
Now, every thing is ok in Light view, but in Dark view, there is no card background. See the attached pictures to see what I mean.
Dark View
Light View

I am attaching my java and xml files so that you can kindly check.

MainActivity

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

  private Context mContext;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    } else {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
    }
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    String theme = sharedPref.getString("theme", "Default");
//    Toast.makeText(this, theme, Toast.LENGTH_LONG).show();
    if (theme.equals("Dark")) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    } else if (theme.equals("Light")) {
      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
      } else {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
      }
    }

    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();

    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

//    Navigation Drawer
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open,
        R.string.navigation_drawer_close);
    if (drawer != null) {
      drawer.addDrawerListener(toggle);
    }
    toggle.syncState();

    NavigationView navigationView = findViewById(R.id.nav_view);
    if (navigationView != null) {
      navigationView.setNavigationItemSelectedListener(this);
    }
  }


  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    // Handle navigation view item clicks here.
    switch (item.getItemId()) {
      case R.id.nav_manage:
        // Handle the tools action (for now display a toast).
        drawer.closeDrawer(GravityCompat.START);
        Intent intentSetting = new Intent(this, SettingsActivity.class);
        startActivity(intentSetting);
        return true;
      case R.id.about_phocast:
        // Handle the share action (for now display a toast).
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://rudrab.github.io/thePhocast")));
        drawer.closeDrawer(GravityCompat.START);
//        displayToast(getString(R.string.chose_share));
        return true;
      default:
        return false;
    }
  }
}

activity_main.xml

appbar_main.xml

<androidx.coordinatorlayout.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">

  <com.google.android.material.appbar.AppBarLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <com.google.android.material.appbar.MaterialToolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="?attr/colorPrimary"
      android:minHeight="?attr/actionBarSize"
      android:theme="?attr/actionBarTheme"
      android:title="@string/app_name" />

    <com.google.android.material.card.MaterialCardView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="8dp"
      android:layout_marginStart="10dp"
      android:layout_marginBottom="-2dp"
      android:background="?attr/colorPrimary"
      android:theme="@style/Theme.MaterialComponents.DayNight"
      app:cardCornerRadius="4dp"
      app:cardElevation="4dp">

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <ImageView
          android:layout_width="match_parent"
          android:layout_height="80dp"
          android:contentDescription="@string/cityimg_desc"
          android:scaleType="fitXY"
          app:srcCompat="@drawable/property_image_3" />



          <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/myrect"
            android:elevation="8dp"
            android:paddingStart="@dimen/activity_horizontal_margin"
            android:paddingEnd="0dp"
            android:text="@string/location_not_found"
            android:textColor="#ffffff"
            android:textSize="20sp" />

      </LinearLayout>
    </com.google.android.material.card.MaterialCardView>
  </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

style.xml=style-night.xml

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>

  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />

</resources>

And I cant find out where the error is.

Kindly help/

Update: after @Soumik and @Gabriele’s post
My updated theme is:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
  android:theme="@style/Theme.MaterialComponents.DayNight"
  tools:context=".MainActivity">

  <com.google.android.material.appbar.AppBarLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.appbar.MaterialToolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="@color/colorPrimary"
      android:minHeight="?attr/actionBarSize"
      android:theme="?attr/actionBarTheme"
      android:title="@string/app_name" />

    <com.google.android.material.card.MaterialCardView
      style="@style/Widget.MaterialComponents.CardView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="8dp"
      android:layout_marginStart="10dp"
      android:layout_marginBottom="-2dp"
      app:cardCornerRadius="4dp"
      app:cardBackgroundColor="@color/colorPrimary"
      app:cardElevation="4dp">

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">

        <ImageView
          android:layout_width="match_parent"
          android:layout_height="80dp"
          android:contentDescription="@string/cityimg_desc"
          android:scaleType="fitXY"
          app:srcCompat="@drawable/property_image_3" />


        <TextView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/myrect"
          android:elevation="8dp"
          android:paddingStart="@dimen/activity_horizontal_margin"
          android:paddingEnd="0dp"
          android:text="@string/location_not_found"
          android:textColor="#ffffff"
          android:textSize="20sp" />

      </LinearLayout>
    </com.google.android.material.card.MaterialCardView>
  </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

enter image description here

The result of the changed xml file is updated, and a screenshot is added. Kindly check.

How to&Answers:

Use

    app:cardBackgroundColor="@color/colorPrimary"

instead of

    android:background="?attr/colorPrimary"