1

I need to add a textview in MediaController which shows the name of the song playing. Currently , I am overriding setAnchorView method of MediaController class . But I dont know how should I get the song name populated there . I have the value of song name playing in the Activity class. So how should I approach ? This is my current approach. It should show song name even when pressing back and going back to the app since the music keeps playing.

MediaControllerClass;

    @Override 
  public void setAnchorView(View view) {

     String text = getSongTitle();
     if (text!=null)
     {
         Log.d(this.getClass().getName(), text);
     TextView tvSongTitle = new TextView(getContext());
      tvSongTitle.setText(text);
      tvSongTitle.setTextSize(40.0f);
      FrameLayout.LayoutParams params = new        FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      params.gravity = Gravity.TOP;
      addView(tvSongTitle, params);
     }
     super.setAnchorView(view);
 }

  public String getSongTitle()
  {

      main = new MainActivity();
      return main.getSongTitle();
  }

Inside Activity Class:

  public String getSongTitle()
{
    Log.d(this.getClass().getName(), "getsongtitle");

    Intent i = getIntent();
    if (i!=null)
    {
    String songTitle = i.getStringExtra("songName");
    if(songTitle!=null)
    {
        Log.d(this.getClass().getName(),songTitle );
        return songTitle;
    }
    else
    {
return null;
    }
    }
    else
    {
        return null;
    }

}

Service Class:

    Intent i = new Intent(MusicService.this,MainActivity.class);
i.putExtra("songName",songTitle);
startActivity(i);
Siju
  • 2,585
  • 4
  • 29
  • 53

1 Answers1

1

Add the TextView after setting the AnchorView . setAnchorView will remove all the previous views added.

Best way is to create a custom TextView layout and add the layout when setting the AnchorView and then change the TextView text when song selected

Custom layout with only TextView

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/songTitleView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

Then , Override the setAnchorView in your custom controller class

@Override
public void setAnchorView(View view) {
    super.setAnchorView(view);
    View customView = View.inflate(getContext(),R.layout.song_title_view, null);
    TextView tvSongTitle = (TextView) customView.findViewById(R.id.songTitleView);
    tvSongTitle.setText("set your song title");
    addView(customView);
}

Add a method to change the title in your custom controller

public void setSongTitle(String name){
    TextView tvSongTitle = (TextView) findViewById(R.id.songTitleView);
    tvSongTitle.setText(name);
}

Call the method when user select a song

controller.setSongTitle("song name");
Libin
  • 16,967
  • 7
  • 61
  • 83
  • Sorry haven't tried it yet since I was stuck with another issue .Will try soon and let you know. Thanks :) – Siju May 07 '14 at 15:00