Ghasem
Ghasem

Reputation: 15573

getActionBar doesn't work using AppCompat lib

I'm trying to create a TabLayout android app using Intellij Idea and AppCompat v7 library.

import android.support.v7.app.ActionBar;

public class MainActivity extends FragmentActivity implements TabListener { 
...
private ActionBar actionBar;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
actionBar = getActionBar(); //Error line
...

}

When using getActionBar() I'm getting this error:

Incompatibale types:

Required: android.support.v7.app.ActionBar

Found: android.app.ActionBar

I don't have import android.app.ActionBar; in my activity. I tried:

actionBar = android.support.v7.app.ActionBar.getActionBar();

and

actionBar = getSupportActionBar();

But I get

Can't resolve method getActionBar() //Or getSupportActionBar()

How can I use getActionBar() using appCompat library? (Or maybe there is an alternative which I don't know about?)

Edit

I also replaced FragmentActivity with ActionBarActivity in this line:

 public class MainActivity extends FragmentActivity //ActionBarActivity

But got no luck

Upvotes: 3

Views: 746

Answers (3)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363755

First of all, you have to extends AppCompatActivity instead of FragmentActivity.

Then you can use getSupportActionBar().

To do this just import the right dependency:

compile 'com.android.support:appcompat-v7:23.1.1'

But it is very important to chech that TabListener is the wrong way to obtain a tab layout. This interface was deprecated in API level 21.

With the new Design Support Library now you can use the new TabLayout.

Just add this dependency to your build.gradle

compile 'com.android.support:design:23.1.1'

The code is very simple:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

To implement many of the features of material designs you should use it within a CoordinatorLayout and a AppBarLayout.

Something like this:

 <android.support.design.widget.CoordinatorLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">


     <android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

     </android.support.design.widget.AppBarLayout>

     <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

 </android.support.design.widget.CoordinatorLayout>

Upvotes: 3

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Bad Approach

Calling older import android.support.v7.app.ActionBar;

If you are extending AppCompatActivity/FragmentActivity then you are providing backward support for older Android Versions and for that you should have to use getSupportActionBar().

Read getSupportActionBar using FragmentActivity

public class MainActivity extends AppCompatActivity {
  // ...

  ....

 ActionBar actionBar =getSupportActionBar();
}

Add this .

dependencies {
   // … 
   compile 'com.android.support:appcompat-v7:23.1.0'
}

Check Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

AppCompatActivity is from the appcompat-v7 library. Principally, this offers a backport of the action bar. Since the native action bar was added in API Level 11, you do not need AppCompatActivity for that.

For your Information How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA

Upvotes: 3

Pitty
Pitty

Reputation: 1977

Change your Import

import android.support.v7.app.ActionBar;

insted of

import android.app.ActionBar;

and use

public class MainActivity extends AppCompatActivity {
   // ...
}

Upvotes: 0

Related Questions