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?
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.
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.
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.
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?) { } })
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();
}
}
});
}
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) {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With