View.gone showing empty space in Recyclerview android - android

I am using recyclerview for showing data from web service. Based on particular condition i need to remove the particular recycler view item. So i am using View.Gone. But it is showing empty space. I googled about this issue and set the height to wrapcontent, but it is not working. Please any one help me.
my code:
xml: item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/businfo"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:contentPaddingBottom="10dp"
app:contentPaddingTop="16dp"
android:elevation="10dp"
android:layout_margin="5dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<TextView
android:id="#+id/travel_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"
android:layout_marginLeft="5dp"
android:textColor="#color/holo_blue_light"
android:text="CGR Travels"/>
<TextView
android:id="#+id/bus_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="14sp"
android:textColor="#F04C3B"
android:layout_marginRight="30dp"
android:text="Rs.349"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="5dp"
android:background="#b6b6b6"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/arrtime_desttime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:textSize="15sp"
android:textColor="#1e356a"
android:text="9.00P.M - 7.00AM"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="12sp"
android:layout_marginRight="30dp"
android:text="38 seats"
android:id="#+id/seatcount" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12sp"
android:layout_marginLeft="5dp"
android:text="2+1 semi sleeeper"
android:id="#+id/bustype" />
<TextView
android:id="#+id/cancellationpolicy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="12sp"
android:textColor="#color/holo_blue_light"
android:layout_marginRight="30dp"
android:text="Cancellation policy"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/routeid"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/dropdate"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
java:
#Override
public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) {
if ((bus_routes.get(position).getAvailableSeats() == 0)) {
viewHolder.businfo.setVisibility(View.GONE);
} else {
viewHolder.businfo.setVisibility(View.VISIBLE);
}
}

Although removing the item from list and then updating list (notifydatasetchanged()) is a good practice but in some cases like when you have multiple viewHolders in a signle recyclerView it may be complicated, so in order to hide an specific item in your recyclerView you should do as below:
- Dont hide the root view of your layout, instead add a child to rootView of your layout which contains all other views and hide that. for example in your case you can set your cardView visibity to View.GONE and dont make any change to its parent linearLayout (businfo). i had similar case and this worked for me.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- don't change visibility of rootView view -->
<LinearLayout
android:id="#+id/child_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Place all child views inside here -->
<!-- change visibility of child_container view -->
</LinearLayout>
</LinearLayout>

You should remove this item from your bus_routes:
bus_routes.remove(position);
And then:
notifyItemRemoved(position);
notifyItemRangeChanged(position, bus_routes.size());

You can not directly hide view this way. you have to remove unwanted items from your ArrayList or don't add unwanted item before setting adapter. If your items are updated then again remove unwanted items and call notifydataset changed

Remove this element from ArrayList and use notifyDataSetChanged();
Example :
holder.imageDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
paths.remove(position);
notifyDataSetChanged();
}
});

You dont need to set visible or gone just remove item from list and than update list.
#Override
public void onBindViewHolder(final BusrouteAdapter.ViewHolder viewHolder, final int position) {
if ((bus_routes.get(position).getAvailableSeats() == 0)) {
bus_routes.remove(position);
}
}
try to call youradapter.notifydatasetchanged() after set adapter to recyclerview from activity.

Related

How to align text of textview in RecyclerView at the bottom

