Onclick doesn't get triggered on CardView - android

I have an OnClickListener on a CardView. The listener only works when I tap on a region outside the content(TextViews/ImageViews). I also have a linear layout inside my CardView. I wanted it to work when I tap anywhere on the CardView. Is there a better way to implement this compared to how I'm doing it?
Here's my CardView in xml
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:padding="10dp"
android:layout_margin="3dp"
android:elevation="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/guilt_theme"
android:padding="4dp"
android:text="GUILT"
android:textColor="#color/textColor"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/post_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:layout_marginTop="10dp"
android:focusableInTouchMode="false"
android:clickable="true"
android:text="text content"
android:textColor="#color/secondaryText"
android:textSize="16sp" />
<TextView
android:id="#+id/date_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:inputType="number"
android:text="on 16/5/2018 by user"
android:textColor="#color/weaker_text"
android:textSize="14sp" />
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/view_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:layout_marginTop="4dp"
android:src="#drawable/ic_action_name" />
<TextView
android:id="#+id/total_views"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:inputType="number"
android:layout_marginTop="3dp"
android:textColor="#color/secondaryText"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:layout_marginLeft="20dp"
android:id="#+id/like_image"
android:layout_width="20dp"
android:layout_marginTop="4dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:src="#drawable/thumbs_up"
android:layout_height="20dp" />
<TextView
android:id="#+id/total_upvotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_marginTop="3dp"
android:inputType="number"
android:textColor="#color/secondaryText"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/dislike_image"
android:layout_marginLeft="20dp"
android:layout_width="20dp"
android:layout_marginTop="4dp"
android:src="#drawable/thumb_down"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground"
android:layout_height="20dp" />
<TextView
android:id="#+id/total_downvotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_marginTop="3dp"
android:inputType="number"
android:textColor="#color/secondaryText"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
I then added a listener to it in my Activity.
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), PostReader.class);
TextView posttext = view.findViewById(R.id.post_text);
String post = posttext.getText().toString();
intent.putExtra("postinfo", post);
startActivity(intent);
}
});

I think you are getting that behaviour because some of your TextViews and ImageViews have the attribute android:clickable="true"
Try:
Remove the attributes and add it only to the CardView. You may also have to remove the attribute
android:focusable="true" from the ImageViews so that the cardview can listen to click events.

In my case the issue was setting the inner view of the CardView to catch clicks by setting android:clickable="true" & android:focusable="true" to the inner view. This prevents the CardView from catching clicks.
Removing both allowed the CardView to catch clicks.

I've facing a problem with it today, a card view with the view I created was not performing the click function.
I tried many things and nothing worked. My solution was only to take the content of the card view and insert it into a constraint layout requiring all the view area. After that the click started working normally.
Example
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:backgroundTintMode="add"
android:elevation="5dp"
app:cardBackgroundColor="#android:color/transparent"
app:cardCornerRadius="5dp"
app:strokeColor="#color/yellow"
app:strokeWidth="2dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/textMove"
style="#style/TextAppearance.MaterialComponents.Subtitle2"
android:layout_width="120dp"
android:layout_height="50dp"
android:gravity="center"
android:padding="2dp"
android:text="#string/move"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

Related

Button not visible when keyboard is shown

