Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get tab click event in activity on TabLayout android

Tags:

android

This is the implementation of the onCreate method in my activity. I need to create the tabs dynamically. For simplicity, I made 2 tabs.

public class ActivityProductList extends AppCompatActivity {

    private android.support.v7.widget.SearchView searchView = null;
    private RecyclerView recyclerView;
    MyAdapter adapter;
    List<ParentListItem> parentListItems = new ArrayList<>();
    List<ParentListItem> originalProductList = new ArrayList<>();
    String query = null;
    int categoryId = 0;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("PRODUCTS");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
        tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
        //setupViewPager(viewPager);
        //tabLayout.setupWithViewPager(viewPager);
        //viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        searchView = (android.support.v7.widget.SearchView) findViewById(R.id.searchView);
        searchView.setIconified(false);
        searchView.onActionViewExpanded();
        searchView.clearFocus();
        searchView.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (adapter != null)
                    filterData(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                if (adapter != null)
                    filterData(query);
                return false;
            }
        });

    }
}

I want to change the EditText value on clicking on a tab. That EditText is in the same activity.

How do I get Tab click event in Activity?

like image 637
Devesh Agrawal Avatar asked May 15 '16 06:05

Devesh Agrawal


People also ask

How do I use tabLayout on Android?

This example demonstrates how do I create a Tab Layout in android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main. xml.

How do I add tabs in programmatically?

addTab(Tab tab): This method is used to add a tab in the TabLayout. By using this method we add the tab which we created using newTab() method in the TabLayout. The tab will be added at the end of the list and If it is the first tab to be added then it will become the selected tab.


4 Answers

You can use the onTabSelected listener to achieve that.

tabLayout.addOnTabSelectedListener(new OnTabSelectedListener() {         @Override         public void onTabSelected(Tab tab) {              switch(tab.getPosition()) {                  case 0:                   ....              }         } 

P.S. setOnTabSelectedListener is deprecated, please use addOnTabSelectedListener instead.

like image 102
Shashank Udupa Avatar answered Sep 30 '22 14:09

Shashank Udupa


You can use the onTabSelected listener in Kotlin like this

tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {             override fun onTabSelected(tab: TabLayout.Tab?) {             }              override fun onTabUnselected(tab: TabLayout.Tab?) {             }              override fun onTabReselected(tab: TabLayout.Tab?) {             }         }) 
like image 43
Zohab Ali Avatar answered Sep 30 '22 15:09

Zohab Ali


If what you want is when a tab is clicked and not each time a tab is selected, you can use custom view for each tab and then handle click on respective view like this:

for (int i = 0; i < tabLayout.getTabCount(); i++) {
    final TabLayout.Tab tab = tabLayout.getTabAt(i);
    final View tabView = LayoutInflater.from(this).inflate(
            R.layout.item_tab, (ViewGroup) tab.getCustomView(), false);
    tabLayout.setCustomView(tabView);

    final TextView customView = (TextView) tab.getCustomView();
    customView.setText(mAdapter.getPageTitle(i));
    final int index = i;
    customView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (tabLayout.getSelectedTabPosition() == index) {
                // change edittext value
            } else {
                tab.select();
            }
        }
    });
}
like image 29
hakim Avatar answered Sep 30 '22 16:09

hakim


In Java,

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                Toast.makeText(this, "position : "+position, Toast.LENGTH_SHORT).show();
            }

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

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
like image 33
HAZEEM JOONUS Avatar answered Sep 30 '22 16:09

HAZEEM JOONUS