Matthias
Matthias

Reputation: 5764

can't add Android Material Design Toolbar

I'm trying to follow Google's material design guidelines and therefore I would like to use the new Toolbar instead of the old ActionBar. But how do I set it in my Activity? The MainActivity has two screens that can be swiped left/right.

The setSupportActionBar method is not found and setActionBar requires a different version of the toolbar. Any idea?

package com.bla;

import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import com.bla.ui.TabsPagerAdapter;


public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        // does not work WTF!
        setSupportActionBar(toolbar);

        ViewPager pager = (ViewPager)findViewById(R.id.pager_list_views);
        TabsPagerAdapter pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager(), this);
        pager.setAdapter(pagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(pager);
    }
}

Upvotes: 2

Views: 771

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

The problem is that you are extending FragmentActivity try to change it to AppCompatActivity

Change it as follows

public class MainActivity extends AppCompatActivity {

Don't forget to import

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

Read the this it might help you to understand why :)

Upvotes: 1

karllindmark
karllindmark

Reputation: 6071

public class MainActivity extends FragmentActivity {

You should be extending the AppCompatActivity class, as the FragmentActivity class doesn't have the setSupportActionBar(Toolbar) method. It does however have a setActionBar(Toolbar), but that method expects a non-support-v7 version of the Toolbar to be used.

Upvotes: 2

Related Questions