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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With