Increase width custom view on tab in tablayout - android

I currently have a tablayout with a custom tab. Currently the tabs display great the only issue is that the tabs seem to be confined to the tablayout tab size leaving a space in between the next tab. Is there a way to make the custom view on the tab match the tablayouts tab size? Please let me know.
custom_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="50dp">
textview element
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
TabLayout tabLayout = findViewById(R.id.tabLayout);
new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
#Override
public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
tab.setCustomView(R.layout.custom_tab);
//get textview and set custom text
//set background color
}
}).attach();

After some digging I was able to find an answer. The problem is when using a custom view, the tablayout inflates the view to a maximum width and height. The best way to add a background that fully matches the full tablayout tab width and height is to use the tablayout xml tabBackground and add a drawable that includes a selector plus the textColor and textSelectedColor. Follow this tutorial for more information: https://www.thecodecity.com/2016/12/changing-background-color-of-tab.html#:~:text=%20How%20to%20change%20tab%20color%20in%20TabLayout,create%20as%20the%20background%20of%20the...%20More%20

Related

Hide Tablayout in some Fragments

I would like to hide my Tablayout in some Fragments my app will show up. One solution would be setting the android:aylout_height attribute of the TabLayout to eiher "0dp" or "wrap_content". But how do I set the height to 0dp programmatically? Is that even possible with a TabLayout?
In the fragments that you want your TabLayout to not show...
tabLayout.setVisibility(View.GONE);
I would put the above part of your code in your fragment's onCreateView() method.
Then, in fragments where you want it to show again:
tabLayout.setVisibility(View.VISIBLE);
xml:
android:visibility="gone"
code:
TableLayout layout= (TableLayout ) view.findViewById(R.id.layout_table);// change id here
layout.setVisibility(View.GONE); //VIEW.VISIBLE etc.

Android TabLayout Inconsistent Tab Label Size

Problem
To my understanding, the guidelines of material design mention that tab single-line labels should not be resized: https://www.google.com/design/spec/components/tabs.html#tabs-usage
However, my tabLayout changes the size of a label and looks awful as seen in this picture:
S6-TabMode-Fixed
TabLayout Code:
mTabLayout = (TabLayout) findViewById(R.id.company_tabs);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.setBackgroundColor(actionbarColor);
mTabLayout.setTabTextColors(Color.WHITE, Color.WHITE);
mTabLayout.setSelectedTabIndicatorColor(Color.WHITE);
mTabLayout.setTabMode(TabLayout.MODE_FIXED);
TabLayout XML:
<android.support.design.widget.TabLayout
android:id="#+id/company_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Attempted:
Changed tabmode to TabLayout.MODE_SCROLLABLE, this resulted in tabs having the correct text size, but being left-aligned.
Experimented with favorites label, shortening the text to "favorit" seemed to fix the tab size from being resized, but this is not an acceptable solution
Question: What is causing this behavior, and what can be done so that the tabs fit the full width of the screen, but the text size of the labels remains unchanged?

PagerSlidingTabStrip making horizontalscrollview's width fixed

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.

How to change Android ViewPager Background Color?

ViewPager Background is grey. I can't find on internet any clear article that shows how to just change that background color in black for example.Any idea will be appreciated.
You can achieve it in 2 ways,
1.Add the following in your ViewPager in the xml
android:background="#000000"
2.Do it dynamically like below,
ViewPager pager= (ViewPager)findViewById(R.id.pager);
pager.setBackgroundColor(Color.BLACK);
Try this
private ViewPager mPager;
mPager = (ViewPager) view.findViewById(R.id.pager);
mPager.setBackgroundColor(Color.BLACK);
A ViewPager is not grey by default, it's transparent like other ViewGroups, so you will see your parent layout background or window background by default.
If you want to change the background color of your whole Activity, I suggest that you override the android:windowBackground attribute in your Activity's theme, which is more efficient.
You can set android:background="#android:color/transparent" to ViewPager in your xml file
and set android:background="#android:color/black" for parent layout.
I hope this will help.

Set width of tabs

I found a lot of questions about this topic, but no working answers. So I have to ask the same question again...
I have a Fragment in which I am displaying two Tabs. On smartphone devices everything looks fine. Just like I want to. But on tablets (or generally on larger screens) the two Tabs are centered, that there are two "gaps", left an right of the Tabs.
Now I want to get rid of these gaps. My favorite solution would be to stretch both Tabs to cover the complete screen width. If that is not possible, I at least want to change the color of the TabBar.
I create and add my tabs like this (I left out the text and TabListener creation and assignment):
Tab tabA;
Tab tabB;
final ActionBar myActionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
myActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
myActionBar.addTab(tabA);
myActionBar.addTab(tabB);
When I try something like this
myActionBar.getTabAt(0).getCustomView().getLayoutParams().width = 200;
I always have a NullPonterException. I also tried to use a CustomView for the tabs. I created a new TabWidget.xml Like this:
<?xml version="1.0" encoding="utf-8"?>
<TabWidget xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dip"
android:layout_height="match_parent"
>
</TabWidget>
But no chance. The tab size just does not change.
Just found this post: Stacked ActionBar tab bar not filling parent
It seems as if it is not possible to change the Tab width to a value bigger 160 dip. So for the moment I have to use plan b and change the color of the TabBar.
Instead of tabs, try to place image view with background image

Categories

Resources