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>
Related
I have one parent Horizontal Recycleview which contains twelve Vertical Recycleviews. I would like to show only two columns per page, on only one horizontal row. I use GridLayoutManager.
GridLayoutManager layoutManager = new GridLayoutManager(this,1);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
Increasing span to 2 it creates two rows which i don't want.
Also i tried layout:width in xml to fixed value in dp or sp that results in different number of columns on different screen sizes.
xml for each block
<?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:id="#+id/view_table_view_game_block_constraint_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#drawable/shape_round_top"
android:layout_marginTop="8dp">
<TextView
android:id="#+id/textView_table_view_odds_block_time"
android:layout_width="0dp"
android:layout_height="50sp"
android:gravity="center_vertical"
android:textColor="#color/colorFontWhite"
android:textSize="#dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_table_view_odds_block_hometeam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:textAlignment="textStart"
android:background="#android:color/transparent"
android:textColor="#color/colorFontWhite"
android:textSize="#dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView_table_view_odds_block_time" />
<Button
android:id="#+id/button_table_view_odds_block_drawable"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="#android:color/transparent"
android:textAlignment="textStart"
android:textColor="#color/colorFontWhite"
android:textSize="#dimen/font_size"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_table_view_odds_block_hometeam" />
<Button
android:id="#+id/button_table_view_game_odds_awayteam"
android:layout_width="0dp"
android:layout_height="50sp"
android:layout_marginTop="4dp"
android:background="#android:color/transparent"
android:textAlignment="textStart"
android:textColor="#color/colorFontWhite"
android:textSize="#dimen/font_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_table_view_odds_block_drawable" />
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Set width dynamically according to the screen size. this is the best way to get perfect result.
Update onBindViewHolder of your Horizontal Recyclerview like that.
#Override
public void onBindViewHolder(#NonNull UsersAdapter.UsersAdapterVh holder, int position) {
UserModel userModel = userModelList.get(position);
String username = userModel.getUserName();
String prefix = userModel.getUserName().substring(0,1);
holder.tvUsername.setText(username);
holder.tvPrefix.setText(prefix);
holder.item_layout.getLayoutParams().width = getScreenWidth(context)/2;
}
public static int getScreenWidth(Context context) {
WindowManager wm= (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
This is how the RecyclerView item layout looks like for the given example.
<?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="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:padding="10dp"
android:id="#+id/holder_view"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="50dp"
android:background="#drawable/users_bg"
android:layout_height="50dp">
<TextView
android:id="#+id/prefix"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#color/headerColor"
android:text="T"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
</RelativeLayout>
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#color/headerColor"
android:text="username"
android:layout_marginStart="90dp"
android:layout_centerVertical="true"
android:layout_height="wrap_content"/>
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imageView"
android:layout_margin="10dp"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_navigate_next_black_24dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
Header and body using ConstraintLayout
Hi !
I would make a simple layout
Header should be of a different color from Body, and I want a shadow between header and body (body should be elevate).
Make it with linear layout is soo simple, but I can't do it with constraintLayout.. Can you help me ? I'm studying constraint and it is not soo easy for me ..
Some other details:
the header will stick
the recycler view will be covering all
the space other than header view but with a left and right margin
i don't undestand how can I divide the section, with linear layout I
would have created an external linearLayout with vertical
orientation. Inside that I would have created a container at top
with a given background and an elevation, with matchparent height
(some dp), under that I would have created another linearLayout
match parent , match parent. Inside this last linear layout I would
have inserted a recycler view with a margin left and right
Please try below code:
<androidx.constraintlayout.widget.ConstraintLayout
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">
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_gray"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/tv_header"/>
<TextView
android:id="#+id/tv_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="header"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view1"
app:layout_constraintBottom_toTopOf="#id/view2"/>
<View
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_gray"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_header"
app:layout_constraintBottom_toTopOf="#id/tv_body"/>
<TextView
android:id="#+id/tv_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="Body"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view2"
app:layout_constraintBottom_toTopOf="#id/view3"/>
<View
android:id="#+id/view3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/color_gray"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_body"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Output of above code is:
I hope it works for you.
If I understand your question correctly, then as per that you will make something like this:
TextView : id: tvTopHeader - Header and body using ConstraintLayout Hi ! I would make a simple layout
View : id: divider1
TextView : id: tvHeader - Header
View: id: divider2
TextView : id: tvBody - Body
View: id: divider3
Now having the layout-width = "match_parent" and layout-height ="wrap_content" or some fixed value as per requirement...the constraints will be something like the following:
tvTopHeader: constraintTopToTopOf = "parent"
divider1: constraintTopToBottomOf ="#id/tvTopHeader"
tvHeader : constraintTopToBottomOf = "#id/divider1"
divider2: constraintTopToBottomOf ="#id/tvHeader"
tvBody : constraintTopToBottomOf = "#id/divider2"
divider3: constraintTopToBottomOf ="#id/tvBody"
Edit
<?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="match_parent">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"
app:layout_constraintBottom_toBottomOf="#id/view"
app:layout_constraintEnd_toStartOf="#id/btn2"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:text="Button2"
app:layout_constraintBottom_toBottomOf="#id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:elevation="2dp"
android:padding="10dp"
android:text="title"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="#id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/view" />
</androidx.constraintlayout.widget.ConstraintLayout>
With Linearlayout I would have get the goal in this way:
<?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="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white"
android:elevation="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="press"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
>
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
Maybe using the constraint layout in this case is not the best ? But why ? I supposed that the constraint layout would replace all the other layout types.
I've noticed some RV performance issues.
I have a RecyclerView with some amount of items. When I launch my Fragment with RV and trying to scroll it for the first time I see some lags. I'll attach a GIF with active GPU profiling at the end of this question.
As you can see, I have a huge FPS drop when I start scrolling then it becomes smooth. So my question is -
Is it possible to optimise this process? I tried the following:
1. To create View for all my ViewHolders once:
private var itemView: View? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
if (itemView == null) {
itemView = LayoutInflater.from(parent.context).inflate(R.layout.rv_contact_item, parent, false)
}
return ContactViewHolder(itemView)
}
But it's not possible, cuz this view will be attached to it's parent with first onCreateViewHolder call and I'm getting crash "Detach your view from parent first".
2. Create ViewHolders in background thread.
Doesn't do anything too.
I know there's a possibility to create View programmatically, but will it work as intended? I have a pretty difficult View, so it would take some time to try it.
UPD:
My XML for single item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:id="#+id/btn_contact"
android:orientation="horizontal"
android:padding="16dp"
android:background="?attr/selectableItemBackground">
<com.myapp.custom.AvatarView
android:id="#+id/v_avatar"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginEnd="16dp"
android:layout_gravity="center"
android:transitionName="chat_avatar_transition"
tools:background="#color/pale_red">
<com.myapp.custom.UserStatusView
android:id="#+id/v_user_status"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_gravity="bottom|end"/>
</com.myapp.custom.AvatarView>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginEnd="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_conference"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAlignBottom="true"
android:layout_marginEnd="2dp"
app:srcCompat="#drawable/ic_vector_conference"/>
<TextView
android:id="#+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:ellipsize="end"
android:lines="1"
android:transitionName="chat_name_transition"
tools:text="Title" />
</LinearLayout>
<TextView
android:id="#+id/tv_contact_subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/text_light"
android:textSize="14sp"
android:ellipsize="end"
android:lines="1"
tools:text="Subtitle" />
<LinearLayout
android:id="#+id/ll_last_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_message_direction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="3dp"
app:srcCompat="#drawable/ic_vector_arrow_right" />
<TextView
android:id="#+id/tv_last_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#color/text_light"
android:textSize="14sp"
android:ellipsize="end"
android:lines="1"
tools:text="Hello!" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_birthday"
android:layout_width="24dp"
android:layout_height="24dp"
app:srcCompat="#drawable/ic_vector_birthday" />
<ImageView
android:id="#+id/iv_app_type"
android:layout_width="24dp"
android:layout_height="24dp"
app:srcCompat="#drawable/ic_vector_mobile" />
<ImageView
android:id="#+id/iv_important_messages"
android:layout_width="24dp"
android:layout_height="24dp"
app:srcCompat="#drawable/ic_vector_flag" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_message_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_gravity="bottom|end"
app:srcCompat="#drawable/ic_vector_checked" />
<TextView
android:id="#+id/tv_unread_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:padding="3dp"
android:background="#drawable/shape_rect_rounded_12dp_blue_regular"
android:gravity="center"
android:textColor="#android:color/white"
android:layout_gravity="bottom|end"
tools:text="10" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
UPD2:
My bind() method:
fun bind(contact: ContactModel) {
avatarView.apply {
initialize(
contact.id,
contact.avatar,
contact.fullName)
}
ivConference.visibility = if (ContactType.CONFERENCE == contact.type)
View.VISIBLE
else
View.GONE
tvName.text = contact.fullName
tvContactSubtitle.text = contact.subtitle
contact.lastMessageDirection?.let { direction ->
llLastMessage.visible
ivLastMessageDirection.setImageDrawable(
VectorDrawableCompat.create(
itemView.context.resources,
direction.icon,
null)
)
tvLastMessage.text = contact.lastMessage
} ?: llLastMessage.invisible
ivBirthday.visibility = if (contact.isBirthdayInc) {
View.VISIBLE
} else
View.GONE
ivImportantMessages.visibility = if (contact.hasImportantMessages)
View.VISIBLE
else
View.GONE
if (contact.unreadCounter == 0) {
tvUnreadCounter.gone
} else {
tvUnreadCounter.visible
tvUnreadCounter.text = contact.unreadCounter.toString()
}
}
That is because the quality of the image you've used is too high for so many item which takes time to optimize.
Please convert your images to WebP format for better performance.
Hope it helps.
I need to show a list of buttons either vertically or horizontally and make that decision based on how the text fits in the view.
I need to display the buttons like this:
but if the width of the text is longer and it needs to move it to a new line, I need to change the orientation and set them vertically, like so:
The number of items can differ, from 2 to at most 10. How can I know if the text will fit in the horizontally aligned views so that If they don't I can make them align vertically?
If Parent Layout has more than 4(or)5 view the String value will definitely break into new line,so if u want suggestions Use this if it helps
Make Parent Layout as Linear Layout with default orientation as horizontal in XML
In JAVA code check String raspuns = "dynamic string"; length
Try this
LinearLayout parent_layout = findViewById(R.id.parent);
if(raspuns.length > 7){
parent_layout.setOrientation(LinearLayout.VERTICAL);
}
Hope it Helps
What you need is to use android.support.v7.widget.ButtonBarLayout which is
An extension of LinearLayout that automatically switches to vertical
orientation when it can't fit its child views horizontally.
Which is being used by Android itself in abc_alert_dialog_button_bar_material.xml
So the code would look like
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.ButtonBarLayout 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="wrap_content"
android:gravity="bottom"
android:layoutDirection="locale"
android:orientation="horizontal"
android:paddingBottom="4dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="4dp">
<Button
android:id="#android:id/button3"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Neutral button" />
<android.widget.Space
android:id="#+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible" />
<Button
android:id="#android:id/button2"
style="?attr/buttonBarNegativeButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Negative button" />
<Button
android:id="#android:id/button1"
style="?attr/buttonBarPositiveButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Positive button" />
</android.support.v7.widget.ButtonBarLayout>
Output of this xml is
And if text is small for buttons, output for same layout is
just try like this and it's working fine for me
Layout
<HorizontalScrollView 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="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#color/colorAccent"
android:padding="16dp"
android:text="Hello World!"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#color/colorAccent"
android:padding="16dp"
android:text="Hello World!"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#color/colorAccent"
android:padding="16dp"
android:text="Hello World!"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#color/colorAccent"
android:padding="16dp"
android:text="Hello World!"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#color/colorAccent"
android:padding="16dp"
android:text="Hello World!"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</HorizontalScrollView>
code for Rearrange views
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels
var viewsWidth = 0;
for (i in 0..linear.childCount - 1) {
linear.getChildAt(i).post({
viewsWidth += linear.getChildAt(i).width
if (i == linear.childCount - 1) {
if (width < viewsWidth) {
linear.orientation = LinearLayout.VERTICAL
for (k in 0..linear.childCount - 1) {
(linear.getChildAt(k) as Button).width = (width - resources.getDimension(R.dimen.spacing_large)).toInt()
linear.getChildAt(k).requestLayout()
}
}
}
});
}
And spacing_large is sum of margin_start and margin_end
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.