Change text color in android mediacontroller

Hi, there is a way to change the color of the text in the media center showing the total time and the remaining time of the audio file. In Android 2.3, the time is clearly visible, but when my application runs on Android 4.0 or 4.1, the text showing the times when both sides of the progress bar are too dark. I am currently extending the mediacontroller class to create my own media controller that does not disappear, maybe something that I could add to this class? Any help is appreciated.

public class WillMediaController extends MediaController { public WillMediaController(Context context) { super(context); } @Override public void hide() { // Do Nothing to show the controller all times } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { ((Activity) getContext()).finish(); }else{ super.dispatchKeyEvent(event); } if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) { // don't show the controls for volume adjustment return super.dispatchKeyEvent(event); } return true; } } 

thanks

+4
source share
2 answers

I had the same problem. A good way is to change the color using a theme. Use the ContextThemeWrapper app to apply the theme to your MediaController, for example:

 private final class AudioMediaController extends MediaController { private AudioMediaController(Context context) { super(new ContextThemeWrapper(context, R.style.Theme_MusicPlayer)); } } 

Your topic may look like this:

 <resources> <style name="Theme_MusicPlayer"> <item name="android:textColor">#FFFFFF</item> </style> </resources> 

save it as an XML file in the res / values ​​folder. The color of each text in the media controller (current time and end time) is now white.

+13
source

This is actually complicated. If you look at the source code of the MediaController , you will notice that the views it uses have an internal identifier (for example, com.android.internal.R.id.time ) that you cannot easily access.

Although, you can try to use reflection to get instances of views, and then change their attributes. For example, you can try to get a link to the mEndTime field, and then change its text color. eg:

 try { Field currentTime = getClass().getDeclaredField("mCurrentTime"); currentTime.setAccessible(true); TextView currentTimeTextView = (TextView) currentTime.get(this); currentTimeTextView.setTextColor(Color.RED); } catch (Exception pokemon) { } 
+4
source

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


All Articles