D
D
Dima2312019-10-03 17:23:12
Java
Dima231, 2019-10-03 17:23:12

Android. How to use the toolbar in an application?

Hello! I recently started learning Android development. I'm still bad at it. I want to make a toolbar in the application. I have an application on API 14. And the toolbar appeared with API 21. That is. I need to add Support Library. But I can't figure out how to do it. Here is the dependency text

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.toolbar"
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:support-core-utils:28.0.0'
}

The last line swears, requires AndriodX. And I can’t find how to change this line for AndroidX.
In MainActivity I have this code
package com.example.toolbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toolbar;
//import android.support.v7.app.AppCompatActivity; //На эти две строки тоже ругается
//import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); // Я хочу чтобы работала эта функция
//но на нее ругается
    }
}

Please tell me how to write dependencies and imports correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AndroidRS, 2019-10-03
@AndroidRS

The Support Library is currently out of date, instead, as the studio suggested, you need to use AndroidX
Accordingly, the toolbar is imported from the package:
androidx.appcompat.widget.Toolbar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question