Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swiching tabs programally in TabLayout

I'm new to android programming and I can't find a way to switch between tab1 to tab2 in a TabLayout, when the user clicks a button located in the tab1.

It may be something very simple, but I am clueless in my first app.

I have tried the following at first:

TabLayout.Tab tab = tabLayout.getTabAt(1);
tab.select();

And it used to work, but I changed the code and, at some point, it just didn't work anymore.

I also triedtab.getCustomView().setSelected(true);, but I got NullPointerException. So I checked in a if statement if tab was null, and it wasn't.

And then I tried

tabLayout.setScrollPosition(1,0f,true);
ViewPager viewPager = new ViewPager(mainView.getContext());
viewPager.setCurrentItem(1);

But none of the solutions above worked for me.

Here is my code:

View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    try {
        view = inflater.inflate(R.layout.fragment_main, container, false);
        Button GoB = view.findViewById(R.id.GoB);
        final EditText USETV = view.findViewById(R.id.USETV);
        final EditText commandEV = view.findViewById(R.id.CommandTV);
        final SqlHelper db = new SqlHelper(getContext(), "myDatabase", null, 1);
        GoB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String a = "";
                    if (USETV.getText().toString().length() > 0) {
                        a += "USE " + USETV.getText().toString() + " ;";
                    }
                    a += commandEV.getText().toString();
                    String[][] c = db.SqlQuery(a);
                    LayoutInflater factory = getLayoutInflater();
                    View resultView = factory.inflate(R.layout.fragment_result, null);
                    TableLayout tableLayout = resultView.findViewById(R.id.ResultContainer);
                    tableLayout.removeAllViews();
                    View mainView = factory.inflate(R.layout.main_activity,null);
                    TabLayout tabLayout = mainView.findViewById(R.id.tabs);
                    if (c[0].length > 0 && c[1].length > 0) {
                        TabLayout.Tab tab = tabLayout.getTabAt(1);
                        //tab.select();
                        //tab.getCustomView().setSelected(true);
                        //tabLayout.setScrollPosition(1,0f,true);
                        //ViewPager viewPager = new ViewPager(mainView.getContext());
                        //viewPager.setCurrentItem(1);
                        //
                        // do some stuff
                    }
                } catch (Exception e) {
                    showException(e);
                }
            }
        });
    } catch (Exception e) {
        showException(e);
    }
    return view;
}
like image 225
Vinícius Gabriel Avatar asked Nov 30 '25 01:11

Vinícius Gabriel


2 Answers

TabLayout and ViewPager belong to your Activity, so in your Fragment you have to call Activity function to switch tab

Here is an example:

public class MyActivity extends Activity {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    @Override
    public onCreate(...) {
        MyFrament myFragment = new MyFragment(this);
        // Add fragment to Viewpager ...
        // Attach ViewPager to TabLayout ...
    }

    public void switchTab(int index) {
        // Check index ...
        mViewPager.setCurrentItem(index);
    }
}

then

public class MyFragment extends Fragment {
    private Context mContext;
    private Button mButton;

    public MyFragment(Context context) {
        mContext = context;
    }

    @Override
    public View onCreateView(...) {
        ...

        mButton.setOnClickListener(v->{
            ((MyActivity)mContext).switchTab(1);
        });

        ...
    }
}

Hope this help!

like image 120
Liar Avatar answered Dec 04 '25 22:12

Liar


Try putting these two lines inside your button's handler :

ActionBar actionBar = (ActionBar)getActivity().getActionBar();
actionBar.setSelectedNavigationItem(1);
like image 35
Aviral_Ruhela Avatar answered Dec 04 '25 22:12

Aviral_Ruhela