7

Could someone please explain to me the difference between them and why aren't they interchangeable?

Importing android.widget.Toolbar will cause compilation error whereas importing android.support.v7.widget.Toolbar works perfectly fine.

What's the distinction between these two imports?

import android.widget.Toolbar;

...

Toolbar toolbar = findViewById(R.id.app_bar);

setSupportActionBar(toolbar);

import android.support.v7.widget.Toolbar;

...

Toolbar toolbar = findViewById(R.id.app_bar);

setSupportActionBar(toolbar);
Ajeet Yadav
  • 693
  • 1
  • 11
  • 31
Ong Jin Bin
  • 155
  • 3
  • 7
  • 3
    Possible duplicate of [Difference between support.v7.widgets and android.widgets Toolbar, Fragment](https://stackoverflow.com/questions/34239162/difference-between-support-v7-widgets-and-android-widgets-toolbar-fragment) – John Joe Apr 03 '19 at 09:38

3 Answers3

4

The support library (now AndroidX) is designed for backwards compatibility while android.widget.Toolbar is the current plattform type.
If you don't know what the support library is take a look at this answer.
By default Android Studio makes you use AppCompatActivity which is part of the support library and thusly expects a support toolbar as well.

Vringar
  • 502
  • 1
  • 4
  • 14
1

Why the Compilation error?

Because parameter in the setSupportActionBar(toolbar) is

android.support.v7.widget.Toolbar and not the

android.widget.Toolbar

How to check it?

setSupportActionBar is part of AppCompatActivity. If you want to see the parameter then just override the method in your Activity and remove the import. It will indicate which class should be imported in order to get rid of the Compilation error.

enter image description here

What is the distinction?

It is pretty evident. Both are different classes one belongs to Support Library and the other does not.

Conjugation with AppCompatActivity

All the new features keep getting added in the Support libraries so that you will have the same functionality across all API levels. In other words, support libraries are updated constantly. For example, it is recommended to use android.support.v4.app.Fragment with conjugation with AppcompatActivity.
So compatibility across all the API levels could be a reason.

Point being If your Activity extends AppCompatActivity then use

  • android.support.v7.widget.Toolbar
  • android.support.v4.app.Fragment
  • android.support.v7.widget.RecyclerView etc
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
0

when you see v4 Support Libraries know that it is the android support library for older devices with low API level (backwards compatibility).

These libraries include the largest set of APIs compared to the other libraries, including support for application components, user interface features, accessibility, data handling, network connectivity, and programming utilities.

You can check all the support libraries here

Note - you can also use AndroidX , it is the new and improved support library.

AndroidX is a major improvement to the original Android Support Library.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53