How to delete space when I hide a CardView - android

How can I delete the space when I hide a CardView? I doing an app with android and Firebase and I show the information from firebase in cardviews, but when the info isn't correct the cardview must disappear. The point is that the cardview disappear, but still uses a space in the layout, I've tried the answers in
setVisibility(GONE) view becomes invisible but still occupies space But it doesn't work for me.
#Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
CursosDB.class,
R.layout.design_row_cursos,
MainActivity.CursosPViewHolder.class,
myRef)
{
#Override
protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position)
{
String estado = Cmodel.getSTATUS().toString();
if (estado.equals("OK")){
viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
viewHolder.setTitle(Cmodel.getTITLE());
viewHolder.setCiudad(Cmodel.getPLACE());
viewHolder.setLevel(Cmodel.getLEVEL());
viewHolder.setDur(Cmodel.getDURACION());
viewHolder.setPrice(Cmodel.getCOSTO());
}else{
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
viewHolder.mView.setLayoutParams(layoutParams);
viewHolder.mView.setVisibility(View.INVISIBLE);
viewHolder.mView.setVisibility(View.GONE);
//viewHolder.mView.setVisibility(View.GONE);
//Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
//Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
}
});
}
};
mCourses.setAdapter(firebaseRecyclerAdapter);
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">
<LinearLayout android:id="#+id/layout_to_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Cimg"
android:layout_width="match_parent"
android:layout_height="165dp"
android:scaleType="center"
app:srcCompat="#drawable/knowit_logo" />
<TextView
android:id="#+id/Ctitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Cimg"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:paddingTop="8dp"
android:text="Titulo"
android:textStyle="bold" />
<TextView
android:id="#+id/Clugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Ctitle"
android:layout_alignStart="#+id/Ctitle"
android:layout_below="#+id/Ctitle"
android:layout_marginTop="10dp"
android:text="Lugar" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Clugar"
android:layout_alignStart="#+id/Clugar"
android:layout_below="#+id/Clugar"
android:layout_marginTop="10dp"
android:text="$" />
<TextView
android:id="#+id/Cprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView29"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#+id/textView29"
android:layout_toRightOf="#+id/textView29"
android:paddingLeft="5dp"
android:text="0.0" />
<TextView
android:id="#+id/Cdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Clugar"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="Duración" />
<TextView
android:id="#+id/Cnivel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Cprice"
android:layout_alignLeft="#+id/Cdur"
android:layout_alignStart="#+id/Cdur"
android:layout_marginBottom="0dp"
android:text="Nivel" />
</RelativeLayout>
</LinearLayout> </android.support.v7.widget.CardView>
This is the mView:
public static class CursosPViewHolder extends RecyclerView.ViewHolder {
View mView;
public CursosPViewHolder(final View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
post_title.setText(title);
}
public void setCiudad (String ciudad){
TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
post_city.setText(ciudad);
}
public void setPhotoURL(Context ctx, String pgotouserurl) {
ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
Picasso.with(ctx).load(pgotouserurl).into(post_image);
} }
I hope you can help me.

First the XML code you've posted ils wrong closed,(must finish with a CardView tag)
On addition be sure you're pointing the CardView layout with mView field ans Only use GONE or INVISIBLE,
INVISIBLE change your Panel ( Root view ) structure but GONE don't

Related

Cannot click on item's view inside RecyclerView

