Home > other >  Button OnClick from an Activity to a specific Tab/Fragment
Button OnClick from an Activity to a specific Tab/Fragment

Time:10-30

Here's the problem. I have 7 buttons in an Activity and 7 tabs/fragments in a different activity. The 7 tabs/fragments are slidable. I wanted to make the buttons to be able to open their respective tabs. For example, clicking on button 1 will redirect to tab/fragment 1, clicking on button 2 will open tab/fragment 2, etc. So, how do I make the buttons to work as I intended? Any answers or ideas would be much appreciated. Thank you in advance.

Here's my code: TabbedActivity.java

public class TabbedActivity extends AppCompatActivity {

    TabLayout tabLayoutaa;
    ViewPager2 pager2aa;
    FragmentAdapter adapteraa;
    int value;

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

        tabLayoutaa = findViewById(R.id.tab_layoutaa);
        pager2aa = findViewById(R.id.view_pager2aa);

        FragmentManager fm = getSupportFragmentManager();
        adapteraa = new FragmentAdapter(fm, getLifecycle());
        pager2aa.setAdapter(adapteraa);

        tabLayoutaa.setTabMode(tabLayoutaa.MODE_SCROLLABLE);
        tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 1"));
        tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 2"));
        tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 3"));
        tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 4"));
        tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 5"));
        tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 6"));
        tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 7"));

        tabLayoutaa.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                pager2aa.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        pager2ah.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                tabLayoutaa.selectTab(tabLayoutaa.getTabAt(position));
            }
        });
    }
}

ButtonList.java

public class ButtonList extends AppCompatActivity {


    private Button btntotab1;
    private Button btntotab2;

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

        btntotab1 = (Button)findViewById(R.id.btntotab1);
        btntotab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openTab1();
            }
        });
    }

    public void openTab1(){
        Intent intent = new Intent(this, TabbedActivity.class);
        startActivity(intent);
    }
}

CodePudding user response:

Search onclick property each button from right panel in Android IDE. Place your tab change function over there. In the Main Activity, use switch statement inside button function compare id.

Use same function in onclick property of each button in xml

Like in MainActivity you have function: openTab() be like.

public void OpenTab(View view)
{
    switch view.getId()
    {
        case r.id.xx:
        // Your working
        break;
    }
}

CodePudding user response:

First, pass your buttonId to the Activity which contains all Tabs/fragments.

ButtonList.java

   public class ButtonList extends AppCompatActivity {
    private Button btntotab1;
    private Button btntotab2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buttonlist);
    
        btntotab1 = (Button)findViewById(R.id.btntotab1);
        btntotab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openTab(1);
            }
        });
       btntotab2 = (Button)findViewById(R.id.btntotab2);
        btntotab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openTab(2);
            }
        });
    }
    
    public void openTab(int buttonId){
        Intent intent = new Intent(this, TabbedActivity.class);
        intent.putExtra("button_id",buttonId);
        startActivity(intent);
    }
   }

Now pass that buttonId to the fragmentManager or your viewPager.

TabbedActivity.java

 public class TabbedActivity extends AppCompatActivity {

TabLayout tabLayoutaa;
ViewPager2 pager2aa;
FragmentAdapter adapteraa;
int value, buttonId;

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

    tabLayoutaa = findViewById(R.id.tab_layoutaa);
    pager2aa = findViewById(R.id.view_pager2aa);
     buttonId=getIntent().getIntExtra("button_id",0); // to get the button id

    FragmentManager fm = getSupportFragmentManager();
    adapteraa = new FragmentAdapter(fm, getLifecycle());
    pager2aa.setAdapter(adapteraa);
   
    tabLayoutaa.setTabMode(tabLayoutaa.MODE_SCROLLABLE);
    tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 1"));
    tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 2"));
    tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 3"));
    tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 4"));
    tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 5"));
    tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 6"));
    tabLayoutaa.addTab(tabLayoutaa.newTab().setText("Tab 7"));

     pager2aa.setCurrentItem(buttonId-1);  // to get the current Tab

    tabLayoutaa.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            pager2aa.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    pager2ah.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            tabLayoutaa.selectTab(tabLayoutaa.getTabAt(position));
        }
    });
}
}

You can also use a switch case to get the selected tab and pass it to the fragment manager if it doesn't work. there may be some syntax errors so be careful.

  • Related