0

I have some url strings, some ending in .png and others in .svg. The .png ones load into the recyclerview fine but the .svg ones are nowhere to be seen. I have searched through many forums but nothing has worked as of yet.

I have the following code in the onBindViewHolder of my recyclerviewadapter which finds out if the url is a .svg or not. If anyone has any ideas I would be very grateful.

    String[] homeUrlSplit = mData.get(i).getHomeTeamBadge().split("\\.");

    String homeImageType = homeUrlSplit[homeUrlSplit.length-1];

    if(homeImageType.equals("svg")){
        // do something here
    }
    else {
        Glide
                .with(mContext)
                .load(mData.get(i).getHomeTeamBadge())
                .into(myViewHolder.homeTeamBadge);
    }

Thanks

Edit: I do not quite understand the solution included in the comments under the "Possible duplicate" answer. If anyone knows of any other solutions, or would be able to explain this particular solution in layman's terms I would be very grateful.

Here is the adapter setup:

    RecyclerViewAdapterFixtures recyclerViewAdapterFixtures = new RecyclerViewAdapterFixtures(getContext(), matchesList, getActivity());
    recyclerViewFixtures.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerViewFixtures.setAdapter(recyclerViewAdapterFixtures);

And here is the recylerviewadapter code

public class RecyclerViewAdapterFixtures extends RecyclerView.Adapter<RecyclerViewAdapterFixtures.MyViewHolder> {

Context mContext;
List<Match> mData;
Activity mActivity;

public RecyclerViewAdapterFixtures(Context mContext, List<Match> mData, Activity mActivity) {
    this.mContext = mContext;
    this.mData = mData;
    this.mActivity = mActivity;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View v;
    v = LayoutInflater.from(mContext).inflate(R.layout.item_fixture, viewGroup, false);
    MyViewHolder vHolder = new MyViewHolder(v);

    return vHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {

    myViewHolder.textViewHomeTeam.setText(mData.get(i).getHomeTeamName());

    String[] homeUrlSplit = mData.get(i).getHomeTeamBadge().split("\\.");

    String homeImageType = homeUrlSplit[homeUrlSplit.length-1];

    if(homeImageType.equals("svg")){

        SvgLoader.pluck()
                .with(mActivity)
                .setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                .load(mData.get(i).getHomeTeamBadge(), myViewHolder.homeTeamBadge);

    }
    else {
        Glide
                .with(mContext)
                .load(mData.get(i).getHomeTeamBadge())
                .into(myViewHolder.homeTeamBadge);
    }
}

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

public static class MyViewHolder extends RecyclerView.ViewHolder {

    private TextView textViewVersus;
    private EditText homeTeamScore;
    private EditText awayTeamScore;
    private TextView textViewHomeTeam;
    private TextView textViewAwayTeam;
    private ImageView homeTeamBadge;
    private ImageView awayTeamBadge;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        textViewVersus = (TextView) itemView.findViewById(R.id.textViewVersus);
        homeTeamScore = (EditText) itemView.findViewById(R.id.homeTeamScore);
        awayTeamScore = (EditText) itemView.findViewById(R.id.awayTeamScore);
        textViewHomeTeam = (TextView) itemView.findViewById(R.id.textViewHomeTeam);
        textViewAwayTeam = (TextView) itemView.findViewById(R.id.textViewAwayTeam);
        homeTeamBadge = (ImageView) itemView.findViewById(R.id.homeTeamBadge);
        awayTeamBadge = (ImageView) itemView.findViewById(R.id.awayTeamBadge);

    }
}

}

  • Possible duplicate of [Does Glide have a method for loading both PNG and SVG?](https://stackoverflow.com/questions/35507893/does-glide-have-a-method-for-loading-both-png-and-svg) – Ufkoku Aug 21 '18 at 13:53
  • Thanks for your reply. This is one of the forums I had previously come across, and it's where I thought to find out if the url ends with .svg or something else. But I was unable to get the request builder to work – Jack Heslop Aug 21 '18 at 13:59

1 Answers1

1

Put this in ur build.gradle file(app level) in depedencies :

implementation 'com.github.ar-android:AndroidSvgLoader:1.0.1'

and in build.gradle project level you should add maven { url 'https://jitpack.io' } so it will look like this in most cases

allprojects {
  repositories {
    google()
    jcenter()
   maven { url 'https://jitpack.io' }
  }
 }

then

String[] homeUrlSplit = mData.get(i).getHomeTeamBadge().split("\\.");

 String homeImageType = homeUrlSplit[homeUrlSplit.length - 1];

 if (homeImageType.equals("svg")) {
  SvgLoader.pluck()
   .with(mActivity) // ur activity
   .setPlaceHolder(R.mipmap.ic_launcher, R.mipmap.ic_launcher)//use ur place holder
   .load(mData.get(i).getHomeTeamBadge(), myViewHolder.homeTeamBadge);
 } else {
  Glide
   .with(mContext)
   .load(mData.get(i).getHomeTeamBadge())
   .into(myViewHolder.homeTeamBadge);
 }

Remove Glide dependencies implementations from ur project if you have any , since the AndroidSVGLoader already have them

Badran
  • 515
  • 1
  • 7
  • 21
  • Thanks for your reply. Unfortunately I am getting this error when trying to sync the gradle: Failed to resolve: com.github.ar-android:AndroidSvgLoader:1.0.1. Edit: Managed to fix this by adding maven { url 'https://jitpack.io' } into repositories – Jack Heslop Aug 21 '18 at 19:33
  • yes , I just edited the solution check it out now... you should add maven { url 'https://jitpack.io' } to build.gradle in project level in repositories – Badran Aug 21 '18 at 19:37
  • So I have implemented your method, but the use of .with(mContext) brings up an error as the parameter needs to be an activity. Have tried using mActivity but this causes a crash and brings up the error: No static method buildStreamModelLoader(Ljava/lang/Class;Landroid/content/Context;)Lcom/bumptech/glide/load/model/ModelLoader; in class Lcom/bumptech/glide/Glide; or its super classes (declaration of 'com.bumptech.glide.Glide' appears in /data/app/com.example.jackh.adivinar-1/split_lib_dependencies_apk.apk) – Jack Heslop Aug 21 '18 at 19:46
  • are you assigning mContext through constructor from the activity? if yes then please can you show me your Adapter – Badran Aug 21 '18 at 19:52
  • I am yes. For added context I am writing this code in the recyclerviewadapter and the recyclerview is in a fragment. I know that sometimes the use of fragments can mean that context needs to be defined differently. Could that be a reason? – Jack Heslop Aug 21 '18 at 19:55
  • I will add the adapter into the main post – Jack Heslop Aug 21 '18 at 19:56
  • Okay, I just implemented a recycler view with SVG images using the same solution I gave to you and it worked fine. I can send you the example via email if you want or post it here and the problem now is with the Context not with loading SVG images into recycler view maybe you should open a new thread for it and accept my solution if you wish so :) and please link me to the new thread if you want to open. – Badran Aug 21 '18 at 20:02
  • I have added the adapter code. Ok so I suspect there's only a minor change that needs to be made for this to work. I will accept your solution as clearly the method is correct. I'd be very grateful if you could stick around to try and get to the bottom of it with me! As it may help others if they're in a similar position – Jack Heslop Aug 21 '18 at 20:09
  • I edited the solution it should be mActivity not mContext , my bad sorry but I will try to find the second error – Badran Aug 21 '18 at 20:41
  • ohhhh I got it , lol try removing Glide implementation in ur build.gradle file since the library i gave you is already implementing it , if you have implementation 'com.github.bumptech.glide:glide:4.8.0' and annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' just remove them and it will work fine for sure – Badran Aug 21 '18 at 21:03
  • 1
    That's the ticket! Thank you so so much I really appreciate it – Jack Heslop Aug 21 '18 at 21:11
  • You are most welcome, and I am sorry I should have noticed that before :( that he is using Glide also feel free to upvote the solution now – Badran Aug 21 '18 at 21:15