android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar
if your project is already migrated to AndroidX then import thisimport androidx.appcompat.widget.Toolbar;
else migrate to AndroidX by
Refactor>Migrate to AndroidX
Full code Example of XML and Java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".Events.Events_Main_Activity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbarlayout_id"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsingtoolbar_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="8dp"
app:expandedTitleMarginStart="8dp"
app:layout_scrollFlags="exitUntilCollapsed|scroll"
app:title="Anime Title"
app:titleEnabled="true">
<ImageView
android:id="@+id/aa_thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_sec"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="shshhs" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
package com.uafappdevelopers.example;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
//note this import
import androidx.appcompat.widget.Toolbar;
import com.uafappdevelopers.uafportal.R;
public class Events_Second_Activity extends AppCompatActivity {
//add this for the navigation back button click event
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
finish();
return super.onSupportNavigateUp();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_second);
//change id to Your id
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_sec);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
setTitle("University of Agriculture Faisalabad");
}
}
You use android.support.v7.widget.Toolbar
in your XML, but in your java code you are importing android.widget.Toolbar
which is a different type. Change your import to android.support.v7.widget.Toolbar
and it should work.