'Android long click on tab
I have a TabHost with some tabs, and after doing a long press on a tab, I want to get the position or the tag of the tab which was long pressed, and not the current tab that is showed. Below there is some code in which I create the long press listener for the TabHost:
myTabHost.getTabWidget().getChildAt(i).setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}
});
Any solution? Is correct to apply the listener at TabHost in my case?
Solution 1:[1]
I resolved my problem adding to the view of the tab a tag information , and then I attached at the view a listener that gets and prints this tag:
View tabView= mTabHost.getTabWidget().getChildAt(i);
// set the tag information at the view of the tab (the tag contains the position number of the tab)
tabView.setTag( Integer.valueOf(i));
tabView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
// I print the number position of the tab
Log.d("tab number", ((Integer)view.getTag()).toString() );
return false;
}
});
Solution 2:[2]
The identifier of the tab that was long-clicked resides in the View v
parameter of the onLongClick
function. Call v.getId()
and the rest are details
Solution 3:[3]
val tabCount = binding.tabLayout.tabCount
for (index in 0 until tabCount) {
binding.tabLayout.getTabAt(index)?.apply {
view.isLongClickable = false
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
view.tooltipText = ""
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Bogdan Alexandru |
Solution 3 | ??? |