I am creating a list of text sizes UI that will have the different sizes of the same text rendering in the RecyclerView.
The requirement is, the text item that is rendering in the RecyclerView shall render from the bottom of the view, but its rendering from the top. Here is my code:
RecyclerView declaration:
<RelativeLayout
android:id="#+id/sizes_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:elevation="30dp"
android:paddingVertical="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="#+id/size_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/subtitles"
android:layout_gravity="center_horizontal"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<TextView
android:id="#+id/size_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="16dp"
android:gravity="center_vertical"
android:minWidth="50dp"
android:text="#string/size"
android:textColor="#color/white"
android:textStyle="bold"
tools:textColor="#color/black" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/text_track_size_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/size_textview"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</RelativeLayout>
</RelativeLayout>
View Item declaration:
<?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:id="#+id/focus_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp">
<TextView
android:id="#+id/row_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingHorizontal="10dp"
android:paddingVertical="7dp"
android:textAllCaps="false"
android:textColor="#drawable/text_size_color"
tools:text="Aa" />
</FrameLayout>
Bind View Holder method:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val textSizeOption: TextSizeOption = extSizeOptions[position]
holder.focusView.tag = textSizeOption
holder.rowItem.textSize = textSizeOption.fontSize
if(mSelectedFormat != null && mSelectedFormat == textSizeOption.id) {
holder.focusView.isSelected = true
} else {
holder.focusView.isSelected = (position == 0 && mSelectedFormat == null)
}
}
I have tried multiple things like wrapping the item view in the Relative Layout and giving alignParentBottom, etc but that didn't work as well.
Here is the screenshot of how it's looking right now:
Sizes text top aligning
Even the Size label is not centre aligning as it's given in the code. Not sure if I am missing something, but any help will be super appreciated
Modifying your view item like this will solve your problem
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/focus_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal">
<TextView
android:layout_gravity="center_vertical"
android:id="#+id/row_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#drawable/text_size_color"
android:text="Aa" />
</LinearLayout>

How to change recyclerview background(not item background) color dynamically based on child position?

I have a recyclerview like the image below
the background color is given dynamically in adapter using position, hence only if there is item(card) i can color the background i have linearlayout as cardview's parent and i am coloring the linear layout.
recyclerview:
<android.support.v7.widget.RecyclerView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="40dp"
style="#style/scrollbar_style"
/>
this is item resource:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/back"
android:paddingBottom="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/iconEntry"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_marginBottom="2dp"/>
<TextView
android:id="#+id/titleEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_marginBottom="2dp"/>
<TextView
android:id="#+id/titleSpanishEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:layout_marginBottom="2dp"/>
<TextView
android:id="#+id/countEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="4dp"
android:paddingEnd="4dp"
android:textSize="10sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
adapter:
public LinearLayout back;
public MyViewHolder(View view) {
super(view);
back = view.findViewById(R.id.back);
}
adapter onBindViewHolder:
holder.back.setBackgroundColor(Color.parseColor(
EntriesItemList.get(getCategory(position))
.getDisposalTypeCategory()
.getColor()));
now the requirement is color even if there is no item like the empty space in 1st section.
that can be done only if i can change the recyclerview background(not card background) dynamically based on adapter position, but looks impossible as there is no relation between the two. any suggestions on how to do this?
I have solved this by always making sure there are 3 items in a row by adding dummy invisible items, so that the background color extends all the way through the row.
One solution is that you pass the RecyclerView in the adapter constructor and in your adapter onBindViewHolder method change the RecyclerView background according to your adapter position.

Change Recycler view item background color by code

I want to change the background color of my RecyclerView item. I think I should be able to do it in onBindViewHolder method but I am not able to do this. I only get bottom border color of the item changed but I want to change the full background color
Here is what I want
public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {
if (//Some Condition) {
holder.itemView.setBackgroundColor(Color.GREY);
}
else {
holder.itemView.setBackgroundColor(Color.RED);
}
}
I think this should produce something like
What I get is this
Here is my RecyclerView Fragment layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/installed_recyclerView"
android:paddingTop="1dp"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
Here is my item layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="3dp"
android:paddingBottom="1dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/post_card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="1dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arial New"
android:id="#+id/installed_font_name"
android:textSize="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monotype Solutions"
android:textSize="12dp"
android:id="#+id/installed_preview_company_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preview Test"
android:id="#+id/installed_preview_textview"
android:textSize="30dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center"
android:id="#+id/installed_preview_Unnstall">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_remove_circle_24dp"
android:background="#android:color/transparent"
android:tint="#android:color/holo_red_light"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Uninstall"
android:textSize="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center"
android:id="#+id/installed_preview_flip_layout"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_autorenew_24dp"
android:background="#android:color/transparent"
android:tint="#android:color/holo_blue_dark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flip This"
android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Please try to change the code in onBindViewHolder as below
if(// Any Condition) {
((CardView)holder.post_card_view).setCardBackgroundColor(Color.GREY);
}
else
{
((CardView)holder.post_card_view).setCardBackgroundColor(Color.RED);
}
I hope this will help you , Feel free to comment.
I think that holder.itemView is not CardView but LinearLayout which holds CardView. Try something like this:
public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {
if (//Some Condition) {
holder.yourCardView.setCardBackgroundColor(Color.GREY);
}
else {
holder.yourCardView.setCardBackgroundColor(Color.RED);
}
}
#pankaj as #pawelo said casting can be costly sometimes so the best solution would be to give transparent color to CardView in xml like this
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/post_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardBackgroundColor="#android:color/transparent"
card_view:cardCornerRadius="1dp">
and then set background color to holder itemView like this.
public void onBindViewHolder(InstalledFontViewRecyclerAdapter.ViewHolder holder, int position) {
if (//Some Condition) {
holder.itemView.setBackgroundColor(Color.GREY);
}
else {
holder.itemView.setBackgroundColor(Color.RED);
}
}
now it should work as intended.
I tried the above answers in my project, it worked but Android studio gave me errors.
So I used holder.text_container.setBackgroundResource(R.color.category_numbers); instead and the red lines were gone. I used color from my custom resource.
I hope this helps someone.

