Venkatesh Dharavath
Venkatesh Dharavath

Reputation: 520

Cannot resolve TabLayout

Im trying to use TabLayout in my project but I'm unable to import TabLayout class in my project please help me. here is the code.

build.graddle

 dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation "com.google.android.material:material:1.0.0"

}

ive added android.material:material:1.0.0" also by referring the previous question but it didnt work out for me.

here is my xml file.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/white_grey_border_top">


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



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

</RelativeLayout>

actually i was following a tutorial in youtube and it was working for him but not for me.

here is my java class.

package Home;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabLayout; //TabLayout showing red color.`enter code 
here`

import com.example.footag.R;

public class HomeActivity extends AppCompatActivity {

private static final String TAG = "HomeActivity";
private static final int ACTIVITY_NUM = 0;
private Context mContext = HomeActivity.this;

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

private  void setupViewPager(){
    SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new CameraFragment());
    adapter.addFragment(new HomeFragment());
    adapter.addFragment(new ProfileFragment());
    ViewPager viewPager = (ViewPager) findViewById(R.id.container);
    viewPager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);// TabLayout showing red color.
    tabLayout.setupWithViewPager(viewPager);

}

}

Upvotes: 1

Views: 6719

Answers (2)

Ujjwal Jung Thapa
Ujjwal Jung Thapa

Reputation: 644

Or your import might be incorrect. Try this

package Home;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
//import android.widget.TabLayout;
import android.support.design.widget.TabLayout;

Upvotes: 1

Anton Holovin
Anton Holovin

Reputation: 5673

I suggest you perform 2 steps.

Step 1. Migrate to the new AndroidX library instead of using AppCompat library. Use RefactorMigrate to AndroidX. It will replace your dependencies with AndroidX analogs.

Step 2. Use com.google.android.material:material:1.0.0 dependency to get com.google.android.material.tabs.TabLayout class. Use this class in your xml and java files. Package name of ViewPager will be androidx.viewpager.widget.

Example of xml with imports form AndroidX library:

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:layout_alignParentTop="true">

  <com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/white_grey_border_top">

    </com.google.android.material.tabs.TabLayout>
  </com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>

Upvotes: 9

Related Questions