I have some CardViews in my RecyclerView, and when I want to set space between them by adding margin, I get this:
As you can see, distance between cards is double. If I for example set margin just for top, left, and right, problem is solved but last card will touch the bottom of screen, and I don't want it. Like this:
As you can see, all spaces are equal and good, but there is no space in the bottom.
I have a MainActivity.java, activity_main.xml, recyclerview_row.xml, and MyAdapter.java in my project.
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
And this is my recyclerview_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="false"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/image_view"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
My question is how can I set equal distance between these Cards. Thank you.
Change your activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:paddingBottom="10dp"
android:clipToPadding="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
The magic is done by this line: android:clipToPadding="false"
Try This in Main Activity,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity"
android:paddingBottom="#dimen/dim_10"
android:background="#dddddd">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Related
Hi I'm new to Android Studio and currently in my activity_main xml I have this constraint layout with a few text views.
<android.support.constraint.ConstraintLayout
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:id="#+id/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.monash.fit2081.countryinfo.CountryDetails">
<TextView
android:id="#+id/textView"
android:layout_width="150dp"
android:layout_height="25dp"
//some code here
Now I want to change it to a fragment and make that activity as the bottom fragment.I made my fragment like this but how do nest the layout above into my frame layout?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="edu.monash.fit2081.database_4.MainActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_top"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fragment_border"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/fragment_top"
android:background="#drawable/fragment_border"
/>
</RelativeLayout>
Any help will be appreciated!
The FrameLayout is a ViewGroup just like RelativeLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/fragment_top"
android:background="#drawable/fragment_border"
>
<android.support.constraint.ConstraintLayout
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:id="#+id/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.monash.fit2081.countryinfo.CountryDetails">
<TextView
android:id="#+id/textView"
android:layout_width="150dp"
android:layout_height="25dp"
/>
</FrameLayout>
I am trying to center my progressbar within the fragements screen. Here is what I have tried so far and what in my opinion shoul be correct:
<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/recycler_row" />
</ScrollView>
</LinearLayout>
I tried deleting the wrapper layout, getting rid of the whole ScrollView (but as the visibility is set to gone, it shouldn't matter). The current result is, that the ProgressBar sticks to the top of the screen.
Might the cause be because of the Activity layout implementing a NestedScrollView?
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.mediasaturn.productlistpoc.activities.MainActivity"
tools:showIn="#layout/activity_main">
<fragment android:name="com.productlistpoc.recyclerview.RecyclerViewFragment"
android:id="#+id/fragment_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_recycler" />
</android.support.v4.widget.NestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Update your code like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/recycler_row" />
</LinearLayout>
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Don't use RecyclerView inside ScrollView. You have already used NestedScrollView as container layout so you don't need to add ScrollView.
Update your XML as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<RelativeLayout
android:id="#+id/layoutProgress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/pgFragment"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
#. If you want to show/hide prograssbar, just show or hide the layoutProgress programmatically using layoutProgress.setVisibility(View.VISIBLE) and layoutProgress.setVisibility(View.GONE) method.
Hope this will help~
i have a layout titled "status2.xml" and its code is posted below. and i have a main layout titled "meine_vertaege.xml" and its code is posted below as well. the "act_main.xml" includes the aforementioned layouts,
which are namely "status2.xml" and "meine_vertaege.xml".
I am including "status2.xml" and "meine_vertaege" in the "act_main.xml" as shown below in the code of the "act_main.xml".
the problem i have is when i include both of "status2.xml" and meine_vertaege" in the "act_main.xml", i cant see the entire contents of the "status2.xml" because it seems that the contents of "status2.xml" and "meine_vertaege.xml"
got mixed up
please let me know how to include both of "status2.xml" and "meine_vertaege" in "act_main.xml" so that i can see their entire contents in such way that they are organized vertically
status2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:paddingStart="10dp"
android:gravity="center_vertical"
android:text="Status"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="3dp"
android:layout_weight="5">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
android:paddingStart="10dp"
android:text="vertraege"/>
</LinearLayout>
</LinearLayout>
output of status2.xml:
meine_vertaege.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/actMain_meine_vertraege_layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="#dimen/padding">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/gray"
android:gravity="center_vertical|start"
android:paddingStart="#dimen/padding"
android:text="Meine Verträge" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/actMain_lisVie"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</ScrollView>
</LinearLayout>
output of meine_vertaege.xml:
act_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.bestandlayout_00.ActMain">
<include
android:id="#+id/lay1"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/status2"></include>
<include
android:id="#+id/lay2"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/meine_vertaege"></include>
</LinearLayout>
output of act_main.xml:
Try like this .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/lay1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1dp"
layout="#layout/status2"></include>
<include
android:id="#+id/lay2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1dp"
layout="#layout/meine_vertaege"></include>
</LinearLayout>
Vertical layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingTop="30dp"
android:paddingRight="16dp"
android:orientation="vertical"
tools:context="john.unitconverter.UnitConverter">
<fragment
android:id="#+id/unitConverter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="john.unitconverter.ConverterFragment"></fragment>
</RelativeLayout>
horizontal layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/unitConverter"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="john.unitconverter.ConverterFragment"></fragment>
<fragment
android:id="#+id/discConverter"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="john.unitconverter.DisclaimerFragment"></fragment>
</LinearLayout>
what's inside the second fragment
<FrameLayout 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"
tools:context="john.unitconverter.DisclaimerFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/disclaimer"
android:text="landscape mode is missing some features." />
</FrameLayout>
what happens is the second fragment doesn't show in landscape. why is that happening? basically, only the first fragment is showing up in the screen both horizontal and vertical layout.
thanks
As said in the comment you have to use the weight attribute:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/discConverter"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.trist_000.teststack.ArticleFragment"></fragment>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.1"/>
<fragment
android:id="#+id/unitConverter"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.trist_000.teststack.ArticleFragment"></fragment>
</LinearLayout>
My fragment layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#android:color/holo_red_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/disclaimer"
android:text="landscape mode is missing some features." />
</FrameLayout>
The result:
In my activity, I have two RecyclerViews inside a ScrollView. The problem is that when I swipe/flick the ScrollView, scrolling stops immediately. Is there a way to get the ScrollView to implement momentum or inertia so there's some deceleration before the scrolling stops?
My XML is below:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="#+id/threadload_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView 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:id="#+id/subforums"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_subforum"/>
<android.support.v7.widget.RecyclerView 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:id="#+id/threadlist"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_thread"/>
</LinearLayout>
</ScrollView>
I figured it out! All I had to do was set setNestedScrollingEnabled(false); to the two RecyclerViews, and everything started working like a charm.
You can disable nested scrolling directly in your XML with: android:nestedScrollingEnabled="false"
So your XML would look like:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ProgressBar
android:id="#+id/threadload_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView 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:id="#+id/subforums"
android:nestedScrollingEnabled="false"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_subforum"/>
<android.support.v7.widget.RecyclerView 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:id="#+id/threadlist"
android:nestedScrollingEnabled="false"
android:name="net.polunom.forum.fragments.ThreadFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="LinearLayoutManager"
tools:context=".fragments.ThreadFragment"
tools:listitem="#layout/fragment_thread"/>
</LinearLayout>
</ScrollView>