Question

I'm integrating the example here into my Android app to include a WebView vimeo player:

https://github.com/droid28/VimeoVideo

I'm running into an error when I try to setContentView. When I setContentView, it disables the buttons I have in my activity (okay and delete). I tried to replace setContentView with addView, specifying the view to add, but then I get the error "The specified child already has a parent. You must call removeView() on the child's parent first"

How can I use setContentView while still having my buttons work?

Media Preview Activity

public class MediaPreview extends Main implements YouTubePlayer.OnInitializedListener {
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_preview);
        okay = (ImageButton) findViewById(R.id.okay_imagePreview);
        delete = (ImageButton) findViewById(R.id.delete_imagePreview);

        okay.setOnClickListener(new OnClickListener() {
            ....
        });

        delete.setOnClickListener(new OnClickListener() {
            ....
        });

        setUpPreview();

    }

    private void setUpPreview(){
        if(mediaURL.toLowerCase().contains("vimeo")){
               Log.d(TAG, "adding vimeo");
               isVimeoVideo = true;
               vimeoPlayer = new HTML5WebView(this);
               vimeoPlayer.loadUrl(mediaURL);    
               setContentView(vimeoPlayer.getLayout());
        }
     }

HTML5WebView

public class HTML5WebView extends WebView {
 private void init(Context context) {
 mContext = context;     
        Activity a = (Activity) mContext;

        mLayout = new FrameLayout(context);

        mBrowserFrameLayout = (RelativeLayout) LayoutInflater.from(a).inflate(R.layout.activity_media_preview, null);
        mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
        mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.media_holder);

        mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);

        // Configure the webview
        WebSettings s = getSettings();
        s.setLoadWithOverviewMode(true);
        s.setSaveFormData(true);
        s.setJavaScriptEnabled(true);
        mWebChromeClient = new MyWebChromeClient();
        setWebChromeClient(mWebChromeClient);

        setWebViewClient(new WebViewClient());


        mContentView.addView(this);
    }

}

activity_media_preview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black"
    tools:context=".MediaPreview" >

    <FrameLayout
        android:id="@+id/media_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_button_image_preview"
        android:layout_alignParentTop="true"
        android:background="@color/black"
        android:gravity="center" >
        <FrameLayout 
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            ></FrameLayout>
    </FrameLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/bottom_button_image_preview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:background="@color/white" >
        <ImageButton
            android:id="@+id/delete_imagePreview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:adjustViewBounds="true"
            android:maxHeight="60dp"
            android:maxWidth="60dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_action_discard"
            android:background="@drawable/custom_button_blue"

            android:contentDescription="@string/delete_imagePreview"

            android:layout_marginRight="1dp"            

             >
        </ImageButton>

        <ImageButton
            android:id="@+id/okay_imagePreview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:adjustViewBounds="true"
            android:maxHeight="60dp"
            android:maxWidth="60dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_action_accept_white"
            android:background="@drawable/custom_button_blue"

            android:contentDescription="@string/okay_imagePreview"


            >
        </ImageButton>

    </LinearLayout>

</RelativeLayout>
Was it helpful?

Solution 2

This is how I ended up fixing it. It's a bit hacky because in the end, it creates two sets of image buttons, but I hide the ones that are created from the HTML5WebView.

MediaPreview.java

public class MediaPreview extends Main implements YouTubePlayer.OnInitializedListener {

  FrameLayout mainContentView;

  protected void onCreate(Bundle savedInstanceState) {
      mainContentView = (FrameLayout) findViewById(R.id.main_content);
      setUpPreview();
       ...
   }

   private void setUpPreview(){
       if(mediaURL.toLowerCase().contains("vimeo")){
             Log.d(TAG, "adding vimeo");
             isVimeoVideo = true;
             vimeoPlayer = new HTML5WebView(this);
             vimeoPlayer.loadUrl(mediaURL);                       
             mainContentView.addView(vimeoPlayer.getLayout());                         
  }
}

HTML5WebView.java

public class HTML5WebView extends WebView {
    private void init(Context context) {
        mContext = context;     
        Activity a = (Activity) mContext;

        mLayout = new FrameLayout(context);

        mBrowserFrameLayout = (RelativeLayout) LayoutInflater.from(a).inflate(R.layout.activity_media_preview, null);
        mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
        mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.media_holder);
        okayButton = (ImageButton) mBrowserFrameLayout.findViewById(R.id.okay_imagePreview);
        deleteButton = (ImageButton) mBrowserFrameLayout.findViewById(R.id.delete_imagePreview);
        okayButton.setVisibility(View.GONE);
        deleteButton.setVisibility(View.GONE);

        mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);

        // Configure the webview
        WebSettings s = getSettings();
        s.setLoadWithOverviewMode(true);
        s.setSaveFormData(true);
        s.setJavaScriptEnabled(true);
        mWebChromeClient = new MyWebChromeClient();
        setWebChromeClient(mWebChromeClient);

        setWebViewClient(new WebViewClient());
        mContentView.addView(this);

    }

    public FrameLayout getLayout() {
        return mLayout;
    }
}

OTHER TIPS

When you use setContentView you are telling the activity to display the supplied view. So it can only show 1 view this way. But you can layer multiple views within 1 view, now that is what you should do. So you should make a container view like a FrameLayout and then add your vimeo view to that container.

In your case you have to do something like following:

setContentView(R.layout.activity_media_preview);
FrameLayout frame = (FrameLayout) this.findViewById(R.id.main_content);
HTML5WebView vimeoView = HTML5WebView.init(this);
frame.addView(vimeoView);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top