I want to have book shelves in my application, so I used cards view, but I can not decrease the distance between the cards from the sides.
everything I try changes the distance from the top and bottom.
this is the CardView xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="110dp"
android:layout_height="180dp"
xmlns:cardview="http://schemas.android.com/tools"
android:layout_marginBottom="5dp"
android:layout_marginEnd="0dp"
cardview:cardCornerRadius="10dp"
cardview:cardElevation="0dp"
app:cardUseCompatPadding="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/shelf_img"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#drawable/shelf" />
<ImageView
android:id="#+id/book_img"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#2d2d2d"
android:scaleType="centerCrop"
/>
<ImageView
android:id="#+id/check_box"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/v_pink"
android:visibility="invisible"/>
<TextView
android:visibility="gone"
android:id="#+id/book_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Name"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
and the recycler view defined like this:
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/progress_bar"
android:layout_marginTop="16dp">
</android.support.v7.widget.RecyclerView>
and in the activity -
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
eventually it looks like this:
I want the cards to be close to each other from the sides so it will look like one long shelf...
plaease any help??
also if someone knows how do i put the first ImageView ("book_img") in front of the second ("shelf_img") it will be awesome... the only way i succeeded now is one above the second
edit : i succeeded this one with RelativeLayout
thanks!
Related
I am new to android and trying to make a profile section of a social media app. The profile section is a fragment and inside this I wanted to have a CardView to hold user's information and then a RecyclerView to show user's posts. I have tried implementing this using a ScrollView and putting insinde the cardView and RecyclerView. I wanted both cardView and RecyclerView to be scroll as one, but currently the cardView is at its place and only elements in recyclerView are scrolling.
EDIT
NestedScrollView kind of worked for me. But I also wanted overScrollMode to work fine while scrolling to the end.
<ScrollView 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"
tools:context=".ui.profile.ProfileFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
style="?attr/materialCardViewFilledStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<androidx.cardview.widget.CardView
android:layout_width="120dp"
android:layout_height="120dp"
app:cardCornerRadius="100dp">
<ImageView
android:id="#+id/profileDisplayPicture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/user_profile_picture"
android:scaleType="centerCrop" />
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/profileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
<TextView
android:id="#+id/profileCollegeInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.divider.MaterialDivider
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/profileLinkedinBtn"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_margin="10dp"
android:contentDescription="#string/linkedin"
android:src="#drawable/ic_linkedin" />
<ImageView
android:id="#+id/profileMailBtn"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_margin="10dp"
android:contentDescription="#string/mail"
android:src="#drawable/ic_gmail" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/profileRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
i have a task_layout.xmland i wanted to add Programmatically it to card view layout file that already adapted with recyclerView which works so good with <include>command in xml file
Cardview_layout.xml that adapted with recyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/CARD_VIEW"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:layout_marginHorizontal="25dp"
android:elevation="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="#+id/LIST_NAME_TEXT_VIEW"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Groceries"
android:fontFamily="#font/segoeuib"
android:textSize="21sp"
android:textColor="#000000" />
<!-- Here where i want to add the child layout -->
</RelativeLayout>
</android.support.v7.widget.CardView>
the layout task_layout.xml i want to add programmatically and i tried a lot of things and it always crash
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="#+id/CHECK_B0X_TASK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:allowUndo="true"
android:text="Task1"
android:textSize="17sp"
android:fontFamily="#font/segoeui"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/TEXT_CLOCK_TASK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9:00 AM"
android:textSize="17sp"
android:fontFamily="#font/segoeui"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
in the main activity i typed that but still not working it always crash the application even the <include> tag in the xml file is working
CardView cardView = findViewById(R.id.CARD_VIEW);
View child1 = LayoutInflater.from(this).inflate(R.layout.task_layout,null);
cardView.addView(child1);
Try this...
CardView cardView = findViewById(R.id.id_cardview);
Create an object to your layout
View child1 = LayoutInflater.from(this).inflate(
R.layout.extra, null);
cardView.addView(child1);
Basically what I need is overlap the views
View to achieve
Each cardview is an item and will be contained within a recyclerview
this is how my card view is defined
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="-11dp">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
>
<android.support.v7.widget.CardView...>
</ScrollView>
<ImageView
android:layout_width="70dp"
android:layout_height="40dp"
android:src="#drawable/img_food_little"
android:layout_alignParentEnd="true"
android:layout_marginEnd="40dp"
/>
</RelativeLayout>
And looks like (The blue one is the RelativeLayout):
How my item card view
Please Help
Try to replace the linear layout in the following with cardview and use the resultant layout as the layout for the recyclerview items .
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="top"
android:layout_marginTop="20dp"
android:background="#cccccc"
android:layout_height="100dp">
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:src="#mipmap/ic_launcher"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:layout_height="wrap_content" />
</FrameLayout>
Hope this helps.
So I have recyclerView with height wrap_content as I understand this means that recyclerView should expand and collapse whenever I add items to it or remove, but for some reason in NestedScrollView it doesn't behave like that when I add new Objects to it, it stays same size as it was any ideas what could be wrong?
Tried adding
android:fillViewport="true"
But no reactions as far as I understand it could be because RecyclerView not getting updated when children of it changes, because when I restart app it collapses or expands to proper size.
Here is my xml:
<?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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/icons"
android:orientation="vertical">
<LinearLayout
android:id="#+id/main_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested"
android:fillViewport="true"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/empty_group_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/empty_text_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/no_group_first"
android:textColor="#color/secondary_text"
android:textSize="17sp" />
<ImageView
android:id="#+id/add_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/empty_text_first"
android:src="#drawable/ic_add_group_gry" />
<TextView
android:id="#+id/empty_text_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/add_icon"
android:text="#string/no_group_second"
android:textColor="#color/secondary_text"
android:textSize="17sp" />
</RelativeLayout>
</RelativeLayout>
<TextView
android:textSize="14sp"
android:layout_marginStart="6dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Favorite groups"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/favorite_group_list"
android:nestedScrollingEnabled="true"
android:layout_width="match_parent"
android:layout_height="156dp"
android:layout_gravity="center"
android:background="#color/icons"
android:padding="6dp">
</android.support.v7.widget.RecyclerView>
<TextView
android:textSize="14sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginStart="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Groups"/>
<android.support.v7.widget.RecyclerView
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/group_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:nestedScrollingEnabled="false"
android:padding="6dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_new_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="#drawable/ic_playlist_add_48px"
app:behavior_autoHide="true"
app:layout_anchor="#id/main_holder"
app:layout_anchorGravity="bottom|right|end" />
Hi kosas give wrap _content for recycler view and while setting layout
manager follow this
linearLayoutManager = new LinearLayoutManager(getActivity()) {
#Override
public boolean canScrollVertically() {
return false;
}
};
Probably the problem is related with the adapter's layout. Be sure to put some fixed value or wrap_content on the adapter's layout height. If it's match_parent it won't work as expected because each row will fill all the RecyclerView
I have 2 recyclerviews in my activity 1st one is horizontally scrollable and the 2nd one is vertically scrollable. (XML code given below)
But the scrolling is not smooth.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
android:clickable="true"
>
<ProgressBar
android:id="#+id/progressBarCircularIndetermininatesearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="#f44336"
android:layout_centerInParent="true"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/progressbacklin"
android:scrollbars="none"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/videolist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_alignParentTop="true"
android:scrollbars="none"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_below="#+id/videolist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:scrollbars="none"
/>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:id="#+id/progressbacklin"
android:layout_above="#+id/adViewsearch"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="90dp"
android:layout_marginTop="5dp"
android:background="#drawable/white"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
>
<ProgressBar
android:id="#+id/progressBarCircularIndetermininate"
android:layout_width="24dp"
android:layout_height="24dp"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="#f44336"
android:layout_centerInParent="true"/>
</LinearLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adViewsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id" />
</RelativeLayout>
i use
setNestedScrollingEnabled(false);
for both recycler view.
Any ideas to implement this with a smooth scrolling?
Change ScrollView to NestedScrollView.
In order this to work, you need to have RecyclerView with height wrap_content inside NestedScrollView and you need to set recyclerView.setNestedScrollingEnabled(false) from code.
How i solved this problem is by using a single Recycler view instead of two and setting up the other (1st recycler view in question) recycler view as first item of it.
Now it works perfect and scrolling smoothly.