Adding a button to MediaController

4.9k Views Asked by At

I want to add a button to MediaController. So I extended MediaController class, created a button and added it into the frame layout. But the newly added button is not reflecting while running.

Please find the code below

 public class VideoController extends MediaController {

private Button searchButton;
public VideoController(Context context) {
    super(context);

    searchButton = new Button(context);
    searchButton.setText("Search");
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.RIGHT;
    System.out.println("Adding a button");

    addView(searchButton, params);
    //updateViewLayout(this, params);
}

@Override
public void hide() {
}
}

what I am doing wrong here. Any suggestions will be helpful.

Thanks in advance.

2

There are 2 best solutions below

0
On

Actually that happens because media controller's view is constructed later (in makeControllerView method). So you need to override it and add button there.

Unfortunately, it is hidden at the moment. And overriding setAnchorView seems the best solution.

1
On

You have to override setAnchorView in your VideoController class:

 @Override 
 public void setAnchorView(View view) {
     super.setAnchorView(view);

     Button searchButton = new Button(context);
     searchButton.setText("Search");
     FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     params.gravity = Gravity.RIGHT;
     addView(searchButton, params);
}