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);
}
});
Related
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);
}
});
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 am exploring different Android app UIs. I am trying to make one that has two columns, and on each column is a button that shifts into a different screen, but the two columns remain stationary.
An example to what I'm trying to replicate: http://i.imgur.com/a2IqkV1.jpg
The 'list' and 'number' screens each have their own separate layouts, I think I need to do something with fragments, but I don't know where to start.
Thanks
When you dont know where to start you can go for TabLayout : TabLayout and ViewPager : ViewPager and ofcourse Fragment :Fragment .You can create a layout like in the image by using this 3 items. Also you can follow this guide to create a screen like in the image, guide is so good for beginners : google-play-style-tabs-using-tablayout
Additionally if you want to switch your Fragment when any of tab clicked, add this code :
tablayout.addOnTabSelectedListener(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) {
}
});
I've seen several apps smoothly changing their indicator color (even icons!) on tab swipe for example Facebook.
This is how I do it in my app :
tabLayout.setOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(searchViewPager) {
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
tabLayout.setSelectedTabIndicatorColor(getResources().getColor(indicatorColors[tab.getPosition()]));
}
});
Where indicatorColors is an array of normal color, and thus it only changes when user completely swipes to a tab or select the tab. I need the transition here.
What can I do to get a smooth color transition from one color to another color on tab swipe?
TabLayout is coupled with a ViewPager.
Please try this.
...onCreate(){
...
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
setTabColor(tabHost);
}
});
setTabColor(tabHost);
...
}
//Change The Backgournd Color of Tabs
public void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(COLOR_CYAN);
}
//unselected
if(tabhost.getCurrentTab()==0){
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_RED); //1st tab selected
} else {
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(COLOR_BLUE); //2nd tab selected
}
}
I have a tab layout with a view pager and 5 different tabs, if the user is not registered only one option is available, so I want to disable the click on the other tabs. What I did is override the onTabSelected to change the current item in the viewPager.
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (User.current != null) {
viewPager.setCurrentItem(tab.getPosition());
} else {
viewPager.setCurrentItem(0);
}
}
It works perfectly but it has one problem, the tab indicator change to the selected tab, so I want to keep the tab indicator on the first tab.