android


How to “RE-SELECT” tab in TabLayout programmatically?


TabLayout.OnTabSelectedListener has one callback which is onTabReselected(TabLayout.Tab tab)
We can reselect tab manually by tapping on selected tab again.
So my question is how to reselect tab programatically?
This is how I got my tabs working
Layout for tabs:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin"
android:background="#000000"
app:titleTextColor="#ffffff"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/PopupMenuStyle">
<TextView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/textview"
android:textColor="#color/colorTrueWhite"/>
</android.support.v7.widget.Toolbar>
<!-- our tablayout to display tabs -->
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlack"
android:minHeight="?attr/actionBarSize"
app:tabIndicatorColor="#color/colorTrueWhite"
app:tabIndicatorHeight="5dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Activity using that layout:
public class Main2Activity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
private TabLayout tabLayout;
public static ViewPager viewPager;
public static Context ctx;
Pager adapter;
public static int expired, paid;
Boolean isConnection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
TextView tv = (TextView) findViewById(R.id.textview);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ctx = getApplicationContext();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.pager);
tabLayout.addTab(tabLayout.newTab().setText("title1"));
tabLayout.addTab(tabLayout.newTab().setText("title2"));
tabLayout.addTab(tabLayout.newTab().setText("title3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount(), ctx);
viewPager.setAdapter(adapter);
if(some condition)
{
viewPager.setCurrentItem(2);
}
else
{
viewPager.setCurrentItem(1);
}
viewPager.setOffscreenPageLimit(2);
tabLayout.addOnTabSelectedListener(this);
ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int j = 0; j < tabsCount; j++) {
ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
int tabChildsCount = vgTab.getChildCount();
for (int i = 0; i < tabChildsCount; i++) {
View tabViewChild = vgTab.getChildAt(i);
if (tabViewChild instanceof TextView) {
((TextView) tabViewChild).setTypeface(Typeface.DEFAULT, Typeface.BOLD);
}
}
}
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
}
Pager code:
public class Pager extends FragmentStatePagerAdapter
{
int tabcount;
Context ctx;
private String [] Titles = {"title1", "title2", "title3"};
public Pager(FragmentManager fm, int tabcount, Context ctx)
{
super(fm);
this.tabcount = tabcount;
this.ctx = ctx;
}
#Override
public int getCount() {
return tabcount;
}
#Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
default:
return null;
}
}
}

Related Links

org.apache.cordova.api doesn't exist. PhoneGap 3.0
android default values for shared preferences
Android SherlockListFragment list adapter with custom layout for each item
Understanding android layout and drawable resources
How do I discover a Chromecast device using Android?
reset the color of a previously pressed button after another button is pressed in custom keyboard in android
Maven Deploy Error - Android-Maven + Intellij Integration
App with integrated SQLite database keeps crashing
Android 2.2: javax.net.ssl.SSLException: Not trusted server certificate - Android 2.3: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Getting video frames from a video in Android
making press effect for button in android
Android: change color of disabled text using Theme/Style?
Android Gallery crash
Image Is Cropped when setting as wallpaper
Android Google Map V2 - Find my location error
SDK Manager not opening in Android Studio

Categories

HOME
jpa
awk
polymer
merge
pda
components
camera
hessian
fftw
can
seo
switch-statement
echo
lanczos
line
solrcloud
gulp-watch
ember-cli
monit
beta
android-sharedpreferences
corpus
exchangewebservices
comparable
cppreference
bioinformatics
xbox
dst
hreflang
joomla2.5
indy10
batching
engine.io
apic
salesforce-communities
playframework-2.1
xcode7.3
modernizr
magento-1.9.3
branching-and-merging
drupal-theming
activeperl
carrot2
custom-font
git-flow
boo
sundials
flac
template-toolkit
pocketsphinx-android
pure-css
actor-platform
ziparchive
spring-cloud-feign
mta
mapinfo
cassia
spreadjs
findfirst
design-principles
ascii-art
asyncdisplaykit
character-replacement
wiredep
fuelcms
evolus-pencil
viola-jones
openbabel
cosine-similarity
uncaught-typeerror
jqmodal
codeplex
gnumeric
http-patch
printdialog
kogrid
workitem
apache-abdera
garbage
josql
google-maps-mobile
workflow-services
cuteeditor
tmx
collectionviewsource
cloudfiles
ie-compatibility-mode
cruisecontrol.rb
web-garden
isapi-extension
echo3
dojo-dnd
ifilter
mathematical-typesetting

Resources

Encrypt Message