I am trying to implement a functionality where the button below EditText moves up when the keyboard is shown, this is a login fragment and I would like to have this functionality similiar to what Instagram has. Right now, when keyboard is pressed, the EditText moves up but the button is hidden. I am attaching the xml below for the same.
<FrameLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:scaleType="center"
android:src="#drawable/ic_back" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp"
android:orientation="vertical" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/root_constraints"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="top|center"
android:layout_marginTop="20dp"
android:elevation="20dp"
>
<ImageView
android:id="#+id/std_imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="144dp"
android:layout_marginEnd="30dp"
android:fontFamily="#font/montserrat_bold"
android:textColor="#0B3148"
android:textSize="48sp"
android:textStyle="bold"/>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:fontFamily="#font/sf_bold"
android:textColor="#0B3148"
android:textSize="14sp"
android:textStyle="bold"
/>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:fontFamily="#font/sf_regular"
android:textColor="#0B3148"
android:textSize="14sp" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:fontFamily="#font/roboto_light"
android:hint=""
android:visibility="gone"
android:inputType="number"
android:paddingStart="16dp"
android:textSize="14sp"
android:paddingTop="11dp"
android:paddingEnd="0dp"
android:paddingBottom="10dp"
android:textColorHint="#color/color_text"
android:text=""
android:drawablePadding="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:backgroundTint="#E4E4E4"
android:fontFamily="#font/roboto_light"
android:inputType="number"
android:paddingStart="16dp"
android:textSize="14sp"
android:paddingTop="11dp"
android:paddingEnd="0dp"
android:paddingBottom="10dp"
android:textColorHint="#color/color_text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/et_countrycode"
android:visibility="gone"
android:textSize="12sp"
android:layout_marginTop="6dp"
app:layout_constraintStart_toStartOf="#+id/et_countrycode"
app:layout_constraintEnd_toEndOf="#+id/et_countrycode" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginHorizontal="30dp"
android:background="#drawable/bg_button_1"
android:textColor="#111B21"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout>
</FrameLayout>
Note: I have removed a bit of text and backgrounds but remember that all these views are essential
EDIT: I tried the following approach with adjustresize but the button is not overlapping with edittext
<FrameLayout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:scaleType="center"
android:src="#drawable/back" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:layout_marginBottom="20dp"
android:orientation="vertical" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/root_constraints"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="top|center"
android:layout_marginTop="20dp"
android:elevation="20dp">
<ImageView
android:id="#+id/std_imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:backgroundTint="#38A19C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="144dp"
android:layout_marginEnd="30dp"
android:fontFamily="#font/montserrat_bold"
android:textColor="#0B3148"
android:textSize="48sp"
android:textStyle="bold"
/>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:fontFamily="#font/sf_bold"
android:text="#string/welcome_to_isaac"
android:textColor="#0B3148"
android:textSize="14sp"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:fontFamily="#font/sf_regular"
android:textColor="#0B3148"
android:textSize="14sp" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/et_countrycode"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:backgroundTint="#E4E4E4"
android:drawablePadding="8dp"
android:fontFamily="#font/roboto_light"
android:hint=""
android:inputType="number"
android:paddingStart="16dp"
android:paddingTop="11dp"
android:paddingEnd="0dp"
android:paddingBottom="10dp"
android:text=""
android:textColorHint="#color/color_text"
android:textSize="14sp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/et_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:backgroundTint="#E4E4E4"
android:fontFamily="#font/roboto_light"
android:hint="#string/phone_number"
android:inputType="number"
android:paddingStart="16dp"
android:paddingTop="11dp"
android:paddingEnd="0dp"
android:paddingBottom="10dp"
android:textColorHint="#color/color_text"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:textSize="12sp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="#+id/et_countrycode"
app:layout_constraintStart_toStartOf="#+id/et_countrycode"
app:layout_constraintTop_toBottomOf="#+id/et_countrycode" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/bt_finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="12dp"
android:background="#drawable/bg_button_1"
android:text="#string/continue_txt"
android:textColor="#111B21"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="#id/scrollView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Use ConstraintLayout to position the button to the bottom of the screen and then add.
<activity
android:name=".activity"
android:windowSoftInputMode="adjustResize"
android:exported="false" />
...in the activity that would host that Fragment's layout.
Note
The button should be layout in this manner:
<androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
</androidx.constraintlayout.widget.ConstraintLayout>
... but right now in your code, I can see the button inside a LinearLayout. Change the layout to make the button be positioned at the bottom of a ConstraintLayout and make that ConstraintLayout the parent of that button. The ConstraintLayout should also fill the whole screen.
Edit:
When you use windowSoftInputMode="adjustResize the system positions the element at the bottom of the screen on top of the keyboard and makes your layout shrink in size. So you need to attach a ScrollView to make your layout scrollable.
Like this:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
...>
// Position this scrollView to fill the entire screen
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
...>
// This scroll view layout would contain all your other layout views
</ScrollView>
<Button
android:id="#+id/finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
</androidx.constraintlayout.widget.ConstraintLayout>
Working principle
The button should not be a child view of the ScrollView, it should be a child view of the root view, which is a ConstraintLayout that fills the screen, then the button is positioned to the bottom of the root view. The bottom of the ScrollView should be positioned to the top of the Button. That way, the child views of the ScrollView is scrollable and the button stays on top of the keyboard.
Go to your AndroidMenifest File, Inside you <activity> tag put android:windowSoftInputMode = "adjustpan" it would handle the view after showing up keyboard. if adjustpan doesn't work, there are more property, use one respect to your need.
example:
<activity
android:name=".YourActivityName"
android:windowSoftInputMode="adjustPan"
android:exported="false" />

