While working on the project I faced the task - Open the added video in WebView .

Having found a wonderful sample, I decided to look at his example.

The essence of the project:

  1. The user adds the copied link (from youtube / vimeo) to our application (by clicking the ADD button)
  2. After the user has clicked "ADD" in our RecyclerView, we get a preview of our video.

Well, actually the task , after we added the preview of our video, is to add the ability to click on the list item (in this case, our video) to open it by clicking on the WebView (in the browser) or in the application itself.

Extras: I found a way to open videos beautifully in a new activit, but alas, I don’t know how to use them for links that will be added each time in different ways. That is why if I understand correctly, you need to save the links that the user enters - a guess .

String frameVideo = "<iframe width=\"100%\" height=\"415\" src=\"https://www.youtube.com/embed/"\" frameborder=\"0\" allowfullscreen></iframe>"; 

Well, actually the working code of the program itself: Adapter for RecyclerView

 public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> { private ArrayList<VideoPreview> mItems; private Context mContext; public DataAdapter(Context context) { mContext = context; mItems = new ArrayList<>(); } @Override public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_video_preview, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { viewHolder.mPreviewLinkTextView.setText(mItems.get(i).getVideoLink()); viewHolder.mPreviewLinkPreviewTextView.setText(mItems.get(i).getPreviewLink()); Picasso.with(mContext).load(mItems.get(i).getPreviewLink()).resize(120, 90).into(viewHolder.mPreviewImageView); } @Override public int getItemCount() { return mItems.size(); } public void addItem(VideoPreview videoPreview) { mItems.add(videoPreview); notifyDataSetChanged(); } public boolean contains(VideoPreview newVideoPreview) { for (VideoPreview videoPreview : mItems) { if (videoPreview.getVideoLink().equals(newVideoPreview.getVideoLink())) return true; } return false; } public class ViewHolder extends RecyclerView.ViewHolder{ ImageView mPreviewImageView; TextView mPreviewLinkTextView; TextView mPreviewLinkPreviewTextView; public ViewHolder(View view) { super(view); mPreviewImageView = (ImageView) view.findViewById(R.id.item_video_preview_image_view); mPreviewLinkTextView = (TextView) view.findViewById(R.id.item_video_preview_link_text_view); mPreviewLinkPreviewTextView = (TextView) view.findViewById(R.id.item_video_preview_link_preview_text_view); } } } 

Acitivity code

 public class VideoPreviewActivity extends AppCompatActivity implements VideoTypes { private RecyclerView mVideoPreviewRecyclerView; private EditText mAddEditText; private Button mAddButton; private OkHttpClient httpClient; private DataAdapter mDataAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_preview); mVideoPreviewRecyclerView = (RecyclerView) findViewById(R.id.video_preview_recycler_view); mAddEditText = (EditText) findViewById(R.id.add_edit_text); mAddButton = (Button) findViewById(R.id.add_button); mVideoPreviewRecyclerView = (RecyclerView)findViewById(R.id.video_preview_recycler_view); mVideoPreviewRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); mVideoPreviewRecyclerView.setLayoutManager(layoutManager); mDataAdapter = new DataAdapter(getApplicationContext()); mVideoPreviewRecyclerView.setAdapter(mDataAdapter); httpClient = new OkHttpClient(); mAddButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mDataAdapter.getItemCount() == 10) { Toast.makeText(getApplicationContext(), "You can not add more than 10 videos!", Toast.LENGTH_SHORT).show(); return; } String videoLink = mAddEditText.getText().toString(); int videoType = VideoMethods.getLinkType(videoLink); if (videoType == TYPE_UNKNOWN) { Toast.makeText(getApplicationContext(), "Incorrect link!", Toast.LENGTH_SHORT).show(); return; } mAddEditText.getText().clear(); InputMethodManager inputMethodManager = (InputMethodManager) VideoPreviewActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(VideoPreviewActivity.this.getCurrentFocus().getWindowToken(), 0); String videoId = VideoMethods.getId(videoLink); switch (videoType) { case TYPE_YOUTUBE: VideoPreview videoPreview = new VideoPreview(videoLink, "http://img.youtube.com/vi/" + videoId + "/1.jpg"); if (!mDataAdapter.contains(videoPreview)) { mDataAdapter.addItem(videoPreview); mVideoPreviewRecyclerView.scrollToPosition(mDataAdapter.getItemCount()-1); } else { Toast.makeText(getApplicationContext(), "This video already exists!", Toast.LENGTH_SHORT).show(); } break; case TYPE_VIMEO: try { getVimeoPreview(videoLink, videoId); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Something going wrong!", Toast.LENGTH_SHORT).show(); } break; } } }); } public void getVimeoPreview(final String videoLink, final String videoId) throws Exception { Request request = new Request.Builder() .url("http://vimeo.com/api/v2/video/" + videoId + ".xml") .build(); httpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Toast.makeText(getApplicationContext(), "Something going wrong!", Toast.LENGTH_SHORT).show(); } @Override public void onResponse(Call call, Response response) throws IOException { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String s = response.body().string(); Pattern p = Pattern.compile("<thumbnail_small>(.*)<\\/thumbnail_small>"); final Matcher m = p.matcher(s); m.find(); runOnUiThread(new Runnable() { @Override public void run() { VideoPreview videoPreview = new VideoPreview(videoLink, m.group(1)); if (!mDataAdapter.contains(videoPreview)) { mDataAdapter.addItem(videoPreview); mVideoPreviewRecyclerView.scrollToPosition(mDataAdapter.getItemCount()-1); } else { Toast.makeText(getApplicationContext(), "This video already exists!", Toast.LENGTH_SHORT).show(); } } }); } }); } } 

Perhaps the VideoMethods class will also be useful for handling video links:

 public static int getLinkType(String link) { Pattern pattern = Pattern.compile("^https:\\/\\/www\\.(youtube\\.com)\\/watch\\?v=[\\w-]{11}$|^https:\\/\\/(youtu\\.be)\\/[\\w-]{11}$|^https:\\/\\/(vimeo\\.com)\\/[0-9]*$"); Matcher matcher = pattern.matcher(link); if (!matcher.matches()) return TYPE_UNKNOWN; for (int i=1; i<=matcher.groupCount(); i++) { if (matcher.group(i) != null) { switch (i) { case 1: case 2: return TYPE_YOUTUBE; case 3: return TYPE_VIMEO; default: return TYPE_UNKNOWN; } } } return TYPE_UNKNOWN; } public static String getId(String link) { Pattern pattern = Pattern.compile("^https:\\/\\/www\\.youtube\\.com\\/watch\\?v=([\\w-]{11})$|^https:\\/\\/youtu\\.be\\/([\\w-]{11})$|^https:\\/\\/vimeo\\.com\\/([0-9]*)$"); Matcher matcher = pattern.matcher(link); matcher.matches(); for (int i=1; i<=matcher.groupCount(); i++) { if (matcher.group(i) != null) { return matcher.group(i); } } return null; } } 
  • The question is incomprehensible. What does "check click on an item (containing a link)" mean? - post_zeew
  • Well, I click on the item in the list and this item contains a link. - Inkognito
  • Hmm, somewhere I already saw and told. Well, without an adapter code for the “list”, you are unlikely to be told something specific. - post_zeew
  • Yes, I’m still looking for a solution, according to your advice, but in a different way) I try differently. - Inkognito
  • You take a span, you stylize it under a link, you hang up a click on a click and there you check everything you want. - Drag13