I want to show a popup menu for each item of a RecyclerView when a button is clicked.
My Adapter:
public class SearchResultRecyclerViewAdapter extends RecyclerView.Adapter<SearchResultRecyclerViewAdapter.ViewHolder> {
public SearchResultRecyclerViewAdapter(ArrayList<BookModel> bookModels, Context context) {
this.bookModels = bookModels;
this.context = context;
}
ArrayList<BookModel> bookModels;
Context context;
#NonNull
#NotNull
#Override
public ViewHolder onCreateViewHolder(#NonNull #NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("aa", bookModels.get(position).getThumbnailLink());
Glide.with(context)
.asBitmap()
.load(bookModels.get(position).getThumbnailLink())
.into(holder.coverImage);
holder.name.setText(bookModels.get(position).getName());
holder.author.setText(bookModels.get(position).getAuthor());
holder.category.setText(bookModels.get(position).getCategories().toString());
holder.maturity.setText(bookModels.get(position).getMaturityRating());
holder.desc.setText(bookModels.get(position).getDesc());
holder.option.setOnClickListener(new View.OnClickListener() { ///////////// here
#Override
public void onClick(View v) {
//show popup
Log.d("ItemOption","Clicked");
}
});
if (position+1 == bookModels.size()){
holder.divider.setVisibility(View.GONE);
}
}
#Override
public int getItemCount() {
return bookModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
RoundedImageView coverImage;
TextView name;
TextView author;
TextView desc;
TextView category;
TextView maturity;
ImageView divider;
Button option;
RelativeLayout root;
public ViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
coverImage = itemView.findViewById(R.id.cover_book_item_imageview);
name = itemView.findViewById(R.id.name_book_item_textview);
author = itemView.findViewById(R.id.author_book_item_textview);
desc = itemView.findViewById(R.id.desc_book_item_textview);
category = itemView.findViewById(R.id.category_book_item_textview);
maturity = itemView.findViewById(R.id.maturity_book_item_textview);
divider = itemView.findViewById(R.id.divider_book_item_imageview);
option = itemView.findViewById(R.id.option_book_item_button);
root = itemView.findViewById(R.id.root);
}
}
}
My item Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cover_book_item_cardview"
android:layout_width="#dimen/_81sdp"
android:layout_height="#dimen/_124sdp"
android:layout_margin="#dimen/_15sdp"
app:cardCornerRadius="#dimen/_8sdp"
android:elevation="#dimen/_5sdp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/cover_book_item_imageview"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:src="#drawable/testcover"
android:scaleType="fitXY"
app:riv_corner_radius="#dimen/_5sdp"/>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/name_book_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:layout_marginTop="#dimen/_20sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:textSize="#dimen/_18sdp"
android:textColor="#color/light_black"
/>
<TextView
android:id="#+id/author_book_item_textview"
android:textSize="#dimen/_13sdp"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:layout_marginTop="#dimen/_1sdp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name_book_item_textview"
android:text="author"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp"/>
<TextView
android:id="#+id/desc_book_item_textview"
android:layout_width="#dimen/_190sdp"
android:layout_height="#dimen/_40sdp"
android:layout_below="#id/name_book_item_textview"
android:width="#dimen/_60sdp"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp"
android:layout_marginTop="#dimen/_30sdp"
android:textColor="#color/light_black"
android:text="desc"
android:fontFamily="#font/yanonekaffeesatz_medium"/>
<LinearLayout
android:id="#+id/holder_book_item_linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/name_book_item_textview"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_90sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_90sdp">
<TextView
android:id="#+id/category_book_item_textview"
android:textColor="#color/white"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:background="#drawable/rounded_rectangle_90"
android:backgroundTint="#color/yellow"
android:padding="#dimen/_3sdp"
android:layout_marginTop="#dimen/_72sdp"
android:text="category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/maturity_book_item_textview"
android:textColor="#color/white"
android:fontFamily="#font/yanonekaffeesatz_medium"
android:background="#drawable/rounded_rectangle_90"
android:backgroundTint="#color/light_black"
android:padding="#dimen/_3sdp"
android:layout_marginTop="#dimen/_72sdp"
android:text="maturity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_5sdp"/>
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/option_book_item_button"
android:layout_width="#dimen/_20sdp"
android:layout_height="#dimen/_20sdp"
android:background="#drawable/ic_more"
android:backgroundTint="#color/light_black"
android:layout_alignLeft="#id/cover_book_item_cardview"
android:layout_marginLeft="#dimen/_260sdp"
android:layout_alignStart="#id/cover_book_item_cardview"
android:layout_marginStart="#dimen/_260sdp"
android:layout_marginTop="#dimen/_23sdp"/>
<ImageView
android:id="#+id/divider_book_item_imageview"
android:layout_width="#dimen/_200sdp"
android:layout_height="1dp"
android:background="#DFDFDF"
android:layout_below="#id/holder_book_item_linearlayout"
android:layout_marginTop="#dimen/_15sdp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Initializing RecyclerView:
recyclerView = findViewById(R.id.search_result_recyclerview);
recyclerView.scheduleLayoutAnimation();
recyclerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("CheckRecyclerView","clicked");
}
});
searchResultRecyclerViewAdapter = new SearchResultRecyclerViewAdapter(bookModels,SearchActivity.this);
recyclerView.setAdapter(searchResultRecyclerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
}
XML Layout of Activity:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerlayout"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:gravity="center"
android:animateLayoutChanges="true"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--
Views
-->
<androidx.recyclerview.widget.RecyclerView
android:layout_centerHorizontal="true"
android:layout_below="#id/appbar_cardview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/search_result_recyclerview"
android:layoutAnimation="#anim/search_result_recyclerview_anim"
android:visibility="gone"/> <!-- will become visible later-->
<!--
Views
-->
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
app:itemTextColor="#color/white"
app:itemIconTint="#color/white"
android:background="#color/black"
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_main" />
</androidx.drawerlayout.widget.DrawerLayout>
when the item's view is clicked nothing happens. I've done the same exact thing before with no problem I don't know what I'm missing here.
EDIT:
as #anemomylos mentioned this might be a click issue. in this layout there is a lot of Views that will be Gone in order to RecyclerView shows up. RecyclerViews don't register any clicks themselves so I'm not sure if it's being blocked by another view or not, is there any way to know what is blocking the click? I don't have any android:clickable="true" and I made sure that all Views before RecyclerView are Gone and not Invisible

