I was following this tutorial https://www.youtube.com/watch?v=zcnT-3F-9JA I've used his code from github, but output is wrong. I've put 3 tabs on the top, and when I press on tab, activity supposed to change, but in reality, nothing happens, I still have only my main_activity on the screen. Hope someone will help. Here is my codes
PS - Yes I have also 3 xml files for every fragment (even I have also main_activity, but I have to find out what's wrong, and then will asign 1tab with main activity). I have also 3 java files for this 3 tabs.
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
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.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<TextView
android:id="@+id/lvltext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/expa"
android:layout_centerInParent="true"
android:fontFamily="@font/futuracondensed"
android:text="@string/leveltext"
android:textColor="@color/black"
android:textSize="30sp" />
<TextView
android:id="@+id/lvlnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lvltext"
android:layout_centerInParent="true"
android:fontFamily="@font/futuracondensed"
android:text="@string/levelnum"
android:textColor="@color/black"
android:textSize="30sp" />
<ImageView
android:id="@+id/girl"
android:layout_width="wrap_content"
android:layout_height="262dp"
android:layout_below="@id/lvlnum"
android:src="@drawable/girl" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/girl"
android:fontFamily="@font/futuracondensed"
android:text="@string/button"
android:textColor="@color/black" />
<ImageView
android:id="@+id/girl2"
android:layout_width="match_parent"
android:layout_height="258dp"
android:layout_below="@id/button"
android:src="@drawable/fitnessmodel" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/girl2"
android:fontFamily="@font/futuracondensed"
android:text="@string/button2"
android:textColor="@color/black" />
<LinearLayout
android:layout_below="@id/tabs"
android:id="@+id/expa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/futuracondensed"
android:gravity="center"
android:paddingBottom="5dp"
android:text="0"
android:textColor="@color/black"
android:textSize="60sp" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="@color/black" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/futuracondensed"
android:gravity="center"
android:paddingBottom="5dp"
android:text="100"
android:textColor="@color/black"
android:textSize="60sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
Java
public class MainActivity extends AppCompatActivity {
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Tab1Fragment(), "TAB1");
adapter.addFragment(new Tab2Fragment(), "TAB2");
adapter.addFragment(new Tab3Fragment(), "TAB3");
viewPager.setAdapter(adapter);
}
@Override
public void onResume() {
super.onResume();
}
}
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}