Search code examples
javaandroidandroid-studioviewtoolbar

How to use setSupportActionBar function in android studio?


I've created toolbar in xml layout. Then I've initialized it in java with the following code:

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;    

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

    toolbar = findViewById(R.id.toolbar);
    //setSupportActionBar(Toolbar toolbar);
}}

As you can see in the code above I tried to setSupportActionBar(Toolbar toolbar);, but I got an error. Also I've tried write it as setSupportActionBar(toolbar); and both didn't work.

What is way to use this function correctly? Which arguments should I put here?


Solution

  • Actually I've found an answer. If you have the same problem, then you should put import android.support.v7.widget.Toolbar; instead of import android.v7.Toolbar;