There is a dialog where the user should insert a link.
data is entered into EditText
dialogue code:
private void showAddVideoDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setTitle("Добавить видео"); View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.view_layout_add_video, (ViewGroup) getView(), false); final EditText input = (EditText) viewInflated.findViewById(R.id.edt_videoUrl); builder.setView(viewInflated); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Toast.makeText(getContext(), "ОК", Toast.LENGTH_SHORT).show(); String string = input.getText().toString(); onUserInput(string); getYouTubeId("https://"); } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); Toast.makeText(getContext(), "Отмена", Toast.LENGTH_SHORT).show(); } }); builder.show(); } Then we call the onUserInput method, in which we start working with the string:
private void onUserInput(String input){ String placeholder = "http://img.youtube.com/vi/%s/0.jpg"; String url = input.format(placeholder, id); } The problem is that when you try to pass a variable to the getYouTubeId method, the url we declared in onUserInput is not used (not passed)
private String getYouTubeId (String url) { String pattern = "(?<=youtu.be/|watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(url); if(matcher.find()){ return matcher.group(); } else { return "error"; } } Please tell me how to correct the method.