Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a button to MediaController

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.

like image 936
user977816 Avatar asked Jun 04 '12 10:06

user977816


2 Answers

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);
}
like image 196
Eva Lan Avatar answered Sep 27 '22 20:09

Eva Lan


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.

like image 33
Rigeborod Avatar answered Sep 27 '22 20:09

Rigeborod