android disable / enable tab in tabbedActivity - android

I can not disable a tab in android tabbed activity.
The tabbed activity have 3 tabs and I want to disable the tab in the middle.
I tried the following code in my Fragment but the variable middleTabView is always null!
TabLayout tabhostNew = (TabLayout) getActivity().findViewById(R.id.tabs);
TabLayout.Tab middleTabView = tabhostNew.getTabAt(1).getCustomView();
middleTabView.setEnabled(false); //does not work, because middleTabView is null
The following code should work, but i am not able to get the variable tabwidget.
tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);
Can you please help me?
Thank you in advance!

The method you are trying to call getTabWidget() is implemented in the TabHost class and not in TabLayout (which you are using).
Check out this answer:
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager); // if you are using a view pager
LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}

Related

How to get Tab's parent (TabLayout) from TabSelectedListener

I have two TabLayouts on the same fragment and each of them holds two tabs:
TabLayout studentsTab = view.findViewById(R.id.students_tab);
studentsTab.addTab(studentsTab.newTab().setText("one"));
studentsTab.addTab(studentsTab.newTab().setText("two"));
studentsTab.setTag("students");
TabLayout teachersTab = view.findViewById(R.id.teachers_tab);
teachersTab.addTab(teachersTab.newTab().setText("one"));
teachersTab.addTab(teachersTab.newTab().setText("two"));
teachersTab.setTag("teachers");
and instead of creating two setOnTabSelectedListener listeners i've implemented TabLayout.OnTabSelectedListener by :
studentsTab.setOnTabSelectedListener(this);
teachersTab.setOnTabSelectedListener(this);
and now i have this method:
#Override
public void onTabSelected(TabLayout.Tab tab) {
// detect which TabLayout?
}
and I want to figure which tab triggered the tab selection. Is it studentsTab or teachersTab?
I tried to check if tab.getTag().equals("students") but the tab.getTag() is null.
When i debug the app i have the TabLayout on tab.mParent but it's not accessible.
I couldn't find a way to do it. Any help to find the parent would be appreciated.
You can set a tag associated with your tabs before you add them to the TabLayout. If you add a tag of "student" or "teacher" to the correct tab, then you can reference that information in the onTabSelected().
Check out the docs: TabLayout specifically looking at the getTag() and setTag().
Try setting contentDescription for each tab like this
for(int i=0; i<studentsTab.getTabCount; i++) {
TabLayout.Tab tab = studentsTab.getTabAt(i);
tab.setContentDescription("Text");
}
And then, in the onTabSelected method get the contentDescription
public void onTabSelected(TabLayout.Tab tab) {
tab.getContentDescription();
}

ViewPager blocks Fragment's View's OnClickListeners

I currently have a view pager in a tab layout
//Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SettingsFragmentPagerAdapter(getSupportFragmentManager()));
//Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
And then the fragments in the view pager have TextViews with OnClickListeners attached
final TextView zSemTxt = (TextView) getActivity().findViewById(R.id.zSemTxt);
zSemTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSelectedTextView = zSemTxt;
showDialogToSelectClass();
}
});
My issue is the OnClickListeners are never called, it appears that all touches go to the ViewPager and never goes through to the TextViews. I've tried things like viewPager.requestDisallowInterceptTouchEvent(true); but none of it change anything.
Ideas?
Show more code.
Probably onClick dosen't work because your textview = null.
final TextView zSemTxt = (TextView) getActivity().findViewById(R.id.zSemTxt);
Are You sure about this?
I dont see your code but try
final TextView zSemTxt = (TextView) view.findViewById(R.id.zSemTxt);
where view is your fragment inflated view.
Turns out neither the onActivityCreated or onCreateView were being called on the fragment.
The solution was to go to the PageFragment class I was using to manage which fragment was displayed by the ViewPager and add the onClickListeners to there. The fragments were being inflated there which is why the other classes were never called.

Change tab index of fragments with tabs programmatically

I'm getting a null pointer exception when trying to switch tabs programmatically of a tablayout inside a fragment,
So I have my main activity that has a tab layout (4 tabs) each tab has a view pager holding a fragment, and each of these fragments has a tab layout (x amount of tabs) with a view pager holding a fragment,
i can switch the tabs of my main activity tab layout from any fragment like this
TabLayout tabLayout = MainActivity.tabLayout;
TabLayout.Tab tab = tabLayout.getTabAt(2);
tab.select();
but if i try to change the tabs of one of the fragments in the same way i get a null pointer
TabLayout tabLayout2 = tabFragOne.tabLayout;
TabLayout.Tab tab2 = tabLayout2.getTabAt(2);
tab2.select();
it only happens if I click the button in question when the app first opens, which suggests that the reason for it is that the fragment hasn't been attached or created yet,
for instance if i scroll across to the fragments tab i want to switch to, and then go back to the main activity and press the button in question it will work.
does anyone know the best way to fix this?
Ok ive found half the crux to this question is actually that im using a view pager adapter, a question here sheds a lot of light on my issue
The correct way to set selected Tab, we should set selected index from Pager instead.
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
pager.setCurrentItem(0);//selected position
Here is my layout design in xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#ffffff"
android:background="#color/al_white"
android:elevation="0dp" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
app:tabTextColor="#494a43"
app:tabSelectedTextColor="#494a43"
app:tabIndicatorColor="#494a43"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabGravity="fill" />
</android.support.design.widget.AppBarLayout>
TabLayout tabhost = (TabLayout) getActivity().findViewById(R.id.tabLayout);
tabhost.getTabAt(2).select();
I use this inside my fragment's create function... and it works perfectly
R.id.tabLayout is the tablayout id in your main activity layout.
you can use the setCurrentItem(Postion) in viewpager
example :
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
// tabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(4);
Simple example to automatically scroll from the last page to the first. Note: I added a dummy fragment at the first and last position.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 0) {
viewPager.setCurrentItem(viewPagerAdapter.getCount() - 2);
} else if (position == viewPagerAdapter.getCount() - 1) {
viewPager.setCurrentItem(1);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Using view pager, Its working simple and fine
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs;
String mode;
AllDeals deals;
MyDeals myDeals; those are my fragments
public ViewPagerAdapter(FragmentManager fm, int mNumbOfTabsumb, String moe) {
super(fm);
this.NumbOfTabs = mNumbOfTabsumb;
mode=moe;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if(mode.equals("Deals")){
deals=new AllDeals();
return deals;
}
break;
case 1:
if(mode.equals("Deals")){
myDeals=new MyDeals();
return myDeals;
}
break;
default:
break;
}
return null;
}
#Override
public int getCount() {
return NumbOfTabs;
}
}
add this lines into ur activity
containerViewone.setVisibility(View.VISIBLE);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.title_all_deals)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.title_my_deals)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabTextColors(getResources().getColor(R.color.tabunselect), getResources().getColor(R.color.black));
viewpagerad = new ViewPagerAdapter(getSupportFragmentManager(), Numboftabs, "Deals");
pager.setAdapter(viewpagerad);
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
Try this:
TabLayout tablayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //your activity layout
tabLayout =(TabLayout)findViewById(R.id.tablayout); //your tab layout id
tabLayout.getTabAt(0).select(); //your tab index;
}
In Kotlin:
With TabLayout and ViewPager2 that works for me:
binding.tablayout.getTabAt(1)!!.select()

