javaandroidandroid-recyclerview

At what point can I get the width of a view in a RecyclerView?


I am using recycler. I need to set height of the view as the width of the container. But the problem is when I am trying to get the width size it seems not exist yet so it returns zero. How I can make sure that view is already created to get its value and use it while setting the height?

That is the code where i am setting the height

imageView.getLayoutParams().height = container.getWidth();
        imageView.requestLayout();

The code of the adapter

public class OnlineUsersAdapter extends RecyclerView.Adapter<OnlineUsersAdapter.OnlineUserViewHolder> {

private List<OnlineUser> onlineUsers = new ArrayList<>();
private OnItemClickListener.OnItemClickCallback onItemClickCallback;
private OnItemClickListener.OnItemClickCallback onChatClickCallback;
private OnItemClickListener.OnItemClickCallback onLikeClickCallback;
private Context context;

public OnlineUsersAdapter(OnItemClickListener.OnItemClickCallback onItemClickCallback,
                     OnItemClickListener.OnItemClickCallback onChatClickCallback,
                     OnItemClickListener.OnItemClickCallback onLikeClickCallback) {
    this.onItemClickCallback = onItemClickCallback;
    this.onChatClickCallback = onChatClickCallback;
    this.onLikeClickCallback = onLikeClickCallback;
}

public void addUsers(List<OnlineUser> userList) {
    onlineUsers.addAll(userList);
    notifyItemRangeInserted(onlineUsers.size() - userList.size(), onlineUsers.size());
}

public void clearData(List<OnlineUser> userList){
    int oldSize = userList.size();
    onlineUsers = userList;
    notifyItemRangeRemoved(0, oldSize);
    notifyItemRangeInserted(0, userList.size());
}

@NonNull
@Override
public OnlineUsersAdapter.OnlineUserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    context = parent.getContext();
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, parent, false);
    return new OnlineUsersAdapter.OnlineUserViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull OnlineUsersAdapter.OnlineUserViewHolder holder, int position) {
    OnlineUser user = onlineUsers.get(position);
    holder.bind(user, position);
}

@Override
public int getItemCount() {
    return onlineUsers.size();
}

class OnlineUserViewHolder extends RecyclerView.ViewHolder {
    RelativeLayout container;
    ImageView imageView, likeBtn, chatBtn;
    TextView name, country;

    private LottieAnimationView animationView;

    OnlineUserViewHolder(View itemView) {
        super(itemView);
        context = itemView.getContext();
        container = itemView.findViewById(R.id.item_user_container);
        imageView = itemView.findViewById(R.id.user_img);
        likeBtn = itemView.findViewById(R.id.search_btn_like);
        chatBtn = itemView.findViewById(R.id.search_btn_chat);
        name = itemView.findViewById(R.id.user_name);
        country = itemView.findViewById(R.id.user_country);
        animationView = itemView.findViewById(R.id.lottieAnimationView);
    }

    void bind(OnlineUser user, int position) {
        ViewCompat.setTransitionName(imageView, user.getName());

        if (FirebaseUtils.isUserExist() && user.getUid() != null) {
            new FriendRepository().isLiked(user.getUid(), flag -> {
                if (flag) {
                    likeBtn.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite));
                    animationView.setVisibility(View.VISIBLE);
                } else {
                    likeBtn.setBackground(ContextCompat.getDrawable(context, R.drawable.heart_outline));
                    animationView.setVisibility(View.GONE);
                }
            });
        }

        if (user.getUid() != null) {
            chatBtn.setOnClickListener(new OnItemClickListener(position, onChatClickCallback));
            likeBtn.setOnClickListener(new OnItemClickListener(position, onLikeClickCallback));
        }

        imageView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);


        if (user.getImage().equals(Consts.DEFAULT)) {
            Glide.with(context).load(context.getResources().getDrawable(R.drawable.default_avatar)).into(imageView);
        } else {
            Glide.with(context).load(user.getImage()).thumbnail(0.5f).into(imageView);
        }

        country.setText(user.getCountry());

        ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f).setDuration(500);
        animator.addUpdateListener(valueAnimator ->
                animationView.setProgress((Float) valueAnimator.getAnimatedValue()));

        if (animationView.getProgress() == 0f) {
            animator.start();
        } else {
            animationView.setProgress(0f);
        }
        imageView.getLayoutParams().height = 500;
        imageView.requestLayout();
    }
}

}


Solution

  • Wait until your view gets rendered on UI. There are a few ways to do that. One of them is to use post() of the view which you want to get the height.

    Like

    container.post(new Runnable() {
        @Override
        public void run() {
            imageView.getLayoutParams().height = container.getWidth();
            imageView.requestLayout();
        }
    });
    

    And View's getWidth() and getHeight() returns 0 is an amazing post which contains few Kotlin solutions too, which are more easier than Java.