8

I want to add shadow to ToolBar and I use the following link: ToolBar Shadow

but running app show me this error in LogCat :

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.Toolbar

Toolbar code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:theme="@style/MyCustomToolbarTheme"
    android:background="@drawable/main_header">

    </android.support.v7.widget.Toolbar>

MainActivity XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar_main"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/app_bar"
    android:text="ytkbhjk"/>

MainActivity Java :

public class MainActivity extends ActionBarActivity {

private Toolbar toolbar;

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

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

please help me

Community
  • 1
  • 1
Mohammad
  • 91
  • 1
  • 1
  • 9

4 Answers4

4

You are going to need to make one more xml file which contains your toolbar: Name of this xml is toolbar

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>

Now change your relative layout's include tag like this

<include
    layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

And in your activity, do something like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Vikalp
  • 2,051
  • 4
  • 18
  • 24
2

Where you use the <include /> in your MainActivity XML, you should not put the android:id="@+id/app_bar", but instead in the tag <android.support.v7.widget.Toolbar />.

Leon Joosse
  • 959
  • 1
  • 7
  • 17
1

I have removed relativeLayout instead just use android.support.v7.widget.Toolbar. And ts work.

Aman Srivastava
  • 1,007
  • 1
  • 13
  • 25
0

Try downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.

Vishnu
  • 2,135
  • 2
  • 30
  • 51