Android tablayout selecting the last tab instead of first

I need your help. I have a tablayout with tintable imageview and textview. I want to highlight the default selected tab which is the first one.
This is my sample code:
TabLayout.Tab tab;
mTabs = (TabLayout) findViewById(R.id.tabs);
mViewpager = (ViewPager) findViewById(R.id.viewpager);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewpager.setAdapter(pagerAdapter);
mViewpager.addOnPageChangeListener(new MyPageScrollListener(mTabs));
mTabs.setupWithViewPager(mViewpager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
mTabs.setOnTabSelectedListener(new MyOnTabSelectedListener());
tab.select();
But it's selecting the last tab. Anyone here knows how to fix this? I'd appreciate any kind of help. Thanks!
Finally, I found a solution here: link
I added:
mTabs.getTabAt(1).select();
mTabs.getTabAt(0).select();
And it works!
I found a better solution. Instead if selecting the tab from the TabLayout you can do this with the ViewPager like so:
ViewPager viewPager = (ViewPager) findViewById(R.id.restaurant_menu_viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// ViewPager mit TabLayout verknüpfen
TabLayout tabLayout = (TabLayout) findViewById(R.id.restaurant_menu_tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
See the post here:
https://stackoverflow.com/a/38762670/7318519
You selected the last tab.
Take a look at your source code :)
TabLayout.Tab tab;
...
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
}
...
tab.select();
tab is the last tab you will get with getTabAt(int position).
So when you want to select the first tab this will help
maTabs.getTabAt(0).select();
Another (maybe better) solution is to turn the for loop into this
for (int i = mTabs.getTabCount() - 1; i >= 0; i--) {
tab = mTabs.getTabAt(i);
}
Then your "last" tab you are getting in this loop is the first tab in your TabLayout.
An working full example code looks like this gist.

How to change ViewPager's page?

I'm using ViewPager in my app and define it in the main Activity. Inside onCreate method I load some number of pages from SharedPreferences and then pass it to PagerAdapter:
#Override
public int getCount() {
return numberOfPages;
}
The problem is that if I would change this number in Preferences (or another Activity) to some other less then page index I viewed before, my app crashes because this index is out of bounds when I return to the activity with this ViewPager. It can be fixed simply by changing active ViewPager's page. Is there any way to do it?
I'm not sure that I fully understand the question, but from the title of your question, I'm guessing that what you're looking for is pager.setCurrentItem( num ). That allows you to programatically switch to another page within the ViewPager.
I'd need to see a stack trace from logcat to be more specific if this is not the problem.
slide to right
viewPager.arrowScroll(View.FOCUS_RIGHT);
slide to left
viewPager.arrowScroll(View.FOCUS_LEFT);
Without checking your code, I think what you are describing is that your pages are out of sync and you have stale data.
You say you are changing the number of pages, then crashing because you are accessing the old set of pages. This sounds to me like you are not calling pageAdapter.notifyDataSetChanged() after changing your data.
When your viewPager is showing page 3 of a set of 10 pages, and you change to a set with only 5, then call notifyDataSetChanged(), what you'll find is you are now viewing page 3 of the new set. If you were previously viewing page 8 of the old set, after putting in the new set and calling notifyDataSetChanged() you will find you are now viewing the last page of the new set without crashing.
If you simply change your current page, you may just be masking the problem.
for switch to another page, try with this code:
viewPager.postDelayed(new Runnable()
{
#Override
public void run()
{
viewPager.setCurrentItem(num, true);
}
}, 100);
Supplemental answer
I was originally having trouble getting a reference to the ViewPager from other class methods because the addOnTabSelectedListener made an anonymous inner class, which in turn required the ViewPager variable to be declared final. The solution was to use a class member variable and not use the anonymous inner class.
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
// don't use an anonymous inner class here
tabLayout.addOnTabSelectedListener(tabListener);
}
TabLayout.OnTabSelectedListener tabListener = new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
// The view pager can now be accessed here, too.
public void someMethod() {
viewPager.setCurrentItem(0);
}
}

Categories

Resources