Adding a Button to MediaController

I want to add a button to MediaController. So I extended the MediaController class, created a button, and added it to the frame layout. But the newly added button is not displayed during operation.

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 am i doing wrong here. Any suggestions would be helpful.

Thanks in advance.

+6
source share
2 answers

You must 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); } 
+12
source

Actually, this happens because the media controller view was built later (in the makeControllerView method). Therefore, you need to override it and add a button there.

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

+1
source

Source: https://habr.com/ru/post/917355/


All Articles