Recycler View Items Not loading using firebase [duplicate]

This question already has answers here:
FirestoreAdapter not working to populate a Recycler View
(2 answers)
Closed 3 years ago.
ONStart Code
#Override
public void onStart() {
super.onStart();
DatabaseReference cartlistref = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid()).child("Cart");
FirebaseRecyclerAdapter<Cart, CartViewHolder> adapter = new FirebaseRecyclerAdapter<Cart, CartViewHolder>(
Cart.class,
R.layout.cart_items,
CartViewHolder.class,
cartlistref
) {
#Override
protected void populateViewHolder(CartViewHolder holder, Cart cart, int i) {
holder.pname.setText(cart.getName());
holder.pprice.setText(cart.getPrice());
//holder.quantity.setNumber(cart.getQuantity());
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
CartViewHolder Code
public class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView pname,pprice ;
public ImageView pimage;
public ElegantNumberButton quantity;
ItemClickListener itemClickListener;
public CartViewHolder(#NonNull View itemView) {
super(itemView);
pname = itemView.findViewById(R.id.pname);
pprice = itemView.findViewById(R.id.pprice);
quantity = itemView.findViewById(R.id.qty);
pimage = itemView.findViewById(R.id.pimage);
}
#Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
Cart items xml code
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:padding="16dp"
android:layout_height="160dp"
android:background="?attr/selectableItemBackground"
android:clickable="true">
<ImageView
android:id="#+id/pimage"
android:layout_width="151dp"
android:layout_height="match_parent"
app:srcCompat="#drawable/basket" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="150dp">
<TextView
android:id="#+id/pname"
android:layout_width="match_parent"
android:layout_height="57dp"
android:text="Product Name"
android:textSize="25sp"
android:textAlignment="center"
android:layout_gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black"
android:textStyle="normal|bold" />
<TextView
android:text="Product price"
android:textSize="18sp"
android:textAlignment="center"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/pprice"
android:layout_marginTop="4dp" />
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:textSize="18sp"
android:layout_width="200dp"
android:layout_height="50dp"
android:id="#+id/qty"
android:visibility="visible"
android:layout_marginTop="10dp"
android:layout_gravity="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Cart Fragment code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hackathon.shoppy.fragments.CartFragment">
<RelativeLayout
android:id="#+id/rill"
android:background="#color/colorPrimaryDark"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/totalamt"
android:textSize="20sp"
android:gravity="center"
android:textAlignment="center"
android:textColor="#android:color/white"
android:text="Total Amount = "
android:layout_width="match_parent"
android:layout_height="50dp"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cartList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fancy:layout_constraintBottom_toTopOf="#+id/EditDetails"
fancy:layout_constraintEnd_toEndOf="parent"
fancy:layout_constraintStart_toStartOf="parent"
fancy:layout_constraintTop_toTopOf="parent"
android:layout_below="#+id/rill"
android:layout_above="#+id/checkout"/>
<mehdi.sakout.fancybuttons.FancyButton
android:id="#+id/checkout"
android:layout_width="280dp"
android:layout_height="66dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:textSize="20sp"
fancy:fb_defaultColor="#7ab800"
fancy:fb_radius="10dp"
fancy:fb_text="Proceed To Checkout"
fancy:fb_textColor="#FFFFFF" />
</RelativeLayout>
Firebase Database Structure
Here is the firebase database structure
I have tried the above code
I want to load cart items of a specific user from the firebase database
The view is loading but the recycler view items arent showing up
Also the app is not crashing and no errors in logcat
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cartList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fancy:layout_constraintBottom_toTopOf="#+id/EditDetails"
fancy:layout_constraintEnd_toEndOf="parent"
fancy:layout_constraintStart_toStartOf="parent"
fancy:layout_constraintTop_toTopOf="parent"
android:layout_below="#+id/rill"
android:layout_above="#+id/checkout"/>
here is your problem. You are telling recycler view to WRAP CONTENT. problem is, that in moment of creation, content is empty, so recycler view has height zero.
Try to use
android:layout_width="match_parent"
android:layout_height="match_parent"
and experiment with height. But width for sure better to be left as match_parent
I think its because of wrap content, try to fill color in recylerview with blue or something, if you are not able to see then change to match parent

How to implement EmojiTextView in fragment?

I have an emoji in one of my data in cloud firestore. How do I can make the value readable in my fragment,recycleview? When adding old emoji such as 😭, it shows in my recycler view but when ading new emoji such as "🥺". Is it because of my android keyboard or language didn't update? I hope anyone can solve my problem.
ForumAdapter.java
public ForumHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
EmojiCompat.init(new BundledEmojiCompatConfig(v.getContext()));
return new ForumHolder(v);
}
class ForumHolder extends RecyclerView.ViewHolder{
EmojiTextView textViewTitle;
EmojiTextView textViewDescription;
EmojiTextView timeStamp;
/*
TextView textViewTitle;
TextView textViewDescription;
TextView timeStamp;*/
public ForumHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.tvTitle);
textViewDescription = itemView.findViewById(R.id.tvDescription);
timeStamp = itemView.findViewById(R.id.tvTimestamp);
textViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
// NO_POSITION to prevent app crash when click -1 index
if(position != RecyclerView.NO_POSITION && listener !=null ){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
Because I got this error
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to androidx.emoji.widget.EmojiTextView
cardviewforumtitle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="description"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="timestamp"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="#+id/profilePic"
android:layout_width="match_parent"
android:layout_height="match_parent"></ImageView>
</LinearLayout>
</LinearLayout>
Try replacing the TextViews with
<androidx.emoji.widget.EmojiTextView
..../>
in the layout.

Recyclerview individual item click listener

I have a recyclerview adapter on which I create listener to handle click event.
I implement this listener inside activity or fragment. The recyclerview items contains lot of views inside cardview.
My problem is when I click img_download no callback occur (the log not display) but when I click on other space (not img_download) the appropriate callback occur.
How do I know which view is clicked ?
Adapter Code:
public class ItuneEpisodeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tvTitle;
TextView tvDescription;
View divider;
ImageView img_download;
public ItuneEpisodeViewHolder(View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tv_podcast_title);
tvDescription = itemView.findViewById(R.id.tv_description);
divider = itemView.findViewById(R.id.divider);
img_download = itemView.findViewById(R.id.img_download);
itemView.setOnClickListener(this);
img_download.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view.getId() == img_download.getId()) {
mOnClickListener.onDownloadItemClick();
} else {
mOnClickListener.onItemClick(list.get(getAdapterPosition()), view);
}
}
}
public interface EpisodeClickListener {
void onItemClick(Episode podcast, View view);
void onDownloadItemClick();
}
Activity Implementation:
#Override
public void onItemClick(Episode episode, View view) {
PlayMediaActivity_.intent(this).extra("episode_extra", Parcels.wrap(episode)).extra("img", podcast.getCoverImage()).start();
}
#Override
public void onDownloadItemClick() {
Log.d("download", "yes");
}
Screenshot:
XML code:
<?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:layout_margin="10dp"
android:elevation="4dp">
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/md_blue_grey_100"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/img_podcast"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="5dp"
android:src="#drawable/podcast_img"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider" />
<TextView
android:id="#+id/tv_podcast_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Les chemins de la philosophie"
android:textColor="#color/md_black_1000"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="#+id/img_podcast"
app:layout_constraintRight_toLeftOf="#+id/img_download"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/tv_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:text="This is a description and should be replace with the best one let"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="#+id/tv_podcast_title"
app:layout_constraintRight_toLeftOf="#+id/img_download"
app:layout_constraintTop_toBottomOf="#+id/tv_podcast_title" />
<ImageView
android:id="#+id/img_calendar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_calendar"
app:layout_constraintLeft_toLeftOf="#+id/tv_description"
app:layout_constraintTop_toBottomOf="#+id/tv_description" />
<TextView
android:id="#+id/tv_calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Fév. 14 2016"
app:layout_constraintBottom_toBottomOf="#+id/img_calendar"
app:layout_constraintLeft_toRightOf="#+id/img_calendar" />
<ImageView
android:id="#+id/img_time"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:src="#drawable/ic_time"
app:layout_constraintBottom_toBottomOf="#+id/tv_calendar"
app:layout_constraintLeft_toRightOf="#+id/tv_calendar" />
<TextView
android:id="#+id/tv_realisateur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="49:50"
app:layout_constraintBottom_toBottomOf="#+id/img_time"
app:layout_constraintLeft_toRightOf="#+id/img_time" />
<ImageView
android:id="#+id/img_download"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:tint="#color/primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_drawer_download" />
</android.support.constraint.ConstraintLayout>
Solved it, add this two code on ConstraintLayout (parent layout) and on your img_download (ImageView)
android:clickable="true"
android:focusable="true"
and instead of itemView, put an ID on your ConstraintLayout and set an onClickListener on it.
ConstraintLayout main;
TextView tvTitle;
TextView tvDescription;
View divider;
ImageView img_download;
public ItuneEpisodeViewHolder(View itemView) {
super(itemView);
main = itemView.findViewById(R.id.main); //add this
tvTitle = itemView.findViewById(R.id.tv_podcast_title);
tvDescription = itemView.findViewById(R.id.tv_description);
divider = itemView.findViewById(R.id.divider);
img_download = itemView.findViewById(R.id.img_download);
main.setOnClickListener(this); //set the onclick on the parent layout
img_download.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (view.getId() == R.id.img_download) {
mOnClickListener.onDownloadItemClick();
} else if (view.getId() == R.id.main) {
mOnClickListener.onItemClick(list.get(getAdapterPosition()), view);
}
}
XML:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main" //add this
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true" //add this
android:focusable="true" //add this
android:elevation="4dp">
...
...
<ImageView
android:id="#+id/img_download"
android:clickable="true" //add this
android:focusable="true" //add this
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="5dp"
android:tint="#color/primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_drawer_download" />
</android.support.constraint.ConstraintLayout>

