How would I use YouTubePlayerView with holder to display in recyclerview? - java

I want to get the video ID from my database, use the string and load the YouTube video in the cardview. But I'm lost on how to load the video using the holder.
This is my onBindViewHolder in my adapter class.
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
Exercise exercise = list.get(position);
holder.exerciseName.setText(exercise.getExerciseName());
holder.muscleGroupName.setText(exercise.getMuscleGroupName());
holder.equipmentName.setText(exercise.getEquipmentName());
Glide.with(holder.imageView).load(exercise.getImage()).into(holder.imageView);
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
TextView exerciseName, muscleGroupName, equipmentName;
YouTubePlayerView youTubePlayerView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
exerciseName = itemView.findViewById(R.id.exerciseNameInput);
muscleGroupName = itemView.findViewById(R.id.muscleGroupNameInput);
equipmentName = itemView.findViewById(R.id.equipmentNameInput);
imageView = itemView.findViewById(R.id.imageInput);
youTubePlayerView = itemView.findViewById(R.id.video);
}
}

Related

Why my images is not showing from firebase firestore?

I am working on an application where i am uploading multiple images with 4 edittexts to the firebase then I am trying to retrieve the data to the recyclerview now my editext's data is retrieving but the data of images is not showing in reyclerview.
my adapter code
#Override
public void onBindViewHolder(#NonNull Viewholder holder, int position)
{
holder.name.setText(datalist.get(position).getName());
holder.email.setText(datalist.get(position).getEmail());
holder.desc.setText(datalist.get(position).getDesc());
holder.book.setText(datalist.get(position).getBook());
Glide.with(context).load(datalist.get(position).getImage())
.into(holder.imageView);
}
#Override
public int getItemCount() {
return datalist.size();
}
public class Viewholder extends RecyclerView.ViewHolder{
TextView name, email, desc, book;
ImageView imageView;
public Viewholder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.text_name);
email = itemView.findViewById(R.id.text_email);
desc = itemView.findViewById(R.id.text_desc);
book = itemView.findViewById(R.id.text_book);
imageView = itemView.findViewById(R.id.imageView3);
}
}
In the comment session you mentioned, getImage() is an array then, How it will load the image you need to pass a particular image in the array?
Let's suppose, If you want to load the first image in an array use the below code -
if(!wishlist_models.get(position).getImage().isEmpty()) {
Glide.with(context)
.load(wishlist_models.get(position).getImage()get(0))
.into(holder.wishlist_image);
}

How to change only the background alpha of TextView not the whole view?

N.B: This might be a duplicate post. But no previous solution worked for me...
I'm trying to set TextView (only)background opacity from inside a RecyclerView Adapter(not both the textColor & background). Answer of previous question didn't worked for me.
Output of Previous Suggestions
Case 1:
holder.textView.setBackgroundColor(Color.argb(50, 0,255,0)); //this set alpha to whole view(both background + textColor).
Case 2:
holder.textView.setAlpha(50); //this also set alpha to whole view(both background + textColor).
Case 3:
holder.textView.getBackground().setAlpha(alpha); //NullPointerException
Output
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setAlpha(int)' on a null object reference
Here is my RecyclerView :
...
#RequiresApi(api = Build.VERSION_CODES.O)
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
TextView textView = new TextView(parent.getContext());
return new ViewHolder(textView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.textView.setAlpha(50);
}
static class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
#RequiresApi(api = Build.VERSION_CODES.O)
public ViewHolder(#NonNull View itemView) {
super(itemView);
this.textView = (TextView) itemView;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setPadding(4,4,4,4);
textView.setAutoSizeTextTypeUniformWithConfiguration(6,24,1, TypedValue.COMPLEX_UNIT_DIP);
}
}
...
Try this:
....
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ShapeDrawable drawable = new ShapeDrawable();
drawable.getPaint().setColor(Color.GREEN); //Your color;
drawable.getPaint().setAlpha(75); //BackGround Alpha between 0 to 100;
holder.textView.setBackground(drawable);
}

How to get value from position adapter RecyclerView