Expansion panels in Google Material design android

I am just looking for an example for expansion panels in android (Material design).
https://www.google.com/design/spec/components/expansion-panels.html
I know we have expandable listview. But, I need to show some additional layout view on expanding each panel similar to Accordian view. How can we achieve this in android?
Try the expandable layout here . It can have the same behaviour as an Accordian
Include it to your gradle with compile 'com.github.aakira:expandable-layout:1.5.1#aar'
Example
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/accordian_header"
android:clickable="true">
<TextView
android:id="#+id/accordian_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="#333"
android:textStyle="bold"
android:text="Title" />
<ImageButton
android:id="#+id/down_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="8dp"
android:src="#android:drawable/arrow_down_float" />
</RelativeLayout>
<com.github.aakira.expandablelayout.ExpandableLinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingBottom="14dp"
android:orientation="vertical"
android:id="#+id/content"
app:ael_expanded="false"
app:ael_duration="500"
app:ael_orientation="vertical">
<!--add your content here -->
</com.github.aakira.expandablelayout.ExpandableLinearLayout>
</LinearLayout>
Then in your java code
ExpandableLinearLayout content=(ExpandableLinearLayout) itemView.findViewById(R.id.content);
RelativeLayout header=(RelativeLayout) itemView.findViewById(R.id.accordian_header);
//to toggle content
header.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
content.toggle();
}
});
Hope that was helpful.

Width of my custom InfoWindow layout is ignored in Google Maps Android API v2

The width I set in my custom InfoWindow layout seems to be ignored (it always match_parent).
Am I missing something?
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker arg0) {
return null;
}
#Override
public View getInfoContents(Marker arg0) {
View v = getActivity().getLayoutInflater().inflate(R.layout.info_window_layout, null);
// set my custom data
return v;
}
});
info_window_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="100dp"
android:layout_height="50dp"
android:padding="5dp" >
<!-- some custom stuff -->
</RelativeLayout>
I suppose it would actually be wrap_content.
The way info window works is by drawing your View to a bitmap and not putting it inside any other ViewGroup. layout_xxx are used by parent View.
Hardcoding these values on some or all children of your RelativeLayout will probably solve your problem.
try to use this in getInfoWindow, getInfoContents return null:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/transparent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<!-- some custom stuff -->
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#8BC34A"
android:orientation="vertical"
android:paddingBottom="#dimen/small_padding"
android:paddingLeft="#dimen/very_samll_padding"
android:paddingRight="#dimen/very_samll_padding"
android:paddingTop="#dimen/small_padding">
<LinearLayout
android:layout_width="320dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/tv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white" />
<LinearLayout
android:id="#+id/ll_latlong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_address"
android:layout_marginTop="#dimen/very_samll_padding"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/white" />
<View
android:layout_width="1dp"
android:layout_height="12dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/small_padding"
android:layout_marginRight="#dimen/small_padding"
android:background="#color/white" />
<TextView
android:id="#+id/tv_long"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
Just chnage the width dp according to your requiremnt

Categories

Resources