0

I've been trying to use the new design library and I have a toolbar instead of deprecated action bar. The problem is that the toolbar has no elevation. I added elevation everywhere in layouts, in code, I even added AppBarLayout. Nothing works. I'm testing both on Lollipop and Kitkat.

Any help is highly appreciated.

my style.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <item name="windowActionBar">false</item>

        <!-- Customize your theme here. -->
    </style>

</resources>

activity_main.xml

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
                                        android:id="@+id/drawer_layout"
                                        android:layout_width="match_parent"
                                        android:fitsSystemWindows="true"
                                        android:layout_height="match_parent">
    <RelativeLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        <include
                android:elevation="7dp"
                android:id="@+id/tool_bar"
                layout="@layout/app_bar"
                ></include>
    </RelativeLayout>
    <android.support.design.widget.NavigationView
            android:id="@+id/nav_draw"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/drawer" />
    </android.support.v4.widget.DrawerLayout>

main activity :

 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {



Toolbar toolbar;
View root;
NavigationView nav_draw;
DrawerLayout drawer_layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupToolbar();

    drawer_layout = (DrawerLayout)findViewById(R.id.drawer_layout);
    nav_draw = (NavigationView) findViewById(R.id.nav_draw);
    nav_draw.setNavigationItemSelectedListener(this);

getSupportFragmentManager().beginTransaction()
        .replace(R.id.container, new Fragment())
        .commit();

}
private void setupToolbar(){
    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setElevation(18);
    final ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    ab.setDisplayHomeAsUpEnabled(true);
}
}

app_bar layout file

<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/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="7dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
Fatima
  • 869
  • 10
  • 35
  • 1
    What version of SDK you use? If you want elevation on Pre Lollipop sdk you should check this: http://blog.grafixartist.com/add-a-toolbar-elevation-on-pre-lollipop/ or this: http://stackoverflow.com/questions/26743325/appcompat-v21-toolbar-elevation-pre-lollipop – Michele Lacorte Oct 05 '15 at 14:41
  • For pre-lollipop devices, elevation attribute doesn't work. You may want to add a custom shadow view below your toolbar. – Vinit Shandilya Oct 05 '15 at 14:45
  • sdk version: 23 de[endencies : compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:22.2.0' – Fatima Oct 05 '15 at 15:00
  • as I said in the question I'm testing it on a Lollipop virtual device too. – Fatima Oct 05 '15 at 15:01
  • `android:elevation="7dp"` -> `app:elevation` – njzk2 Oct 05 '15 at 15:32

1 Answers1

0

I tested on a physical Lollipop device and it works out. I don't know why it does not work on the virtual device though.

Fatima
  • 869
  • 10
  • 35