1 answer 1

but alas, I don’t know how to use links that will be added each time in different ways. That is why if I understand correctly, you need to save the links that the user enters.

When adding items to the list, these items are added to the adapter mItems , that is, links to the video will be automatically saved.

Play video in WebView - very bad idea

But if you really want to ...

Adapter:

 public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> { private ArrayList<VideoPreview> mItems; private Context mContext; public DataAdapter(Context context) { mContext = context; mItems = new ArrayList<>(); } @Override public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_video_preview, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { viewHolder.mPreviewLinkTextView.setText(mItems.get(i).getVideoLink()); viewHolder.mPreviewLinkPreviewTextView.setText(mItems.get(i).getPreviewLink()); Picasso.with(mContext).load(mItems.get(i).getPreviewLink()).resize(120, 90).into(viewHolder.mPreviewImageView); } @Override public int getItemCount() { return mItems.size(); } public void addItem(VideoPreview videoPreview) { mItems.add(videoPreview); notifyDataSetChanged(); } public boolean contains(VideoPreview newVideoPreview) { for (VideoPreview videoPreview : mItems) { if (videoPreview.getVideoLink().equals(newVideoPreview.getVideoLink())) return true; } return false; } public class ViewHolder extends RecyclerView.ViewHolder{ ImageView mPreviewImageView; TextView mPreviewLinkTextView; TextView mPreviewLinkPreviewTextView; public ViewHolder(View view) { super(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext, VideoActivity.class); intent.putExtra("video_link", mItems.get(getAdapterPosition()).getVideoLink()); mContext.startActivity(intent); } }); mPreviewImageView = (ImageView) view.findViewById(R.id.item_video_preview_image_view); mPreviewLinkTextView = (TextView) view.findViewById(R.id.item_video_preview_link_text_view); mPreviewLinkPreviewTextView = (TextView) view.findViewById(R.id.item_video_preview_link_preview_text_view); } } } 

activity_video.xml :

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/video_web_view" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> </RelativeLayout> 

VideoActivity :

 public class VideoActivity extends AppCompatActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); mWebView = (WebView) findViewById(R.id.video_web_view); Bundle extras = getIntent().getExtras(); if (extras != null) { String videoLink = extras.getString("video_link"); mWebView.setWebChromeClient(new WebChromeClient() { }); mWebView.loadUrl(videoLink); } } } 
  • Thank you for the detailed answer, but why is the idea not very good? Because it is to this option that I came looking for a solution on the Internet. How would you advise you to play the video (the option with the installation mediaplayer is not relevant to me)? - Inkognito
  • + in RecyclerView you access the link via getAdapterPosition, and what is the equivalent in gridView? - Inkognito
  • I figured out how to access gridView, but as I understand it, elements are not added to adapter items. Therefore, links should be somehow saved. Do not tell me how this is possible? - Inkognito
  • @Inkognito, Because in 9 out of 10 cases the video will not play. I would advise using the corresponding video hosting APIs. - post_zeew
  • one
    @Inkognito, The link is located in the object of the VideoPreview class; when adding an element to the list, we add the corresponding object to the adapter's ArrayList . - post_zeew