How to go other Tabs by clicking on a Button from the current Tab in Android?

16,434

Solution 1

You have to call parent view inside your fragment.

public class MoviesFragment extends Fragment {

     ViewPager viewPager;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_movies, container, false);

        Button btn = (Button) rootView.findViewById(R.id.btn);
        viewPager = (ViewPager) getActivity().findViewById(R.id.pager);

           btn.setOnClickListener(new View.OnClickListener() 
           {

               @Override
              public void onClick(View v) 
              {
                   viewPager.setCurrentItem(0);


              }
          }); 

        return rootView;
    }

}

Solution 2

your ViewPager is null because you are not initializing the variable.

You need do

public class MoviesFragment extends Fragment {

ViewPager viewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle    savedInstanceState) 
{

   View rootView = inflater.inflate(R.layout.fragment_movies, container, false);

   Button btn = (Button) rootView.findViewById(R.id.btn);
   viewPager = (ViewPager) rootView.findViewById(R.id.some_viewpager);//Initializing
   btn.setOnClickListener(new View.OnClickListener() 
   {

       @Override
      public void onClick(View v) 
      {

          viewPager.setCurrentItem(0);

      }
  }); 


 return rootView;
}}
Share:
16,434
user2563306
Author by

user2563306

Updated on July 22, 2022

Comments

  • user2563306
    user2563306 almost 2 years

    I trying to write a code in Android, to switch from one tab to another tab by click on a button.

    I know to by clicking on tab we can switch from one tab to another but can it be possible to switch from one tab to another tab by clicking on one button.

    I tried the following tutorial.

    http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

    In this tutorial i have edited the MovieFragment.java file in following manner.

    MovieFragment.java

    package info.androidhive.tabsswipe;
    
    import info.androidhive.tabsswipe.R;
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager; 
    import android.support.v4.view.ViewPager; 
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    public class MoviesFragment extends Fragment {
    
    ViewPager viewPager;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle    savedInstanceState) 
    {
    
       View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
    
       Button btn = (Button) rootView.findViewById(R.id.btn);
    
       btn.setOnClickListener(new View.OnClickListener() 
       {
    
           @Override
          public void onClick(View v) 
          {
    
              viewPager.setCurrentItem(0);
    
          }
      }); 
    
    
     return rootView;
    }}
    

    XML Code :

    <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" 
         android:background="#17df0d">
    
    <android.support.v4.view.ViewPager
        android:id="@+id/myfivepanelpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Movies Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>
    
    <Button 
        android:id="@+id/btn"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:text="Click"
        />
    
    
    </RelativeLayout>
    

    I have created a button in the xml layout and trying switch to tab -1 but i am getting Null Pointer Exception.

    Error Code:

    05-20 09:07:10.995: E/AndroidRuntime(1117): FATAL EXCEPTION: main
    05-20 09:07:10.995: E/AndroidRuntime(1117): java.lang.NullPointerException
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at info.androidhive.tabsswipe.MoviesFragment$1.onClick(MoviesFragment.java:33)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.view.View.performClick(View.java:4240)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at  android.view.View$PerformClick.run(View.java:17721)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.handleCallback(Handler.java:730)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.dispatchMessage(Handler.java:92)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Looper.loop(Looper.java:137)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.app.ActivityThread.main(ActivityThread.java:5103) 
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Method.java:525) 
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    05-20 09:07:10.995: E/AndroidRuntime(1117):     at dalvik.system.NativeStart.main(Native Method)
    

    Please suggest me some solution or some good technique to do it.

  • Chefes
    Chefes almost 10 years
    Is because you don't have a ViewPager in your XML, add a ViewPager in your XML or check this sample androidtrainningcenter.blogspot.mx/2012/10/…
  • user2563306
    user2563306 almost 10 years
    Yes , i have edited my XML code and added a ViewPager in the xml code but there is no event after clicking on the Button... please help me out... please go through my XML code again ...
  • Jean-François Corbett
    Jean-François Corbett about 6 years
    This is the same thing as the accepted answer posted 4 years earlier...