Search code examples
javaandroidxmlandroid-fragmentsandroid-toolbar

AppCompat Toolbar elevation missing (with RecyclerView) despite setting properties


For some reason, my Toolbar won't cast a shadow for elevation. I even tried in Java but it's still not working.

Any ideas on why this isn't performing as expected? My app doesn't support pre-Lollipop (minimum API is 24).

activity XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/masterToolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize">

        <include layout="@layout/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"/>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/master_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

toolbar XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    android:elevation="4dp"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:minHeight="?android:attr/actionBarSize">

    <LinearLayout
        android:id="@+id/mytoolbar_textlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/mytoolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@android:style/TextAppearance.Material.Widget.ActionBar.Title"/>
    </LinearLayout>
</android.support.v7.widget.Toolbar>

activity class

public class MyActivity extends AppCompatActivity {

    private Boolean mCurrentValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.md);
    }

    @Override
    protected void onStart() {
        super.onStart();

        setContentView(R.layout.md);

        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        Boolean mNewValue = mSharedPreferences.getBoolean("my_preference", false);

        if (mCurrentValue != mNewValue) {
            recreate();
        }

        // ... do other stuff here
        Resources r = getBaseContext().getResources();
        int fourDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, r.getDisplayMetrics());

        final String toolbarColour = "#EE7C0E";

        Toolbar mMasterToolbar = findViewById(R.id.my_toolbar);
        setSupportActionBar(mMasterToolbar);
        mMasterToolbar.setBackgroundColor(Color.parseColor(toolbarColour));
        mMasterToolbar.setElevation(fourDp);
        mMasterToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        final TextView mTitle = this.findViewById(R.id.toolbar_1line_title);
        mTitle.setText(getString(R.string.london_overground));

        FragmentMain newFragment = new FragmentMain();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.master_container, newFragment);
        transaction.commit();
    }
}

Solution

  • You are missing a android:background in your Toolbar.

    Example:

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_toolbar"
        android:elevation="4dp"
        android:background="?attr/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:minHeight="?android:attr/actionBarSize">
    

    Note: In order to apply elevation to any view, it needs to have a background.


    EDIT: Since your app has a min SDK 24 you can use the translationZ attribute.