How to create Placeholder Shimmer Layout for Recycler View using same row which is used in Adapter?

I implemented some examples for Shimmer effect for learning. I don't understand that why people are using separate placeholder layout for while using shimmer effect?
Can't we do change color programmatically and do it for the row which we're used in Adapter class?
See the code for my adapter class row:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground"
app:cardCornerRadius="3dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_toStartOf="#+id/delete"
android:orientation="vertical">
<TextView
android:id="#+id/eventTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:visibility="gone" />
<TextView
android:id="#+id/eventDes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:visibility="gone" />
<TextView
android:id="#+id/eventAttendee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:visibility="gone" />
<TextView
android:id="#+id/eventStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:visibility="gone" />
<TextView
android:id="#+id/eventEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:visibility="gone" />
<TextView
android:id="#+id/eventLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:visibility="gone" />
</LinearLayout>
<ImageView
android:id="#+id/delete"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:clickable="true"
android:focusable="true"
android:tint="#color/colorBlack"
app:srcCompat="#drawable/ic_baseline_delete_24" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
Can I do that with this layout only? or should I create separate? If separate, can you please tell me how to do that for this above row? Everyone using "view" layout only. I don't understand this.
The reason for declaring a new layout is because you have to wrap your views inside Shimmerlayout and its parent layout will be either frame layout or linear layout. So basically you are restricted in terms of which container to use. Moreover, you are only going to show shimmer animation for a few seconds so using it as a primary ViewGroup is not recommended.
So to create this row just wrap it inside
<?xml version="1.0" encoding="utf-8"?>
<com.facebook.shimmer.ShimmerFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/shimmer_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground"
app:cardCornerRadius="3dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_toStartOf="#+id/delete"
android:orientation="vertical">
<TextView
android:id="#+id/eventTitle"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_marginStart="12dp"
android:textSize="18sp"
android:text="your title"
android:background="#cecece"
android:textColor="#cecece"
/>
<TextView
android:id="#+id/eventDes"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:text="your sample description goes here"
android:background="#cecece"
android:textColor="#cecece"
/>
<TextView
android:id="#+id/eventAttendee"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:text="your eventAttendee"
android:background="#cecece"
android:textColor="#cecece"
/>
<TextView
android:id="#+id/eventStart"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:text="your start time"
android:background="#cecece"
android:textColor="#cecece"
/>
<TextView
android:id="#+id/eventEnd"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:text="your end time"
android:background="#cecece"
android:textColor="#cecece"
/>
<TextView
android:id="#+id/eventLocation"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:text="your location"
android:background="#cecece"
android:textColor="#cecece"
/>
</LinearLayout>
<ImageView
android:id="#+id/delete"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:clickable="true"
android:focusable="true"
android:tint="#cecece"
app:srcCompat="#drawable/ic_delete" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</com.facebook.shimmer.ShimmerFrameLayout>
Now when you have to start it just use -
`
ShimmerFrameLayout container =
(ShimmerFrameLayout) findViewById(R.id.shimmer_view_container);
container.startShimmer();

Recyclerview rows gets shrinked after notifydatasetchanged or after notifyItemChanged(position)

I have a recyclerview that takes a Arraylist as paramter. And when i am adding an image uri to one the objects variables of the list and then refreshing the recylerview
using
rv.notifydatasetchanged
then my code properly adds image to the imageview of the object whose uri variable i set but all the rows gets shrinked
and when i am doing
rv.notifyItemChanged(position)
then that row gets shrinked. And other remains same. I am not modifying width or height after refresh or notifydatasetchanged, thats why i am confused why its happening.
Here is my recylverview row:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="feedbackData"
type="in.myapp.event_subscriber.models.MyData" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_question_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="#{Integer.toString(feedbackData.questionNumber)}"
android:textColor="#color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#id/tv_question" />
<TextView
android:id="#+id/tv_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="40dp"
android:text="#{feedbackData.question}"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/tv_question_number"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<EditText
android:id="#+id/et_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="#drawable/stroke_rectangle_grey"
android:gravity="start"
android:hint="Write you answer here..."
android:lines="4"
android:paddingStart="10dp"
android:paddingTop="15dp"
android:text="#{feedbackData.answer.description}"
app:layout_constraintTop_toBottomOf="#id/ll_question" />
<LinearLayout
android:id="#+id/ll_action_attachment"
goneUnless="#{feedbackData.imageUri==null}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/et_answer">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/ic_attachment" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="Add attachment"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:id="#+id/iv_attached_image"
attachImage="#{feedbackData.imageUri}"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:elevation="10dp"
android:scaleType="fitXY"
android:visibility="gone" />
<TextView
android:id="#+id/tv_image_attached_text"
goneUnless="#{feedbackData.imageUri!=null}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:text="Image Attached Successfully"
android:textColor="#color/medium_aqua_marine"
android:visibility="gone" />
<View
android:layout_width="match_parent"
android:layout_height="40dp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="#color/grey" />
</LinearLayout>
</layout>
The first image shows my defualt recyclerview, but as soon as i add an image uri from my local storage to the list variable and then call notifydatasetchanged or notifyItemChanged(position), the thing in second image happens. Everything shrinks, even the textviews and divider and everything shrinks.
Code for my recylerview:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<TextView
android:id="#+id/tv_event_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:background="#color/white"
android:text="Technical corridor"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:background="#color/grey"
app:layout_constraintTop_toBottomOf="#id/tv_event_name" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_subscriber_feedback"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toTopOf="#id/btn_submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider"
tools:listitem="#layout/row_feedback" />
<Button
android:id="#+id/btn_submit"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginBottom="40dp"
android:background="#color/colorPrimary"
android:text="Submit"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I will try to give some hints, might be because recyclerView recalculates views:
1) for your recycler view setHasFixedSize(true)
2) set a LinearLayoutManager to your recyclerView
3) keep using notifyDataSetChanged()
4) let your recyclerview be match_parent both width and height
See what happens.
Adding the imageview on which i was setting image and the textview which was only visible after image is set inside one more linearlayout worked. Now its not shrinking. Maybe now only that linearlayout is shrinking upto the width of imageview. I Dont know why this is happening. But atleast this worked and can be used for now.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
goneUnless="#{feedbackData.imageUri!=null}"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_attached_image"
attachImage="#{feedbackData.imageUri}"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:elevation="10dp"
android:scaleType="fitXY"
android:visibility="gone" />
<TextView
android:id="#+id/tv_image_attached_text"
goneUnless="#{feedbackData.imageUri!=null}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:text="Image Attached Successfully"
android:textColor="#color/medium_aqua_marine"
android:visibility="gone" />
</LinearLayout>

Unable to select TextView inside a Fragment

I am trying to select text from a TextView which is present inside a fragment.
This is the XML for my TextView.
The textIsSelectable, focusable, enabled and longclickable properties are all set to true according to this post.
<TextView
android:id="#+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="#string/stall_user"
android:textColor="#android:color/black"
android:textColorHighlight="#android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
I have also programatically set the following in my Fragment activity:
text.setTextIsSelectable(true);
If it helps, I am fetching data from a Bundle passed through the Activity that holds the fragment, then I am setting the TextView using:
text.setText(Html.fromHtml(data.getString("text")));
text.setTextIsSelectable(true);
I am still unable to select the text. I read in some Stack Overflow post that setting width/height to "wrap_content" allows you to select the text (some old Android bug I guess). This trick has worked for my recyclerview TextView in another activity. Doesn't seem to work here.
Thanks for your help!
Edit:
Here is the entire Fragment layout code as requested:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="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="com.myapp.www.ViewFragments.OriginalPostFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/hide_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text=""
android:layout_alignParentTop="true"
android:background="#eeeeee"
android:gravity="end"
android:padding="4dp"
android:layout_marginEnd="16dp"
android:textColor="#android:color/black"
android:textSize="16sp"
android:elevation="6dp"
/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/detail_wrapper"
android:background="#ffffff"
android:descendantFocusability="blocksDescendants"
android:paddingBottom="48dp"
cardview:cardCornerRadius="4dp"
cardview:cardElevation="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/user_image_recycle"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" />
<TextView
android:id="#+id/post_type_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_toRightOf="#+id/user_image_recycle"
android:textStyle="italic" />
<TextView
android:id="#+id/post_title_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/post_type_recycle"
android:layout_marginStart="16dp"
android:layout_toRightOf="#+id/user_image_recycle"
android:maxLines="1"
android:textColor="#android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/user_name_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/post_title_recycle"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/user_image_recycle"
android:textColor="#0094BD"
android:textSize="16sp" />
<ImageView
android:id="#+id/post_divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#+id/user_image_recycle"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="#android:color/black" />
<TextView
android:id="#+id/post_text_recycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/post_divider"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:autoLink="web"
android:fontFamily="serif"
android:text="#string/stall_user"
android:textColor="#android:color/black"
android:textColorHighlight="#android:color/holo_blue_light"
android:textIsSelectable="true"
android:focusable="true"
android:enabled="true"
android:longClickable="true"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
</FrameLayout>
The reason is descendantFocusability. In Your CardView layout, you set descendantFocusability to blocksDescendants.
What it does it disable the focusability of the descendants, so Your TextView never get those focus events.
For more info see here.
To make it work just remove this line from CardView
android:descendantFocusability="blocksDescendants"

constraint layout performance as recyclerview item

ConstraintLayout version: 1.1.0 stable.
I am using ConstraintLayout as recyclerview item layout, xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_item_bg_selector"
android:minHeight="60dp">
<android.support.constraint.Guideline
android:id="#+id/left_guide_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="16dp"/>
<android.support.constraint.Guideline
android:id="#+id/top_guide_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="10dp"/>
<android.support.constraint.Guideline
android:id="#+id/right_guide_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="16dp"/>
<android.support.constraint.Guideline
android:id="#+id/bottom_guide_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_end="7dp"/>
<ImageView
android:id="#+id/avatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:scaleType="centerCrop"
android:src="#drawable/common_head_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="#+id/left_guide_line"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="#+id/tv_unread_red_point"
style="#style/tips_red_dot_circle_common"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/avatar"
app:layout_constraintLeft_toRightOf="#+id/avatar"
app:layout_constraintRight_toRightOf="#+id/avatar"
app:layout_constraintTop_toTopOf="#+id/avatar"
tools:visibility="visible"/>
<TextView
android:id="#+id/tv_unread_msg_count"
style="#style/tips_red_dot_num_common"
android:layout_marginLeft="32dp"
android:gravity="center"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/avatar"
app:layout_constraintLeft_toLeftOf="#+id/left_guide_line"
app:layout_constraintRight_toRightOf="#+id/avatar"
app:layout_constraintTop_toTopOf="#+id/avatar"
tools:text="99+"
tools:visibility="visible"/>
<cn.test.widget.NickWidget
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/msg"
app:layout_constraintLeft_toRightOf="#+id/avatar"
app:layout_constraintRight_toLeftOf="#+id/time"
app:layout_constraintTop_toTopOf="#+id/top_guide_line"
app:layout_constraintVertical_chainStyle="packed"/>
<TextView
android:id="#+id/time"
style="#style/text_h3_common_36px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxLines="1"
android:text="#string/def_value"
app:layout_constraintBottom_toBottomOf="#+id/name"
app:layout_constraintLeft_toRightOf="#+id/name"
app:layout_constraintRight_toRightOf="#+id/right_guide_line"
app:layout_constraintTop_toTopOf="#+id/name"
tools:text="11:22"/>
<ImageView
android:id="#+id/msg_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="1dp"
android:scaleType="centerCrop"
android:src="#drawable/recent_msg_state"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#+id/msg"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toRightOf="#+id/avatar"
app:layout_constraintRight_toLeftOf="#+id/icon_chatroom"
app:layout_constraintTop_toTopOf="#+id/msg"
tools:visibility="visible"/>
<ImageView
android:id="#+id/icon_chatroom"
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:scaleType="centerCrop"
android:src="#drawable/common_head_icon"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#+id/msg"
app:layout_constraintLeft_toRightOf="#+id/msg_state"
app:layout_constraintRight_toLeftOf="#+id/msg"
app:layout_constraintTop_toTopOf="#+id/msg"
app:layout_goneMarginLeft="10dp"
tools:visibility="visible"/>
<TextView
android:id="#+id/msg"
style="#style/text_h2_common_42px"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="1dp"
android:ellipsize="end"
android:maxLines="1"
android:paddingBottom="1dp"
android:paddingTop="1dp"
app:layout_constraintBottom_toTopOf="#+id/bottom_guide_line"
app:layout_constraintLeft_toRightOf="#+id/icon_chatroom"
app:layout_constraintRight_toLeftOf="#+id/chat_room_exit_icon"
app:layout_constraintTop_toBottomOf="#+id/name"
app:layout_goneMarginLeft="10dp"/>
<ImageView
android:id="#+id/chat_room_exit_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im_icon_chat_room_exit"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#+id/msg"
app:layout_constraintLeft_toRightOf="#+id/msg"
app:layout_constraintRight_toLeftOf="#+id/group_msg_not_notify_icon"
app:layout_constraintTop_toTopOf="#+id/msg"
tools:visibility="visible"/>
<ImageView
android:id="#+id/group_msg_not_notify_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pub_nngroup_icon_shield"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#+id/msg"
app:layout_constraintLeft_toRightOf="#+id/chat_room_exit_icon"
app:layout_constraintRight_toLeftOf="#+id/right_guide_line"
app:layout_constraintTop_toTopOf="#+id/msg"
tools:visibility="visible"/>
</android.support.constraint.ConstraintLayout>
We got really bad performance when scrolling through the RecyclerView, and everything work just fine if I change item layout from a ConstraintLayout to a traditional layout (Nesting 4 or 5 layers). What can we do about this?
I have used ConstraintLayout as item layout of recyclerview, it scrolls smoothly. I think there is no problem of ConstraintLayout, but make sure that you should not call myAdapter.notifyDataSetChanged() in adapter class. It will make your app slow UI rendering.
Refer this Android document for Slow rendering

Categories

Resources