How to show Song List in your Android Application

0
111
views

Content :-

MAIN ACTIVITY

 

 

public class MainActivity extends AppCompatActivity {

    private ArrayList<Song> songList;

    private ListView songView;

    MediaPlayer mp;

    Uri musicUri;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        songView = (ListView)findViewById(R.id.song_list);

        songList = new ArrayList<Song>();

        getSongList();

        Collections.sort(songList, new Comparator<Song>(){

            public int compare(Song a, Song b){

                return a.getTitle().compareTo(b.getTitle());

            }

        });

        SongAdapter songAdt = new SongAdapter(this, songList);

        songView.setAdapter(songAdt);

        ListView lv1 = (ListView) findViewById(R.id.song_list);

        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener()
 {

            public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {

                mp = MediaPlayer.create(MainActivity.this ,musicUri );

                TextView tv1 = (TextView) view;

                String s1 = tv1.getText().toString();

                Toast.makeText(MainActivity.this, s1,
                 Toast.LENGTH_SHORT).show();

            }

        });



        ImageView iv1 = (ImageView) findViewById(R.id.imageView);

        iv1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                MainActivity.this.finish();

            }

        });

        ImageView iv2 = (ImageView) findViewById(R.id.imageView2);

        iv2.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                mp.pause();

            }

        });

        ImageView iv3 = (ImageView) findViewById(R.id.imageView3);

        iv3.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

             }

        });
    }

    public void getSongList() {

        //retrieve song info

        ContentResolver musicResolver = getContentResolver();

        musicUri = android.provider.MediaStore.Audio.Media.
                   EXTERNAL_CONTENT_URI;

        Cursor musicCursor = musicResolver.query(musicUri, null, 
                             null, null, null);

        if(musicCursor!=null && musicCursor.moveToFirst()){

            //get columns

            int titleColumn = musicCursor.getColumnIndex

                    (android.provider.MediaStore.Audio.Media.TITLE);

            int idColumn = musicCursor.getColumnIndex

                    (android.provider.MediaStore.Audio.Media._ID);

            int artistColumn = musicCursor.getColumnIndex

                    (android.provider.MediaStore.Audio.Media.ARTIST);

            //add songs to list

            do {

                long thisId = musicCursor.getLong(idColumn);

                String thisTitle = musicCursor.getString(titleColumn);

                String thisArtist = musicCursor.getString(artistColumn);

                songList.add(new Song(thisId, thisTitle, thisArtist));

            }

            while (musicCursor.moveToNext());

        }  }

}

SONG

This Activity contains Getter and Setter Method , which is used to get and set the details of the Song form internal Storage to our app.
public class Song {

    private long id;

    private String title;

    private String artist;

   public Song(long songID, String songTitle, String songArtist) {

            id=songID;

            title=songTitle;

            artist=songArtist;

        }

 public long getId() {

        return id;

    }

  public String getTitle() {

        return title;

    }

public String getArtist() {

        return artist;

    }}

SONG ADAPTER
This is a custom Adapter which is used to set the Details of the song to the Custom list which we have made
public class SongAdapter extends BaseAdapter{

    private ArrayList<Song> songs;

    private LayoutInflater songInf;

 public SongAdapter(Context c, ArrayList<Song> theSongs){

        songs=theSongs;

        songInf=LayoutInflater.from(c);

    }

 @Override

    public int getCount() {

        return songs.size();

    }

   @Override

    public Object getItem(int position) {

        return null;

    }

   @Override

    public long getItemId(int position) {

        return 0;

    }

   @Override

    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout songLay = (LinearLayout)songInf.inflate

                (R.layout.song, parent, false);

        //get title and artist views

        TextView songView = (TextView)songLay.findViewById(R.id.song_title);

        TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);

        //get song using position

        Song currSong = songs.get(position);

        //get title and artist strings

        songView.setText(currSong.getTitle());

        artistView.setText(currSong.getArtist());

        //set position as tag

        songLay.setTag(position);

        return songLay;

    }}

ANDROID MANIFEST
Add a uses permission in android manifest  , before Application  TAG
<uses-permission android:name="android.permission.WAKE_LOCK" />

    <application>

Activity_main

This is the layout file

 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context="com.example.acer.atios.MainActivity">

 <ListView

        android:id="@+id/song_list"

        android:layout_width="fill_parent"

        android:layout_height="400dp" >

    </ListView>
 <ImageView

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:id="@+id/imageView"

        android:src="@android:drawable/ic_menu_close_clear_cancel"

        android:layout_below="@+id/song_list"

        android:layout_alignParentEnd="true" />

   <ImageView

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:id="@+id/imageView2"

        android:src="@android:drawable/ic_media_pause"

        android:layout_alignTop="@+id/imageView"

        android:layout_centerHorizontal="true" />

  <ImageView

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:id="@+id/imageView3"

        android:src="@android:drawable/ic_menu_preferences"

        android:layout_alignTop="@+id/imageView2"

        android:layout_alignParentStart="true" />

</RelativeLayout>

SONG
This is the layout for custom adapter which show the songs list.

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

  <TextView

        android:id="@+id/song_title"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textColor="#000000"

        android:textSize="20sp"

        android:textStyle="bold" />

  <TextView

        android:id="@+id/song_artist"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textColor="#000000"

        android:textSize="18sp" />

</LinearLayout>

SCREEN SHOT

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here