0

I have the following problem: I need to make shadow for Toolbar. I read that I can do it with the following xml attribute: app:elevation="20dp". I set such a big value just to see if shadow present or no. But as you can see, there are no shadows:

enter image description here

So, I cannot understand what's the matter, as my device has API 28 (>21, where this attribute isn't supported.

Here's the code of my layout:

<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.login.LoginActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:elevation="20dp">

        <TextView
            android:id="@+id/tv_toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="#FFFFFF"
            android:textSize="24sp" />

    </androidx.appcompat.widget.Toolbar>

</androidx.constraintlayout.widget.ConstraintLayout>

How can I solve this problem?

Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43

1 Answers1

0

Have you tried using:

android:elevation="20dp"

Or adding a background color black or gray on the toolbar, to see if that works?

Alex
  • 962
  • 4
  • 15
  • yes, I tried to use this attribute. And what about changing background color - I don't see any sense in it – Sergei Mikhailovskii Jul 18 '19 at 08:40
  • I have just tried using your layout, and switching to android:elevation makes all the difference. One thing that comes to mind is have you declared the theme for you activity in the manifest? (android:theme="@style/AppTheme.NoActionBar") And, have you setSupportActionBar onCreate of your activity? Btw try increasing the height of the elevation to a 100dp for example to see the difference. – Alex Jul 18 '19 at 09:14
  • 1) About theme: should I declare theme for every activity if I have the same theme for all activities and this theme is declared in `application` part of manifest? – Sergei Mikhailovskii Jul 18 '19 at 09:33
  • 2)About setSupportActionBar: yes, I have this lines in my Activity class: `setSupportActionBar(toolbar) supportActionBar?.setDisplayShowTitleEnabled(false)` – Sergei Mikhailovskii Jul 18 '19 at 09:34
  • Activities that don't declare a theme will use the theme from the application part of manifest. – Alex Jul 18 '19 at 09:34
  • 3)About increasing of elevation: When I changed value to 100 unfortunately nothing happened – Sergei Mikhailovskii Jul 18 '19 at 09:35
  • If nothing helps i would refer you to this link: https://stackoverflow.com/questions/26575197/no-shadow-by-default-on-toolbar it uses an old school way to add the shadow below toolbar. See the accepted answer. – Alex Jul 18 '19 at 09:38
  • yes, I have already seen this question, thanks, I believed that I would find any better solution, but it's seemed that no :) My last hope to find the solution was to create top margin for recycler view which is straight below the toolbar, but it also didn't solve the problem – Sergei Mikhailovskii Jul 18 '19 at 09:42