3

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Ghasem
  • 14,455
  • 21
  • 138
  • 171

3 Answers3

3

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

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
3

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>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • What if I don't have a `build.gradle`. I'm using Intellij Idea and I didn't use Gradle. How can I compile now? – Ghasem Jan 28 '16 at 06:41
  • Check these links: https://www.jetbrains.com/idea/help/working-with-gradle-projects.html or https://www.jetbrains.com/idea/help/creating-a-gradle-project.html or https://www.jetbrains.com/idea/help/gradle-2.html otherwise you have to extract the classes and the resources from the aar but it is not so simple – Gabriele Mariotti Jan 28 '16 at 06:53
0

Change your Import

import android.support.v7.app.ActionBar;

insted of

import android.app.ActionBar;

and use

public class MainActivity extends AppCompatActivity {
   // ...
}
Ghasem
  • 14,455
  • 21
  • 138
  • 171
Pitty
  • 1,907
  • 5
  • 16
  • 34