Manish Tiwari
Manish Tiwari

Reputation: 1866

android.support.v7.widget.toolbar is not found in java file using android studio?

I have created a toolbar xml file and want to add in MainActivity.

i have include toolbar.xml file in MainActivity layout file

Toolbar.xml code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/dark_blue">

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

Main Activity Xml Layout :

<?xml version="1.0" encoding="utf-8"?>
<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="hogwart.harrypotter.bucketdrop.ActivityMain">

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

Then i want to add toolbar to main activity via java code but here it shows one toolbar :

import android.widget.Toolbar;

But support toolbar is not found :

import android.support.v7.widget.Toolbar;

Main Activity Code:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toolbar;

public class ActivityMain extends AppCompatActivity {

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

Gradle file code:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "hogwart.harrypotter.bucketdrop"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.0'
}

Upvotes: 3

Views: 56625

Answers (9)

canerkaseler
canerkaseler

Reputation: 7468

Maybe, you are using AndroidX. Please check it. Also, you should update your libraries for AndroidX. For this you should use 29 version.

compileSdkVersion 29

And if you want to use Toolbar add this on your gradle file (Module:app):

implementation 'com.google.android.material:material:1.1.0'

Use this Toolbar code in your XML file:

<androidx.appcompat.widget.Toolbar />

Upvotes: 3

Mohamed G. Yaseen
Mohamed G. Yaseen

Reputation: 131

Wherever you want to use it in XML or in java instead of android.support.v7.widget.Toolbar use androidx.appcompat.widget.Toolbar and it should work just fine

Upvotes: 13

Komarov Andrey
Komarov Andrey

Reputation: 11

I have a same problem and i fix it by correct paths:

my toolbar.xml was placed in values/toolbar.xml, and correctm path is the: layout/toolbar.xml

Upvotes: 0

Anton Makov
Anton Makov

Reputation: 845

If you are trying to convert support views like ConstraintLayout ViewPager from support v4/v7 to androidx and you don't know the package name of every view, just press Crtl + space

Example

     <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

             ...

</android.support.constraint.ConstraintLayout>

All you need to do is to delete the package name of the view (in this case android.support.constraint) and just put the cursor after the name ConstraintLayout and press Crtl + space and it will auto complete the package for you.

To this:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">


   ...
</androidx.constraintlayout.widget.ConstraintLayout>

This solution might be trivial for some people but I figured it would be helpful for others that don't know this.

Upvotes: 0

fshkntu
fshkntu

Reputation: 21

replace import android.support.v7.widget.Toolbar; with import androidx.appcompat.widget.Toolbar;.

Upvotes: 2

Joel Min
Joel Min

Reputation: 3457

Add the following dependency in your gradle:

compile 'com.android.support:appcompat-v7:23.1.1'

This library has the support toolbar you are looking for. If this still does not solve the problem, try invalidating cache and restart android studio.

Upvotes: 4

M. M. Human
M. M. Human

Reputation: 118

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

Reminder: Please check maybe your theme is selected to "NoActionBar" try changing it to AppTheme

enter image description here

Upvotes: 0

Gundu Bandgar
Gundu Bandgar

Reputation: 2603

Add below code in your dependency.

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.0.1'
}

And this toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:local="http://schemas.android.com/apk/res-auto"
 android:id="@+id/toolbar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:minHeight="?attr/actionBarSize"
 android:background="#1b5e20"
 local:theme="@style/ThemeOverlay.AppCompat.Dark"
 local:popupTheme="@style/Theme.AppCompat.Light.NoActionBar"
>

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

Upvotes: 6

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

You can add Proper App Compact V7 support support library dependency to your application via build.gradle file.

You should call

 compile 'com.android.support:appcompat-v7:23.1.1'

Then

  1. Clean project
  2. Rebuild project
  3. Sync Gradle

Upvotes: 0

Related Questions