Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setSupportActionBar cannot be applied

I am trying to use toolbar in my app, for this purpose i have create a xml file name my_action_bar

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
    android:id="@+id/my_action_bar_toolbar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/orange"
    android:minHeight="200dp"
    >
<ImageView
         android:id="@+id/imgageView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/dog"/>
</android.support.v7.widget.Toolbar>
</LinearLayout>

This is my MainActivity code

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //action bar toolbar
    Toolbar actionBarToolbar = (Toolbar) findViewById(R.id.my_action_bar_toolbar);
    //important that is set first
    setSupportActionBar(actionBarToolbar);

but it shows an error line on set support action bar set support action bar cannot be applied Error line

How to resolve this error I am using Min API is 11 Any suggestion will be appreciated, Thanks in advance

like image 235
scienticious Avatar asked Nov 26 '22 23:11

scienticious


2 Answers

You need to change your import from,

import android.widget.Toolbar;

to

import android.support.v7.widget.Toolbar;

or

In Androidx you can use

import androidx.appcompat.widget.Toolbar;

This will surely solve the problem.

like image 114
Aritra Roy Avatar answered Nov 29 '22 11:11

Aritra Roy


Change your import to

import android.support.v7.widget.Toolbar;
like image 31
Logic Avatar answered Nov 29 '22 13:11

Logic