objectTabLayout.setupWithViewPager is not found

48 views Asked by At

i am new in android and i am facing an strange situation in which i am not finding the setupWithViewPager.When i am calling objectTabLayout.setupWithViewPager then setupWithViewPager is not finding..what should i do?

My XML Code

<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorBlack"
    android:id="@+id/favoriteFragment_RL"
    tools:context="Fragments.Favorite">

   <com.google.android.material.tabs.TabLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/favoriteFragment_TabLayout"
       android:minHeight="?actionBarSize"
       app:tabGravity="fill"
       app:tabIndicatorColor="@color/colorWhite"
       app:tabIndicatorHeight="4dp"
       app:tabBackground="@color/colorPrimary"
       app:tabMode="fixed"
       app:tabTextColor="@color/colorWhite"/>
    
    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/favoriteFragment_viewPager"
        android:layout_below="@id/favoriteFragment_TabLayout"/>


</RelativeLayout>

my java code

     package Fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.Toast;

import com.example.connectsocialmediaapp.R;

import AdapterClasses.FavoriteStatusTabAdapter;


public class Favorite extends Fragment {


    //XML Variables
    private View parent;
    private TableLayout objectTablayout;
    private ViewPager objectViewPager;

    //Class Variables
    favoriteImageStatusFragment objectFavoriteImageStatusFragment;
    FavoriteStatusTabAdapter objectFavoriteStatusTabAdapter;
    favoriteTextStatusFragment objectFavoriteTextStatusFragment;

    private int[] tabIcons= {
            R.drawable.ic_text, R.drawable.ic_image
    };
    public Favorite() {
        // Required empty public constructor
    }

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

    private void addFragmentToTablayout()
    {
    try
    {
        objectFavoriteStatusTabAdapter=new FavoriteStatusTabAdapter(getChildFragmentManager());
        objectFavoriteStatusTabAdapter.addFragments(objectFavoriteTextStatusFragment);

        objectFavoriteStatusTabAdapter.addFragments(objectFavoriteImageStatusFragment);
        objectViewPager.setAdapter(objectFavoriteStatusTabAdapter);


        objectTablayout.(objectViewPager);
        objectViewPager.setSaveFromParentEnabled(false);

    }
    catch (Exception e)
    {
        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    }


    private void initializeVariables()
    {
        try
        {
            objectFavoriteTextStatusFragment=new favoriteTextStatusFragment();
            objectFavoriteImageStatusFragment=new favoriteImageStatusFragment();

            objectTablayout=parent.findViewById(R.id.favoriteFragment_TabLayout);

            objectViewPager=parent.findViewById(R.id.favoriteFragment_viewPager);
        }
        catch (Exception e)
        {
            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            
        }
    }
}

is there any alternative way to access setupWithViewPager or how should i access it.

1

There are 1 answers

0
LuongXuanNam On

It seems that you are confusing xml using TabLayout and Java code you use TableLayout. Modify TableLayoutOut in Java Code and Running, your problem has been solved

public class Favorite extends Fragment {


//XML Variables
private View parent;
private TabLayout objectTablayout;
private ViewPager objectViewPager;

//Class Variables
favoriteImageStatusFragment objectFavoriteImageStatusFragment;
FavoriteStatusTabAdapter objectFavoriteStatusTabAdapter;
favoriteTextStatusFragment objectFavoriteTextStatusFragment;

private int[] tabIcons= {
        R.drawable.ic_text, R.drawable.ic_image
};
public Favorite() {
    // Required empty public constructor
}

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

private void addFragmentToTablayout()
{
try
{
    objectFavoriteStatusTabAdapter=new FavoriteStatusTabAdapter(getChildFragmentManager());
    objectFavoriteStatusTabAdapter.addFragments(objectFavoriteTextStatusFragment);

    objectFavoriteStatusTabAdapter.addFragments(objectFavoriteImageStatusFragment);
    objectViewPager.setAdapter(objectFavoriteStatusTabAdapter);


    objectTablayout.(objectViewPager);
    objectViewPager.setSaveFromParentEnabled(false);

}
catch (Exception e)
{
    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}


private void initializeVariables()
{
    try
    {
        objectFavoriteTextStatusFragment=new favoriteTextStatusFragment();
        objectFavoriteImageStatusFragment=new favoriteImageStatusFragment();

        objectTablayout=parent.findViewById(R.id.favoriteFragment_TabLayout);

        objectViewPager=parent.findViewById(R.id.favoriteFragment_viewPager);
    }
    catch (Exception e)
    {
        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        
    }
}

}