7

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.

user977816
  • 81
  • 1
  • 3
  • How have you added button in mediaController? can you share your code or some tips thanks – Dilip Sep 12 '12 at 08:31

2 Answers2

14

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);
}
Eva Lan
  • 166
  • 3
1

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.

Rigeborod
  • 356
  • 2
  • 6