I have used multiple tab layout activities (ex.5) & multiple image view (ex:5 ).i click (Ex:3) image then open particular tab (Ex:3). how to write code anybody help.Image used in main activity image click enter tab layout particular tab.
Use Android TabHost and its method setCurrentTab() in your onCreate()-method.Pass your required tab index to this onCreate()
On using ViewPager pass the selected imageview position
viewPager.setCurrentItem(position, true);
Using TabLayout, add onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
When it comes to slide
viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
viewPager.setCurrentItem(position);
tabLayout.setScrollPosition(position, 0f, true);
}
});
Related
so i have a standard tabLayout that is in a viewpager.
But the very last tab needs to open a new activity. its only purpose is to open a new activity. and when the user returns from the activity by pressing the back button the last tab the user was at should be selected.
my tabs are custom views and there are 4 of them but the last one should be a button to trigger an event. so it will look just like a tab but really will be a button with a onclick listener. how can i acheive this ? i am wondering if a framelayout could be used and activated for the very last tab icon . thats the only way i can see to do it is to get the custom view and set its parent to be clickable that way it absorbs the click event. then i can set a onclick listener on the parent to open the activity. but then how to handle swipping to that tab ? swipping should open that activity as well.
Assuming you're using TabLayout.setupWithViewPager(), you can accomplish what you want by simply adding an extra tab and changing the OnTabSelectedListener.
// normal setup
tabs.setupWithViewPager(pager);
// our extra tab
tabs.addTab(tabs.newTab().setText("extra"));
// remove the `OnTabSelectedListener` created by `setupWithViewPager()`
tabs.clearOnTabSelectedListeners();
// add our own `OnTabSelectedListener`
tabs.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(pager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == pager.getAdapter().getCount()) {
// special case for the last tab in the list
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
else {
// otherwise, handle as normal
super.onTabSelected(tab);
}
}
});
I have a parent fragment which has a header view with tab layout & view pager.
I have 2 different views to be shown
view1 = headerView1 & TabLayout1 with ViewPager1
view2 = headerView2 & TabLayout2 with ViewPager2
when the fragment is launched I want to show view1 by default. On item click of a button in tabLayout of view1 I have to show the view2 and hide view1.
I am not able to figure out how to handle this. Any ideas?
Thanks
What have you tried so far? Can you post any code snippet? If I understand correctly, you have your TabLayout and ViewPager in your parent fragment. Attach a OnTabSelectedListener to your TabLayout, and override onTabSelected(TabLayout.Tab tab) method.
#Override
public void onTabSelected(TabLayout.Tab tab) {
//Change your viewpager pages here.
}
If you want to update your parent fragment from your View1 or View2 fragment, use the getParentFragment() method on your child fragments.
((YourParentFragment) getParentFragment()).doTask();
In your parent fragment,
public void doTask()
{
//Do your refreshing here.
//Or switch your pages in Viewpager.
}
Please post some code. That will get you better answers.
I have a main activity with 3 tabs,when i swipe the tabbed indicator doesn't move but the layout changes while selecting a tab the indicator moves and the layout changes too.
Make sure you have a OnPageChangeListener attached to your ViewPager
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mTabs.getTabAt(position).select();
}
});
You can also do like
tabLayout.setupWithViewPager(viewPager);
I have these 5 tabs and in the first tab/fragment I have a button, I want to be able to switch to another tab by clicking this button. Here's my code which contains the tabs:
actBar = getSupportActionBar();
actBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
secPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
vPager = (ViewPager) findViewById(R.id.pager);
vPager.setAdapter(secPagerAdapter);
vPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actBar.setSelectedNavigationItem(position);
}
});
Tab tab = actBar.newTab()
.setIcon(R.drawable.home)
.setTabListener(this);
actBar.addTab(tab, true);
tab = actBar.newTab()
.setIcon(R.drawable.cart)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.users)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.products)
.setTabListener(this);
actBar.addTab(tab);
tab = actBar.newTab()
.setIcon(R.drawable.settings)
.setTabListener(this);
actBar.addTab(tab);
This creates me a pretty nice action bar with tabs and all, and as you see the one with the home drawable has the below code:
actBar.addTab(tab, true);
since it is true, when this activity is opened it starts with this tab. So... I have a button within this tab. When I tap this button, I want it to scroll right through to the third tab which has the users drawable as an icon. I've seen things about tabhost around here and well, if that's the 'only' case, I gotta say, I don't know about tabhost. I tried to change that true boolean to be able to switch between tabs onClick but it didn't work.
Thanks in advance. I'd really appreciate it.
use this inside button click listener
actionBar.setSelectedNavigationItem(tab_position);
I need to show tab content on occasion, otherwise the area must be filled with "non-tabhost" data.However, tabs should be visible and when user clicks any of those tabs "non-tabhost" must be hidden and appropriate tab content must become visible.
It's something connected to a fake tab creation ?
Give an example of creating TabHost with tabs unselected.
Thanks.
What I usually do is, add an extra Tab and use setVisibility(View.GONE) to hide it. THis will just hide the tab button from the user, and the Tab will still be there, in the "background" and you can programmatically select it, by using tabHost.setCurrentTab(0). I also usually keep this tab as the first one.
1.copy the code where you want to tabs make unselected
tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
tabLayout.setTabTextColors(Color.BLACK, Color.BLACK);
2.Override on Tabselected Listener and paste the following code
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override`enter code here
public void onTabSelected(TabLayout.Tab tab) {
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
tabLayout.setTabTextColors(Color.BLACK, Color.RED);
viewPager.setCurrentItem(position);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#EB1C23"));
tabLayout.setTabTextColors(Color.BLACK, Color.RED);
viewPager.setCurrentItem(position);
}
});