0

I am trying to dynamically add youtube videos to my application, and the problem is , that I cannot add anything below the YoutubePlayerFragment for some reason. I tried using a Linear layout(vertical), but the button was still above the fragment.

Here's the java code that has the addTemplate method which adds the template dynamically to the view.

package com.example.shreyass.yourbigday;

import android.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.LayoutDirection;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayerView;
import com.google.android.youtube.player.YouTubeThumbnailLoader;
import com.google.android.youtube.player.YouTubeThumbnailView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

YouTubePlayerFragment videoOne;
Button playVideoOne;
RelativeLayout rlOne;
YouTubeThumbnailView.OnInitializedListener onThumbnailInitializedListener;

YouTubePlayerFragment videoTwo;
Button playVideoTwo;
YouTubePlayer.OnInitializedListener mOnInitializedListener;
YouTubeThumbnailView youTubeThumbnailView;

ArrayList<String> videoIds;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    videoIds = new ArrayList<>();

    rlOne = (RelativeLayout) findViewById(R.id.rlOne);

    mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            youTubePlayer.loadVideo("rb8RlSVua00");
        }

        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        }
    };
    addTemplate(rlOne);
}

public void addTemplate(RelativeLayout relativeLayout){
    final YouTubePlayerFragment ytFragment = new YouTubePlayerFragment();
    Button playButton = new Button(this);
    Button playButton2 = new Button(this);
    VideoTemplate videoTemplate =  new VideoTemplate(ytFragment,playButton);

    FragmentManager fragMan = getFragmentManager();
    FragmentTransaction fragTransaction = fragMan.beginTransaction();

    fragTransaction.add(relativeLayout.getId(),videoTemplate.getYtFragment());
    fragTransaction.commit();



    relativeLayout.addView(playButton);
    playButton.setText("Play");
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) playButton.getLayoutParams();
    params.addRule(RelativeLayout.ALIGN_BOTTOM,videoTemplate.getYtFragment().getId());
    playButton.setLayoutParams(params);

    playButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ytFragment.initialize(youtubeConfig.getApiKey(), mOnInitializedListener);
        }
    });

}
}

addTemplate initializes the template(from a separate class whose code is below) and adds the video fragment and a play button to the layout dynamically. Right now, it's just getting called in OnCreate once for testing purposes.

The class that has the template definition for re-usage-

package com.example.shreyass.yourbigday;

import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.android.youtube.player.YouTubePlayerFragment;


public class VideoTemplate {
private YouTubePlayerFragment ytFragment;
private Button playButton;
private RelativeLayout previousLayout;


public YouTubePlayerFragment getYtFragment() {
    return ytFragment;
}
public void setYtFragment(YouTubePlayerFragment ytFragment) {
    this.ytFragment = ytFragment;
}
public VideoTemplate(YouTubePlayerFragment ytFragment, Button playButton) {
    this.ytFragment = ytFragment;
    this.playButton = playButton;
}

public Button getPlayButton() {
    return playButton;
}

public void setPlayButton(Button playButton) {
    this.playButton = playButton;
}
}

Right now, the button gets added on top of the PlayerFragment, instead of above or below it.

Ryuzaki
  • 25
  • 9
  • change relative layout to Linear layout with vertical orientation and for adding fragment use frameLayout inside Linear – Akash Dubey Oct 25 '18 at 13:25
  • @AkashDubey Already done that, says so in the question! In that case it added the button above the fragment, plus I have to use relative layout for other reasons as well. – Ryuzaki Oct 25 '18 at 13:26
  • Then u can add layoutParams by giving alignments to button, have a look https://stackoverflow.com/a/4639012/5502638 – Akash Dubey Oct 25 '18 at 13:27
  • @AkashDubey Again, that's exactly what I did in my code............that's the problem.... – Ryuzaki Oct 25 '18 at 13:33

0 Answers0