1

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();
        }
    }
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
  • Have you added any content in `Tab1Fragment`, `Tab2Fragment` and `Tab3Fragment` ?. On Tapping of `Tab`, it will change `Fragment` in `ViewPager`, will not change activity – Chintak Patel Mar 29 '18 at 06:13
  • Yes, I've done all like in tutorial (There is a button with toast message on every tab, except I have my own main activity). I want it to be like that, when I run app, I am on first tab (main activity), then when I click to another tab (tab2) I move to another screen. – Redas Shuliakas Mar 29 '18 at 06:22
  • 1
    Problem is with your layout not with your code.U have used nested scroll inside that u have used tabs and viepager.thats why the things are not working. – androholic Mar 29 '18 at 06:27

3 Answers3

2

use this XML as given in the library u have followed then whatever design u want to add them to the fragment layouts u have taken.things will work fine

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    tools:context="com.example.chirag.slidingtabsusingviewpager.MainActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SlidingTabsUsingViewPager"
            android:textSize="20dp"/>


    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tablayout"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/pager">

    </android.support.v4.view.ViewPager>

</LinearLayout>
androholic
  • 686
  • 6
  • 23
  • Honestly, you are my hero and genius! But only thing to polish it. Now on the top I have "Toolbar", which I want to remove (if it's possible), because now I have "extra" bar, with "back arrow" and String in toolbar "Name of my app + SlidingTabsUsingViewPager", can you guess what I can do? – Redas Shuliakas Mar 29 '18 at 08:10
  • Well maybe I've put confusing explanation how it's look like (can't find screenshot on my phone), so it's looks like in Android Geek comment below. – Redas Shuliakas Mar 29 '18 at 08:12
  • (remove title bar etc) https://stackoverflow.com/questions/2591036/how-to-hide-the-title-bar-for-an-activity-in-xml-with-existing-custom-theme – androholic Mar 29 '18 at 09:13
  • (or u can use this) https://stackoverflow.com/questions/36236181/how-to-remove-title-bar-from-the-android-activity – androholic Mar 29 '18 at 09:14
  • Links you provided doesn't help, because it removes status bar (network, wi-fi, time and etc), not what I was talking about. – Redas Shuliakas Mar 30 '18 at 04:14
  • I removed textview from you xml "SlidingTabsUsingViewPager", now I have my app name on the top, tried to remove it in Java (usualy works) and tried to do it in xml, nothing works, I think it's because I have a "Toolbar" in xml...I am very confused now.... – Redas Shuliakas Mar 30 '18 at 04:27
  • I tottaly did it! By myself, I removed Toolbar from XML, and comented this Java lines //toolbar = (Toolbar) findViewById(R.id.toolbar); //setSupportActionBar(toolbar); //getSupportActionBar().setDisplayHomeAsUpEnabled(true); – Redas Shuliakas Mar 30 '18 at 04:29
0

try this code:

OneFragment.java

OneFragment.java
package info.androidhive.materialtabs.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import info.androidhive.materialtabs.R;


public class OneFragment extends Fragment{

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
}

 }

fragment_one.xml

 fragment_one.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="info.androidhive.materialtabs.fragments.OneFragment">

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/one"
    android:textSize="40dp"
    android:textStyle="bold"
    android:layout_centerInParent="true"/>

</RelativeLayout>

activity_main.xml

<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_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <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.design.widget.AppBarLayout>

   <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

MainActivity.java

  package info.androidhive.materialtabs.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

import info.androidhive.materialtabs.R;
import info.androidhive.materialtabs.fragments.OneFragment;
import info.androidhive.materialtabs.fragments.ThreeFragment;
import info.androidhive.materialtabs.fragments.TwoFragment;

[![enter image description here][1]][1]public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

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

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
  }
 }

enter image description here

and add the main activity calling 3 tabs

     private class MyPagerAdapter extends FragmentPagerAdapter {

     public MyPagerAdapter(FragmentManager fm) {
        super(fm);
     }

    @Override
    public Fragment getItem(int pos) {
        switch(pos) {
            case 0: return SimpleFragment.newInstance("FirstFragment, Instance 1");
            case 1: return PieView.newInstance("SecondFragment, Instance 1");
            case 2: return PieView1.newInstance("ThirdFragment, Instance 1");
             case 3: return DataSaveDetails.newInstance("ThirdFragment, Instance 1");
              default:
        }
        return null;
    }

it works please try this

Android Geek
  • 8,956
  • 2
  • 21
  • 35
  • I refactored everything by your code...Well first, "back arrow" never disappears and does nothing, and most important, I still on main activity and that doesn't change when I press to tab. – Redas Shuliakas Mar 29 '18 at 06:44
  • You provided code from different program...I don't have "SimpleFragment" and etc, I tried to put there "OneFragment" not working... – Redas Shuliakas Mar 29 '18 at 06:50
  • Used this public Fragment getItem(int position) { switch (position) { case 0: return new OneFragment(); case 1: return new TwoFragment(); case 2: return new ThreeFragment(); default: } return null; } – Redas Shuliakas Mar 29 '18 at 06:53
0

On your getItem() method. Use Switch and return fragments their instead of adding all the fragments in addFragment method.

    public Fragment getItem(int position){
     switch(position){
       case 0 : return new Tab1Fragment();
       case 1 : return new Tab2Fragment();
       case 2 : return new Tab3Fragment();
     }

    }