Reputation: 79
I have made TabLayout in XML file of for an android application. Like that
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabs">
</android.support.design.widget.TabLayout>
Now I am trying to access TabLayout in java file, but I am unable to do so.Please see attached snapshot and help me that where I am mistaking.Please help me.
Upvotes: 0
Views: 925
Reputation: 4344
To use TabLayout The first thing you need to do is include the Design Support Library in the dependencies of the app’s build.gradle file:
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:design:25.3.1'
In the activity layout (activity_tab_layout.xml) we place a TabLayout widget and a ViewPager:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.voidynullness.android.tabitytabs.TabLayoutActivity">
</android.support.v4.view.ViewPager>
now inside respective activity say TabLayoutActivity.java add this below code
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
public class TabLayoutActivity extends AppCompatActivity {
TabLayout tabs ;
ViewPager pager;
TabsPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);
tabs = (TabLayout) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new TabsPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);
}
}
and run it will work for you
Upvotes: 2