I've been playing around with CardViews inside a RecyclerView, but can't seem to get my cards centered.
My cardview.xml:
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_width="300dp"
android:layout_height="100dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/text"
android:textAlignment="center"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
My activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Image of what my app looks like when I run it:
Okok, I have the same question and I think I have "solved" it. It's really just another hack. The android team at Google really should fix this recycler view and add support for swipe to dismiss.
In onCreateView:
recyclerView.addItemDecoration(new ItemDecoration() {
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
int totalWidth = parent.getWidth();
int maxCardWidth = getResources().getDimensionPixelOffset(R.dimen.card_max_width);
int sidePadding = (totalWidth - maxCardWidth) / 2;
sidePadding = Math.max(0, sidePadding);
outRect.set(sidePadding, 0, sidePadding, padding);
}
});
You need to set LinearLayout's android:layout_width as match_parent in order for android:gravity="center" to take effect.
Had the same problem. My RecyclerView is inside a SwipeRefreshLayout, so I found out I could center it with the SwipeRefreshLayout.
<style name="swipe_refresh_cards_layout">
<item name="android:layout_width">#dimen/swipe_refresh_cards_width</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
I'm using a dimens.xml file for various screen sizes here.
Not the best solution IMO, but it works. I also found it to work (not using the solution above) if I put a LinearLayout as the parent to the SwipeRefreshLayout with
<style name="inner_layout">
<item name="android:layout_width">#dimen/swipe_refresh_cards_width</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:orientation">vertical</item>
</style>
Try wrapping up your Cardview inside a LinearLayout with width set to match_parent and android:gravity = "center".
Just set your parent's(in your case LinearLayout) width to match_parent.
Related
I am trying to make a transparent bottom sheet layout, that would allow me to see the contents of the view below it. The bottom sheet is working as expected, however when I set the background to either #null or #android:color/transparent, the layout's view is white, as opposed to transparent. My layout is as follows:
app_bar_main.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/coordinatorLayout"
android:background="#android:color/transparent"
android:fitsSystemWindows="true"
tools:context=".core.activities.MainActivity">
<!-- stuff here -->
<LinearLayout
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#null"
android:orientation="vertical"
app:layout_behavior="#string/bottom_sheet_behavior">
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
The linear layout with id bottom_sheet holds, well, my bottom sheet. The sheet itself is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#null"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_sheet_placeholder_layout"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:background="#null"
android:layout_height="50dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_sheet_layout"
android:layout_margin="0dp"
android:layout_weight="0.4"
android:layout_width="match_parent"
android:background="#color/my_background"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/my_progress_bar" />
<TextView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:textColor="#color/my_text"
android:id="#+id/txt_my_info"
android:layout_gravity="center_horizontal"
android:visibility="gone"
android:textSize="48px" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/txt_my_address"
android:textColor="#color/my_secondary_text"
android:visibility="gone"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/btn_edit_tree_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginTop="-62dp"
android:elevation="100dp"
android:src="#drawable/ic_create_black_24dp"
app:layout_anchor="#id/bottom_sheet_layout"
app:layout_anchorGravity="top|end|right"
app:useCompatPadding="true"/>
</android.support.design.widget.CoordinatorLayout>
The answer is much more easier: just add this line
myDialog
.getWindow()
.findViewById(R.id.design_bottom_sheet)
.setBackgroundResource(android.R.color.transparent);
And don't change standard ids (R.id.design_bottom_sheet and android.R.color.transparent). It makes background of Bottom Sheet Dialog transparent
private void setupDialogBackground() {
getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
BottomSheetDialog d = (BottomSheetDialog) dialog;
FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.design_bottom_sheet);
if (bottomSheet == null)
return;
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bottomSheet.setBackground(null);
}
});
}
add this in styles.xml
<style name="TransparentDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowCloseOnTouchOutside">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:colorBackground"> #android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.3</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
</style>
BottomSheetDialog bsDialog = new BottomSheetDialog(this,R.style.TransparentDialog);
bsDialog.setContentView(R.layout.bottomsheet_dialog);
bsDialog.show();
and voila it works
I had to wait until after setContentView was called in onActivityCreated in order to access the container. Also while getDialog().getWindow().findViewById(R.id.design_bottom_sheet) did successfully find the FrameLayout, I decided to avoid explicitly calling out the internally defined id by using getView().getParent().
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); // setContentView called here
((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}
Add this in your bottomSheet setupDialog(final Dialog dialog, int style)
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
and also add android:background="#android:color/transparent" to root view
Just need to use this style:
<style name="BaseBottomSheetDialogThem" parent="#style/Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
and set your style to BaseBottomSheetDialogThem.
Here is the kotlin version thanks to kolorszczak . Hope it save someones time converting .
val dialog = BottomSheetDialog(activity!!)
dialog.setOnShowListener {
var dialogTmp: BottomSheetDialog = it as BottomSheetDialog
var bottomSheet: FrameLayout? =
dialogTmp.findViewById(R.id.design_bottom_sheet) as FrameLayout?
?: return#setOnShowListener
bottomSheet?.background = null
}
Create in your color.xml : <color name="colorTransparent">#00000000</color>
and use android:background="#color/colorTransparent"
change color background
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottom_sheet_placeholder_layout"
android:layout_weight="0.6"
android:layout_width="match_parent"
android:background="#00FFFFFF"
android:layout_height="50dp"
android:orientation="horizontal">
</LinearLayout>
Try it!!
Here is my tab layout XML
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/custom_tab_layout_height"
android:background="#color/tab_background_primary"
app:tabGravity="fill"
app:tabIndicatorColor="#color/primary_white"
app:tabIndicatorHeight="3dp"
app:tabMinWidth="120dp"
app:tabMode="scrollable"
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"
app:tabPaddingTop="1dp"
app:tabPaddingBottom="1dp"
/>
It is removing the horizontal padding in between tabs but not the tabPaddingTop and tabPaddingBottom.
How do I remove the top and bottom padding to make each tab match the tabLayout height?
Custom view for each tab
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tab"
android:textColor="#color/primary_white"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="fill"
android:fontFamily="#string/font_fontFamily_medium"/>
I also tried using a Linear Layout with Imagview and Textview as custom view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:gravity="fill"
android:layout_height="match_parent">
<ImageView
android:id="#+id/tab_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#mipmap/ic_settings_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/tab_title"
android:textColor="#color/primary_white"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="center"
android:text="test"
android:fontFamily="#string/font_fontFamily_medium"/>
</LinearLayout>
And here is how I inflated the custom tab view (for custom view with textView)
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("CASH IN");
tabTwo.setBackgroundColor(Color.parseColor("#EC5A0B"));
tabTwo.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_action_cash_light, 0, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
As you can see, I am trying to give different background color to each tab. But the padding at top and bottom is still there.
I think Android design library's TabLayout have default padding like what you can see here
The other recommendation that I can recommend to you is using Google IO's SlidingTabLayout (Don't forget to copy SlidingTabStrip too!)
The usage is simple, just include in your layout :
<com.myapp.ui.widget.SlidingTabLayout
android:id="#+id/main_view_pager_tab"
android:layout_width="match_parent"
android:layout_height="#dimen/tabs_height"
android:background="#color/white" />
and then in your activity set the viewpager and listener (if you want)
mainViewPagerTab.setViewPager(mainViewPager);
mainViewPagerTab.setOnPageChangeListener(onPageChangeListener);
This was the only solution
https://stackoverflow.com/a/37942842/5689605
Try the code, after adding your tabs to your tablayout.
final ViewGroup test = (ViewGroup)(tabs.getChildAt(0));//tabs is your Tablayout
int tabLen = test.getChildCount();
for (int i = 0; i < tabLen; i++) {
View v = test.getChildAt(i);
v.setPadding(0, 0, 0, 0);
}
I got the best solution for this issue, check the below code and this will helpful.
My tablayout XML is like this:
<RelativeLayout
android:layout_height="30dp"
android:background="#333333"
android:layout_width="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextAppearance="#style/MineCustomTabText"
style="#style/tab_bassr"
app:tabMode="scrollable"/>
</RelativeLayout>
the relative layout upon this will reduce the top and bottom margin
<style name="tab_bassr" parent="TextAppearance.Design.Tab">
<item name="android:layout_width">match_parent</item>
<item name="android:tabStripEnabled">false</item>
<item name="tabPaddingStart">5dp</item>
<item name="tabPaddingEnd">5dp</item>
</style>
<style name="MineCustomTabText" parent="TextAppearance.Design.Tab">
<item name="android:textSize">14sp</item>
</style>
The style file is for left and right padding and the text size.
I have an activity that shows extra space at the bottom even though I have no bottom padding applied. Style code, xml code, java code and a screenshot are below and the relevant json data is here:
http://lara.rufflecol.es/strollcharlton/strollcharlton_data_1_2.json
It isn't a huge issue it is just annoying! Have already tried to using android:fillViewport set as true or false but it doesn't seem to make any difference. Is the problem related to my json?
UsefulLinks.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#color/dark_green"
android:minHeight="?attr/actionBarSize" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/useful_links"
style="#style/UsefulLinksTextStyling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left" />
</LinearLayout>
</ScrollView>
</LinearLayout>
styles.xml
<style name="UsefulLinksTextStyling" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/white</item>
<item name="android:textSize">#dimen/fifteen_size</item>
<item name="android:paddingTop">15dp</item>
<item name="android:paddingLeft">15dp</item>
<item name="android:paddingRight">15dp</item>
</style>
UsefulLinks.java
TextView usefulLinksIntroTextView = (TextView) findViewById(R.id.useful_links);
usefulLinksIntroTextView.setText(Html.fromHtml(data.getUsefulLinks()));
usefulLinksIntroTextView.setMovementMethod(LinkMovementMethod.getInstance());
Screenshot
ensure the html that you are setting to the textview does not having blank space inside of it
change your base LinearLayout to wrap_content so that if the html takes up less than the screen height you don't have "extra padding"
You can also turn on "show layout bounds" for your application, that way you will understand which View is giving you the "extra padding" http://tysmith.me/post/27035355999/layoutbounds combine it with the Hierarchy Viewer http://developer.android.com/tools/performance/hierarchy-viewer/index.html to work out which View is at fault
I cannot find a way how to make linearLayout opaque. It's always transparent no matter what I do.
The layout looks like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="vertical"
android:id="#+id/error_panel"
android:layout_width="match_parent"
android:background="#color/gray_background_dark"
android:layout_height="wrap_content">
<TextView
android:id="#+id/error_message"
android:text="#string/dummy_number"
android:gravity="center_vertical|left"
android:textColor="#color/red"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- The content of this scroll view is seen underneath the errorPanel -->
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- Content of ScrollView -->
</ScrollView>
</FrameLayout>
How can I make the LinearLayout opaque? Maybe instead of setting just color I could try to use a solid drawable..?
Background
I am using appcompat v7 theme <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
Things I tried
Override the theme's panelBackground from transparent to <item name="android:panelBackground">#android:color/black</item>
Set background as background=#FF000000 on LinearLayout.
Set alpha as alpha=1 on LinearLayout.
Set background as drawable android:background="#drawable/background_error_panel" where the drawable is:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FF181818"/>
</shape>
Edit 3/25/2015
Maybe the animations are the problem..? i use scale anims and in code:
public void toggle() {
panel.setText("Some text Some text Some text Some text Some text ");
Animation slideIn = AnimationUtils.loadAnimation(MyApplication.context, R.anim.slide_in);
Animation slideOut = AnimationUtils.loadAnimation(MyApplication.context, R.anim.slide_out);
if (layout.getVisibility() == View.GONE) {
layout.setVisibility(View.VISIBLE);
layout.startAnimation(slideIn);
layout.setAlpha(1);
} else {
layout.startAnimation(slideOut);
layout.setVisibility(View.GONE);
}
}
It is your ScrollView being transparent. As per the docs,
Child views are drawn in a stack, with the most recently added child on top.
So you need to load the ScrollView first to have it in the background.
I've got a RecyclerView and would like to have scrollbar showing, when it covers more than one page.
I get no scrollbar at all. Any idea?
My layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/cl_only_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
android:text="#string/cl_only_empty"
android:textColor="#color/white" />
<android.support.v7.widget.RecyclerView
android:id="#+id/callsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
The solution is to set the vertical (or horizontal) scrollbar in the xml layout:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
Use android:scrollbars attribute "vertical" and android:scrollbarThumbVertical attribute to set the color and android:scrollbarSize attribute to specifiy size:
<android.support.v7.widget.RecyclerView
android:id="#+id/document_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:scrollbars="vertical"
android:scrollbarThumbVertical="#android:color/darker_gray"
android:scrollbarSize="5dp"
android:background="#color/activity_bg"
android:dividerHeight="4dp" />
use recyclerView as below in xml layout
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:scrollbars="vertical"
android:fadeScrollbars="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
and add below code for scrollview in java, it will be okay
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(true);
I had the same problem on my old HTC Desire X (api 16) only.
I don't know why, but scollbars of RecyclerView doesn't work properly on this device if the android:background property is not set. Try to set it to any color or to transparent - it works for me, hope it helps you too
You can use from :
setScrollbarFadingEnabled(boolean)
Scrollbar Link
Scroller can be set to recyclerview on multiple ways.
1st you can simply add scrollbar in xml and set its property android:fadeScrollbars="false" to always show it.
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewMachine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:scrollbarThumbVertical="#android:color/darker_gray"
android:scrollbarSize="5dp"
android:scrollbarStyle="outsideOverlay"/>
Or you can make a style theme and use it programitically when initializing recyclerview
<style name="ScrollbarRecyclerView" parent="android:Widget">
<item name="android:scrollbars">vertical</item>
</style>
RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
thanks
Besides the android:scrollbars attribute, you should add android:fadeScrollbars attribute in false state like this:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:fadeScrollbars="false"/>
This way, the vertical scrollbar is always showing when it's using more height of the layout permitted.
Try this:
mLayoutManager = new LinearLayoutManager (this);
mLayoutManager.setSmoothScrollbarEnabled (true);
If you are fine to set ScrollBar programmatically, then you can use ContextThemeWrapper. First you need to define styling in Style.xml file:
<style name="ScrollbarRecyclerView" parent="android:Widget">
<item name="android:scrollbars">vertical</item>
</style>
And then apply styling when you initialize your RecylerView:
RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
Add the code to Recycler view xml to make scroll bar visible.
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="false"
android:scrollbars="vertical"
android:scrollbarSize="#dimen/_2sdp"
android:scrollbarThumbVertical="#color/white"/>
Actually, it's because of the theme. The scrollbars are there but can't be seen because they blend with the color on the parent view or some other view in the line of its ancestors.
You can create a drawable shape and give it the color you want then set that as the vertical or horizontal scrollbar drawable. That is, if you do not want to mess around with your theme colors.
Using xml android:fadeScrollbars="false"
Using java ScrollView.setScrollbarFadingEnabled(false);