Android Tabwidget Detect Click on Current Tab

Android TabWidget detect click on current tab

I think I have found a solution, here follows a sample code:

    intent = new Intent(this, HomeGroup.class);
View tab1 = _inflater.inflate(R.layout.custom_tab_1,null);
homeTab.setTag("Tab1");
spec = tabHost.newTabSpec("Tab1").setIndicator(tab1).setContent(intent);
tabHost.addTab(spec);

View tab2 = _inflater.inflate(R.layout.custom_tab_2,null);
homeTab.setTag("Tab2");
spec = tabHost.newTabSpec("Tab2").setIndicator(tab2).setContent(intent);
tabHost.addTab(spec);

View tab3 = _inflater.inflate(R.layout.custom_tab_3,null);
homeTab.setTag("Tab3");
spec = tabHost.newTabSpec("Tab3").setIndicator(tab3).setContent(intent);
tabHost.addTab(spec);

tabHost.setOnTabChangedListener(this);

//click on seleccted tab
int numberOfTabs = tabHost.getTabWidget().getChildCount();
for(int t=0; t<numberOfTabs; t++){
tabHost.getTabWidget().getChildAt(t).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){

String currentSelectedTag = MainTab.this.getTabHost().getCurrentTabTag();
String currentTag = (String)v.getTag();
Log.d(this.getClass().getSimpleName(), "currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag);
if(currentSelectedTag.equalsIgnoreCase(currentTag)){
MainTab.this.getTabHost().setCurrentTabByTag(currentTag);
String newSelectedTabTag = MainTab.this.getTabHost().getCurrentTabTag();
if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
//do smthg
}else if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){
//do smthg
}else if(newSelectedTabTag.toLowerCase().indexOf("tab3")!=-1){
//do smthg
}
return true;
}
}
return false;
}
});
}

Probably it is possible to improve it, but this does the work for me!

How to detect a click on an already selected tab in TabLayout

Based on spending ~10 seconds reading the documentation... call setOnTabSelectedListener() on the TabLayout. Pass in an OnTabSelectedListener implementation. Your event callback method is onTabReselected().

Get notified when current tab is selected again

You might be able to get this to work by having your TabActivity implement View.onTouchListener and calling setOnTouchListener for each of the tabs...

for (int i = 0; i < tabWidget.getChildCount(); i++) {
View v = tabWidget.getChildAt(i);
v.setOnTouchListener(this);
}

Then override onTouch() in your activity...

@Override
public boolean onTouch(View v, MotionEvent event) {

// Not sure what to do here to identify each tab

return false;
}

As you can see from the comment in the above, I'm not sure what to do with the View (v parameter) in the onTouch listener to identify which Tab has been touched.

I can confirm it fires whether or not you touch the currently selected tab or one which isn't selected and it doesn't interfere with changing tabs.

Hope it helps.

Capturing the tab click event in Android Tabview

If you ares till looking for a solution, I might have found one.
Take a look here: Android TabWidget detect click on current tab

Detecting a click on an already selected tab button

Hide the tab widget and use standard buttons to switch between tabs. You can switch tabs using

tabhost.setCurrentTab('index of the tab');

You can make the button look selected by using the State drawable and setting the button as selected.

button.setSelected(true);

Well, to do your requirement, you can put the appropriate code in the buttons onClick listener. This project might give you an idea on setting up the tabs.

Clicking on tabs does not switch current tab when using Android TabLayout

The main problem is, the ViewPager overlays the TabLayout. So, you should place it below TabLayout:

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/sliding_tabs"/>

And from the Activity's onCreate method, call:

TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);

For more details, check this example:

https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/java/com/support/android/designlibdemo/MainActivity.java

https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml

Is it possible to check if a specific TAB in TabWidget is visible in current View or not?

One solution would be to make you own HorizontalScrollView class and show the ImageViews based on the scroll(although I didn't tested that much the code below should work). Your layout will look like this:

//...
<RelativeLayout
android:id="@+id/rl_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView android:id="@+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/i_button"
android:layout_alignParentLeft="true"
android:visibility="gone"/>

<ImageView android:id="@+id/arrow_Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/i_button"
android:layout_alignParentRight="true"
/>

<com.your.package.here.SpecialScroll
android:id="@+id/my_scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:tabStripEnabled="true"/>
</com.your.package.here.SpecialScroll>
</RelativeLayout>

//...

The SpecialScroll class will look like this:

public class SpecialScroll extends HorizontalScrollView {

public interface PositionListener {

public void onLeftArrowRequired(boolean required);

public void onRightArrowRequired(boolean required);

public View implementScrolledView();
}

private PositionListener listener;

public void setPositionListener(PositionListener listener) {
this.listener = listener;
}

public SpecialScroll(Context context) {
super(context);
}

public SpecialScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public SpecialScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (l == 0) {
listener.onLeftArrowRequired(false);
} else {
listener.onLeftArrowRequired(true);
}
View v = listener.implementScrolledView();
Rect r = new Rect();
v.getDrawingRect(r);
if ((r.width() - l) == getWidth()) {
listener.onRightArrowRequired(false);
} else {
listener.onRightArrowRequired(true);
}

}

}

Then in your activity's onCreate method you'll do:

SpecialScroll hsv = (SpecialScroll) findViewById(R.id.my_scrollView);
hsv.setPositionListener(this); // I made the activity implement the SpecialScroll.PositionListener interface

and the activity's implementation of the PositionListener interface is:

@Override
public void onLeftArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_left))
.setVisibility(View.GONE);
}
}

@Override
public void onRightArrowRequired(boolean required) {
if (required) {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.VISIBLE);
} else {
((ImageView) findViewById(R.id.arrow_right))
.setVisibility(View.GONE);
}
}

@Override
public View implementScrolledView() {
return findViewById(android.R.id.tabs);
}


Related Topics



Leave a reply



Submit