Reputation: 1060
How do I change MediaController
icons programmatically? I mean play/pause/forward and rew.
You see I have added fullscreen button but can't realize how to change those three.
Custom MediaController class:
public class CustomVideoController extends MediaController {
public static boolean full = false;
private ImageButton fullScreenBtn;
Context mContext;
Activity activity;
VideoView mVideoView;
public CustomVideoController(Context context, Activity activity, VideoView videoView) {
super(new ContextThemeWrapper(context, R.style.VideoPlayerTheme));
this.activity = activity;
mContext = context;
mVideoView = videoView;
}
@Override
public void setAnchorView(View view) {
super.setAnchorView(view);
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
frameParams.gravity = Gravity.RIGHT | Gravity.TOP;
frameParams.setMargins(12,14,10,12);
View v = AddFullScreenBtn();
addView(v, frameParams);
}
@Override
public void hide() {
super.hide();
}
@Override
public void show(int timeout) {
super.show(0);
}
private View AddFullScreenBtn() {
fullScreenBtn = new ImageButton(mContext);
if (full)
fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_stretch);
else
fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_shrink);
fullScreenBtn.setBackgroundColor(Color.WHITE);
fullScreenBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (full){
fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_stretch);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
full = false;
} else {
fullScreenBtn.setImageResource(R.drawable.ic_media_fullscreen_shrink);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
full = true;
}
}
});
return fullScreenBtn;
}
Please help me to change drawables for those icons. Is it possible at all? I'm really confused with that one.
Upvotes: 1
Views: 2558
Reputation: 210
I have had the same problem recently and got a tricky way to change it. Firstly extend the MediaController class as your customer class, then override the setAnchorView() as below. Not pretty sure if it is safe enough.
public class MyMediaController extends MediaController {
public MyMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyMediaController(Context context, boolean useFastForward) {
super(context, useFastForward);
}
public MyMediaController(Context context) {
super(context);
}
@Override
public void setAnchorView(View view) {
super.setAnchorView(view);
//find the control buttons
for(int index = 0; index< getChildCount(); ++index) {
View nextChild = getChildAt(index);
if (nextChild instanceof LinearLayout)
{
LinearLayout linearLayout = (LinearLayout)nextChild;
for (int i = 0; i < linearLayout.getChildCount(); i++)
{
if (i == 0 && linearLayout.getChildAt(i) instanceof LinearLayout)
{
LinearLayout linearLayoutControlPanel = (LinearLayout)linearLayout.getChildAt(i);
int len = linearLayoutControlPanel.getChildCount();
if (len > 0) {
// index = 0, pre button, just hide it
View buttonPre = linearLayoutControlPanel.getChildAt(0);
if (buttonPre instanceof ImageButton) {
buttonPre.setVisibility(INVISIBLE);
}
// index = len - 1, next button, change icon to fullscreen
View buttonNex = linearLayoutControlPanel.getChildAt(len - 1);
if (buttonNex instanceof ImageButton) {
((ImageButton) buttonNex).setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_fullscreen));
}
}
}
}
}
}
}
}
Upvotes: 1
Reputation: 1069
If I understood you correctly, you're using android.widget.MediaControleller
. In this case there's no way to change the layout buttons. Here's why (code bellow is from MediaController.class):
/**
* Create the view that holds the widgets that control playback.
* Derived classes can override this to create their own.
* @return The controller view.
* @hide This doesn't work as advertised
*/
protected View makeControllerView() {
LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRoot = inflate.inflate(com.android.internal.R.layout.media_controller, null);
initControllerView(mRoot);
return mRoot;
}
As you can see, it's using private method to sutup and inflate its view.
Upvotes: 0