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.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With