I have an app in which I have added TabLayout with three tabs. I want to show tab indicator from bottom to top but the problem is that when I show indicator from bottom to top tab icon also rotate.How do I resolve this issue?
mTabLayout.getTabAt(0).setCustomView(view);
mTabLayout.getTabAt(1).setIcon(tabIcons[1]);
mTabLayout.getTabAt(2).setIcon(tabIcons[2]);
mTabLayout.setRotationX(180);
I have similar requirement in my previous application
Please try below code it works perfectly for me
//First rotate the tab layout
tabOrderType.setRotationX(180);
//Find all childs for tablayout
for (int i = 0; i <tabOrderType.getChildCount() ; i++) {
LinearLayout linearList = ((LinearLayout)tabOrderType.getChildAt(i));
for(int position = 0;position<linearList.getChildCount();position++) {
//One by one again rotate layout for text and icons
LinearLayout item=((LinearLayout) linearList.getChildAt(position));
item.setBackground(getResources().getDrawable(R.drawable.custom_button_border));
item.setRotationX(180);
}
}
This is working nicely in my application and not creating any issue till now.Another way is you need to make custom tab to manage this.
Hope this helps you...if any issue please ask me...
Add this in your TabLayout XML
app:tabIndicatorGravity="top"
Related
I'm using the new support TabLayout from Android. The thing is that I wanted to use selectors to change the icon when a tab is selected.
I've been looking into the source code and it seems to me that the it never changes the state of the view (and for that reason I can't use the selector).
Does anyone knows some workaround?
Thank you!
Assume your my_selector.xml is,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_on" android:state_selected="true"/>
<item android:drawable="#drawable/icon_off"/> <!-- default -->
</selector>
then you can call setIcon directly,
tab.setIcon(R.drawable.my_selector);
Verified with 'com.android.support:design:22.2.0'.
I found that when I first set the custom view for each tab in the TabLayout I need to set the first one (index 0) as selected.
TabLayout toolbarTabLayout = (TabLayout) findViewById(R.id.tabs);
toolbarTabLayout.setupWithViewPager(mViewPager);
toolbarTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
toolbarTabLayout.setTabMode(TabLayout.MODE_FIXED);
toolbarTabLayout.setTabTextColors(R.color.colorPrimary, R.color.white);
// Iterate over all tabs and set the custom view
for (int i = 0; i < toolbarTabLayout.getTabCount(); i++) {
TabLayout.Tab tab = toolbarTabLayout.getTabAt(i);
View v=mSectionsPagerAdapter.getTabView(i);
// no tabs are actually selected at start, this will make sure the
// selector for the colors comes in right when initialized
if (i==0)
v.setSelected(true);
tab.setCustomView(v);
}
This seems to force the first tab as selected when the custom view is applied. It really feels like a hack, hopefully someone else will figure out the real issue and propose a better fix.
There is way to set customView as a tab with setCustomView(View view) method. So you can create a textview and set a selector to it and set this view to tab.
Hope it helps you!
If you did everything right (and i believe this) so you arrived at same point than me. Maybe it is a little bug in new android appcompat library.
i found an workaround (it's called Gambiarra in a good Portugues) to solve this problem. you need to call the method select() from Tab class like this:
mTabLayout.getTabAt(x).select();
BUT it's very important: x variable must be different than current selected tab index.
This is what worked for me:
Assuming you have your selectors set in the drawable res folder (like Xingang Huang showed above).
In your MainActivity (where you setup your TabLayout) you include your array of icon selectors and then you loop through it like this:
for (int i = 0; i < yourTabLayout.getTabCount(); i++) {
ImageView imageView = new ImageView(this); //your context, in this case MainActivity.class
imageView.setImageResource(arr_tabIcons[i]); //tabIcons is the array of icons
if (i==0) {
imageView.setSelected(true);
}
yourTabLayout.getTabAt(i).setCustomView(imageView);
}
tab.setIcon(R.drawable.icon)
works as well but in my case the icons looked really small, so I had to use the solution with the ImageView to fill the tab view.
Happy coding ;)
I'm using PagerSlidingTabStrip library in my application now. I have 5 tabs, so it's over screen width. so I have to scroll to see last tab.
I want to see all tabs on the screen and never wanna scroll to see other items.
I tried changing HorizontalScrollView to LinearLayout in PagerSlidingTabStrip.java but it's weird a little. Indicator moved bad.
// public class PagerSlidingTabStrip extends HorizontalScrollView
public class PagerSlidingTabStrip extends LinearLayout
and also I tried shouldExpand options is true. but it didn't work again.
app:pstsShouldExpand="true"
What can i do for this ????
I had exactly same problem but following code ,I found that if you need to set tabs shouldExpand you need to do it before setting viewpager to tabs.
tabs = (PagerSlidingTabStrip) findViewById(R.id.slidingTabStrip);
//Before setting view pager
tabs.setShouldExpand(true); //Works
tabs.setViewPager(vpPager);
//After setting view pager
tabs.setShouldExpand(true); //Will not work
I solved this problem by myself. The problem is shouldExpand Attr doesn't work because our tabs are 5 so I can't. but when I set my tab count 4, it works and looks good. They filled with device screen width.
Anyway I changed this width size.
defaultTabLayoutParams = new LinearLayout.LayoutParams(dm.widthPixels/your tab count, LayoutParams.MATCH_PARENT);
I hope it will be helpful and save your time.
I'm using this library for pagerSlidingTabStrip Component. I have TabStrip which occupies full screen in Portrait Orientation. But In Landscape it doesn't occupy full screen.
I'm using app1:paddingLeftRight attribute in tabstrip tag in xml, but it is not working for me. I want to setPadding programmatically for each and every device both in landscape and portrait mode. So I will place the Tabs items equally.
Any Help greatly appreciated...........
Thanks in advance
Try do this in updateTabStyles() in class PagerSlidingTabStrip in library. Something like tab.setPadding(left, top, right, bottom).
This thread might be old, however I found a solution for this. For those who are looking (still) for solution try this :
tabs.setViewPager(pager);
/*
Add top and bottom padding
*/
LinearLayout parent = (LinearLayout)tabs.getChildAt(0);
int padding = 20;
for (int i = 0; i < parent.getChildCount(); i++) {
View tab = parent.getChildAt(i);
tab.setPadding(tab.getPaddingLeft(), padding tab.getPaddingRight(), padding);
}
Put this code after setting your viewpager, or else it will read parent with 0 children.
Cheers, Happy coding.
I have tab layout.That has only icons not text.I tried with clickOnImageButton and ClickOnButton clickOnImage and also pressOnMenuItem(R.drwable.icon)but not worked.How can i do this with solo?
Note: Image View present at the top of the tab.(tab is at bottom)
Tabhosts are evil. Luckily i also have had to automate them and so know the answer.
what you do is you have to get the tab bar view (android.R.id.tabs) and then cast it to a Tabhost or a ViewGroup then you can get each of the tabs via .getChildAt(x) where x is the index of the tabs.
ViewGroup tabs = (ViewGroup) solo.getView(android.R.id.tabs);
View viewYouWantToDoStuffWith = tabs.getChildAt(x); //change x to the index you want.
In your case you would then want something like:
solo.clickOnView(viewYouWantToDoStuffWith);
you can use method solo.clickOnView(solo.getView(resourceId));
where resourceId could be something like R.id.id_Of_Button.
see this link in that they are using image + text. so remove the text made as image that you have to do it.
http://www.androidhive.info/2011/08/android-tab-layout-tutorial/
this is only for text you can set that with image and made as custom tab.
http://www.androidpeople.com/android-tabhost-tutorial-part-1
Hope it will work for you .
Hai, I am new android developer. I want to create a Tab host application . There will be 3 different tab items .Each item is attached with map view. I want to change the zoom label of each map when tab each item. How can i do it...
In each tab item , set programatically the following line with MapView:
mapView.setBuiltInZoomControls(true);
mapView.getController().setZoom(13); //set zoom level according to need for each tab item
Hope this will solve your issue.
Use this event
public void onTabChanged(String arg0) {
int currentTabvalue = tabHost.getCurrentTab();
if(currentTabvalue==0)
mapView.getController().setZoom(----);
else if(currentTabvalue==1)
mapView.getController().setZoom(----);
else if(currentTabvalue==2)
mapView.getController().setZoom(----);
}
I thinks this is the code you want to achieve this.
In this code you fill the value in the place of ----- according to your requirement.
I hope you have got your answer.