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.
Related
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
picture of content main
picture of a menu item fragment
I tried everything
i set background color to white and clickable to true and in the main activity i've writed this "btn.setVisibility(View.GONE);" to avoid seeing the button too
thanks to everyone
Try this
Set a background colour (white) for the Fragment layout's parent container and also set clickable="true"
android:background="#android:color/white"
android:clickable="true"
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.
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 am trying to set a background image to my ViewPager (android.support.v4.view.ViewPager). It can be done via android:background="#drawable/bg" - however, it stretches the image.
For ImageView's I can set scaleType="centerCrop" to prevent this but this doesn't seem to work for PagerView. How can I prevent the background from stretching? I've tried adding an ImageView as a subview of my ViewPager but that doesn't seem to work either.
Thanks in advance!
Sorry, there are no 'centerCrop' option for background drawable. How I do it is set your use FrameLayout having the back contain the ImageView using centerCrop, then the front(top) will be your content with transparent background.