Search code examples
javaandroidandroid-recyclerviewonclicklistenerandroid-viewholder

How to open a URL in a web browser with an intent from a recycler adapter


I am unable to open a web browser to get to a url from my recycler adapter after a click. I initialized the adapter with a context but still the application will crash.

public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder> {

private ArrayList<Item> mItems;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public RecyclerAdapter(ArrayList<Item> items, Context context) {
        mItems = items;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

//other things....

public class ViewHolder extends RecyclerView.ViewHolder{
        public final ImageView mImageView;

        public TopFreeStoreViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.imageViewItem);
            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String Url = www.google.com;
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(Url));
                    mContext.startActivity(intent);
                }
            });
        }
    }
}

The error: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com. }


Solution

  • The url used by you is in wrong format. Try this,

    String Url = "https://www.google.com";