Search code examples
androidandroid-mediaplayerandroid-music-player

How to list music files in the raw folder


I am trying to create a simple Mediaplayer application. It works actually, but I want to do is to show mp3 files on a Textview. I got the list like this way below (I think so). How can I set these filenames to a Textview

List<String>ListOfMusic=new ArrayList<String>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Field[]fields=R.raw.class.getFields();
    for (int i = 0; i < fields.length; i++) {
        ListOfMusic.add(fields[i].getName());               
    }
    initComp();
    textShown.setText(ListOfMusic[0]);

Solution

  • Loop through the ListOfMusic and add the items to the TextView (preceding every song name with the "newline" character if you want do display the song names on separate lines).

    Something like this:

    String songs="";
    for(String songName: ListOfMusic){
        songs+=songName+"\n";
    }
    textShown.setText(songs);