Firebaserecyclerview Gravity

I'm trying to create a Chat App. It is working really good. But now I'd like to gravity the messages. I don't know how I can set the gravity of the sent messages to the right side. viewHolder.mView.setForegroundGravity(); doesn't work.
public void populateViewHolder(final BlogViewHolder viewHolder, final Blog model, final int position) {
DatabaseReference getname = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Username");
getname.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Grün und weiß setzen
String myname = dataSnapshot.getValue(String.class);
if(myname.equals(model.getUsername())){
LinearLayout linear = (LinearLayout)findViewById(R.id.singleMessageContainer);
linear.setGravity(Gravity.RIGHT);
viewHolder.setNachricht(model.getNachricht());
}else{
viewHolder.setNachricht(model.getNachricht());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachricht(String Nachricht){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.textViewMessage);
post_Nachricht.setText(Nachricht);
}
public void setUsername(String Username){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.kommentareimg);
post_Nachricht.setText(Html.fromHtml("<b>" + Username + ":</b>"));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
<LinearLayout
android:id="#+id/warpper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textViewMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Message"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</LinearLayout>
</LinearLayout>
In your case you might consider having two TextView in your list item layout. Make the layout such that one layout is aligned to the right and the other is aligned to the left. Now you just have to set the visibility of the left or right layout to GONE or VISIBLE.
So you need to have some other functions in your ViewHolder class.
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setNachrichtRight(String Nachricht) {
TextView post_Nachricht_Right = (TextView)mView.findViewById(R.id.textViewMessageRight);
TextView post_Nachricht_Left = (TextView)mView.findViewById(R.id.textViewMessageLeft);
post_NachrichtRight.setText(Nachricht);
// Now set the visibility
post_Nachricht_Right.setVisibility(View.VISIBLE);
post_Nachricht_Left.setVisibility(View.GONE);
}
public void setNachrichtLeft(String Nachricht) {
TextView post_NachrichtLeft = (TextView)mView.findViewById(R.id.textViewMessageLeft);
TextView post_NachrichtRight = (TextView)mView.findViewById(R.id.textViewMessageRight);
post_NachrichtLeft.setText(Nachricht);
// Now set the visibility
post_Nachricht_Right.setVisibility(View.VISIBLE);
post_Nachricht_Left.setVisibility(View.GONE);
}
public void setUsername(String Username){
TextView post_Nachricht = (TextView)mView.findViewById(R.id.kommentareimg);
post_Nachricht.setText(Html.fromHtml("<b>" + Username + ":</b>"));
}
}
So inside the onDataChange function you may consider doing something like this.
if(myname.equals(model.getUsername())){
viewHolder.setNachrichtRight(model.getNachricht());
} else {
viewHolder.setNachrichtLeft(model.getNachricht());
}
Finally, your layout may look something like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="#+id/textViewMessageRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Message"
android:textSize="14sp"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textViewMessageLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignParentLeft="true"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:text="Message"
android:textSize="14sp"
tools:ignore="HardcodedText" />
</RelativeLayout>

Categories

Resources