Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar

I have a toolbar.xml:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_primary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />

I have an include tag to another of my activity layouts:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme">

<include
    layout="@layout/toolbar"
    android:id="@+id/tb">
</include>

<android.support.v7.widget.RecyclerView
    android:id="@+id/grade_list"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" />

And I have it implemented in my activity which extends AppCompatActivity:

Toolbar toolbar;
    toolbar = (Toolbar)findViewById(R.id.tb);
    setSupportActionBar(toolbar);

However, I am getting the following error when I run my app:

Error:(26, 29) error: incompatible types: android.widget.Toolbar cannot be converted to android.support.v7.widget.Toolbar

Furthermore, when I view my layout in the editor, I receive the following message:

Missing styles. Is the correct theme chosen for this layout?
Use the theme combo box above the layout to choose a different layout, or to fix the theme style references.
Failed to find style 'toolbarStyle' in current layout.

And my styles.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/color_primary</item>
    <item name="colorPrimaryDark">@color/color_primaryDark</item>
    <item name="colorAccent">@color/color_accent</item>
    <item name="colorControlNormal">@color/color_accent</item>
</style>

I don't see where I am going wrong. The correct theme is selected in the editor and my manifest file. All help is appreciated as always!

like image 212
mlz7 Avatar asked Aug 04 '15 00:08

mlz7


People also ask

How do I set up a support action bar?

To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.

What is Setsupportactionbar?

Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.

What is Toolbar Android?

In Android applications, Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar.


3 Answers

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.

like image 158
Gennadii Saprykin Avatar answered Oct 20 '22 08:10

Gennadii Saprykin


if your project is already migrated to AndroidX then import this
import 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");
}
}
like image 20
Faisal Amin Avatar answered Oct 20 '22 09:10

Faisal Amin


android.support.v7.widget.Toolbar toolbar;


toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
like image 4
Shubham Shendre Avatar answered Oct 20 '22 10:10

Shubham Shendre