Separate the clickable ad process

It is good practice to separate your player’s UI logic from the process that manages ad clicks.

One way to do this is to implement multiple Fragments for an Activity.
  1. Implement one fragment to contain the MediaPlayer (to be responsible for video playback).

    This Fragment should call notifyClick.

    public class PlayerFragment extends SherlockFragment {
    ....
    public void notifyAdClick () {
    	_mediaPlayer.getView().notifyClick();
    }
    ....
    }
    
  2. Implement a different fragment to display a UI element that indicates that an ad is clickable, monitor that UI element, and communicate user clicks to the fragment that contains the MediaPlayer.

    This Fragment should declare an interface for fragment communication. The Fragment captures the interface implementation during its onAttach lifecycle method and can then call the Interface methods to communicate with the Activity.

    public class PlayerClickableAdFragment extends SherlockFragment {
    private ViewGroup viewGroup;
    private Button button;
    OnAdUserInteraction callback;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
    savedInstanceState) {
    	// the custom fragment is defined by a custom button
    	viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_player_clickable_ad, container, false);
    	button = (Button) viewGroup.findViewById(R.id.clickButton);
    	// register a click listener to detect user interaction
    	button.setOnClickListener(new View.OnClickListener() {
    		@Override
    		public void onClick(View v) {
    			// send the event back to the activity
    			callback.onAdClick();
    		}
    	});
    	viewGroup.setVisibility(View.INVISIBLE);
    	return viewGroup;
    }
    public void hide() {
    	viewGroup.setVisibility(View.INVISIBLE);
    }
    public void show() {
    	viewGroup.setVisibility(View.VISIBLE);	
    }
    @Override
    public void onAttach(Activity activity) {
    	super.onAttach(activity);
    	// attaches the interface implementation
    	// if the container activity does not implement the methods from the interface an exception will be thrown
    	try {
    		callback = (OnAdUserInteraction) activity;
    	} catch (ClassCastException e) {
    		throw new ClassCastException(activity.toString()
    			+ " must implement OnAdUserInteraction");
    		}	
    	}
    	// user defined interface that allows fragment communication
    	// must be implemented by the container activity
    	public interface OnAdUserInteraction {
    	public void onAdClick();
    }
    }