I have 2 data items where the data has different id. When i click radio button then getting id by position.
Example : I have 2 data items, the first id is 57 and the second id is 59. When I click on the first data it must get a value of 57 or when I click on the second data it must get a value of 59.
private String convertAddressID;
private int previousSelected = -1;
private boolean isRadioChecked;
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
ModelGetAddress adapterAddress = modelGetAddressList.get(position);
convertAddressID = String.valueOf(adapterAddress.getId());
}
public class ViewHolder extends RecyclerView.ViewHolder {
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
Toast.makeText(getContext, convertAddressID, Toast.LENGTH_SHORT).show();
}
});
}
}
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
ModelGetAddress adapterAddress = modelGetAddressList.get(position);
convertAddressID = String.valueOf(adapterAddress.getId());
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.radioButton);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int position=getAdapterPosition();
Toast.makeText(conext, ""+modelGetAddressList.get(position).getId(), Toast.LENGTH_SHORT).show();
}
}
You can set the onClickListener in the onBindViewHolder where you always have the position. Call the findViewById in the ViewHolder class.
Try something like this my (untested) code:
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, final int position) {
((ViewHolder)holder).button.setOnClickListener(...)
}
public class ViewHolder extends RecyclerView.ViewHolder {
Button button;
ViewHolderContent(#NonNull View itemView) {
super(itemView);
button= itemView.findViewById(R.id.button);
}
}

Unable to declare variable

Why I'm unable to declare variable in MessageViewHolder class? I want to findviewById for my xml file. BTW I'm running latest version of Android Studio in latest 64 bit Ubuntu. But, surely there is no problem with version. I tried with some other variable. Same problem!
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.message_single_layout, parent, false);
return new MessageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MessageViewHolder holder, int position) {
Messages messages = messagesList.get(position);
holder.chatText.setText(messages.getMessage());
}
#Override
public int getItemCount() {
return messagesList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView chatText;
public CircleImageView chatImage;
public MessageViewHolder(View itemView) {
super(itemView);
}
chatText =
chatImage
}
}
You need to do findViewById inside MessageViewHolder() Using itemView
Try this
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView chatText;
public CircleImageView chatImage;
public MessageViewHolder(View itemView) {
super(itemView);
chatText =itemView.findViewById(R.id.chatText);
chatImage=itemView.findViewById(R.id.chatImage);
}
}
In view holder you take id's from view
So it is -
public MessageViewHolder(View itemView) {
super(itemView);
chatText = itemView.findViewById(R.id.*);
chatImage = itemView.findViewById(R.id.*);
}

Horizontal RecyclerView of images

Which is the code to obtain a Horizontal RecyclerView of images? I already have the code for horizontal RecyclerView of Strings. I need to have the same but of Images.
I have to create a RecyclerView of 10 horizontal scrollable imageView.
This is my actual code of 10 horizontal TextViews:
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
private String[] items;
public HorizontalAdapter(String[] items) {
this.items = items;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.text.setText(items[position]);
}
#Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
TextView text;
public HorizontalViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text_view_top_10_ricette_titolo_ricetta);
} }
}
and I manage it on a Fragment in this way:
RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
list.setAdapter(new HorizontalAdapter(new String[]{"Android","Programming","Java","RecyclerView","Layout"}));
How I have to modify this?
Try this make below changes in your HorizontalAdapter
public class HorizontalAdapter extends RecyclerView.Adapter<HorizontalAdapter.HorizontalViewHolder> {
private int[] items;
public HorizontalAdapter(String[] int) {
this.items = items;
}
#Override
public HorizontalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout_recycler_view_top_10, parent, false);
return new HorizontalViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalViewHolder holder, int position) {
holder.image.setImageResource(items[position]);
}
#Override
public int getItemCount() {
return items.length;
}
public class HorizontalViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public HorizontalViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.imageviewid);
} }
}
code for RecyclerView
RecyclerView list = (RecyclerView) view.findViewById(R.id.recycler_view_ricette_categorie_primi_top_10);
list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
list.setAdapter(new HorizontalAdapter(new int[]{R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher}));
NOTE : Your layout (item_layout_recycler_view_top_10) needs to contain a ImageView instead of a TextView
Instead of a recyclerView full of textViews (R.layout.item_layout_recycler_view_top_10) you want images?
Just create another layout which contains an ImageView instead of a TextView in it and use it accordingly to your current solution.
String array change to image url array or drawable image array create and send to adapter and set into imageview.

Categories