How to Build Spotify Clone in Android?

package com.gtappdevelopers.spotify_java;

  

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.inputmethod.EditorInfo;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

  

import androidx.appcompat.app.AppCompatActivity;

import androidx.recyclerview.widget.RecyclerView;

  

import com.android.volley.AuthFailureError;

import com.android.volley.Request;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.StringRequest;

import com.android.volley.toolbox.Volley;

  

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

  

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map;

  

public class MainActivity extends AppCompatActivity {

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        

        initializeAlbumsRV();

        initializePopularAlbumsRV();

        initializeTrendingAlbumsRV();

        initializeSearchView();

    }

  

    

    private void initializeSearchView() {

        

        

        EditText searchEdt = findViewById(R.id.idEdtSearch);

        searchEdt.setOnEditorActionListener(new EditText.OnEditorActionListener() {

            @Override

            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    

                    searchTracks(searchEdt.getText().toString());

                    return true;

                }

                return false;

            }

        });

    }

  

    

    

    private void searchTracks(String searchQuery) {

        

        

        Intent i = new Intent(MainActivity.this, SearchActivity.class);

        i.putExtra("searchQuery", searchQuery);

        startActivity(i);

    }

  

    

    private String getToken() {

        

        SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);

        return sh.getString("token", "Not Found");

    }

  

    @Override

    protected void onStart() {

        super.onStart();

        

        

        generateToken();

    }

  

    private void generateToken() {

        

        

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        

        StringRequest request = new StringRequest(Request.Method.POST, url, new com.android.volley.Response.Listener<String>() {

            @Override

            public void onResponse(String response) {

                try {

                    JSONObject jsonObject = new JSONObject(response);

                    

                    

                    String tk = jsonObject.getString("access_token");

                    SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE);

                    SharedPreferences.Editor myEdit = sharedPreferences.edit();

                    myEdit.putString("token", "Bearer " + tk);

                    myEdit.apply();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

  

            }

        }, new com.android.volley.Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError error) {

                

                Toast.makeText(MainActivity.this, "Fail to get response = " + error, Toast.LENGTH_SHORT).show();

            }

        }) {

            @Override

            public Map<String, String> getHeaders() throws AuthFailureError {

                HashMap<String, String> headers = new HashMap<>();

                

                

                headers.put("Authorization", " Add your authorization here.");

                headers.put("Content-Type", "application/x-www-form-urlencoded");

                return headers;

            }

        };

        

        queue.add(request);

    }

  

  

    private void initializeAlbumsRV() {

        

        RecyclerView albumsRV = findViewById(R.id.idRVAlbums);

        

        

        ArrayList<AlbumRVModal> albumRVModalArrayList = new ArrayList<>();

        AlbumRVAdapter albumRVAdapter = new AlbumRVAdapter(albumRVModalArrayList, this);

        albumsRV.setAdapter(albumRVAdapter);

        

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        

        JsonObjectRequest albumObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject response) {

                try {

                    JSONArray albumArray = response.getJSONArray("albums");

                    for (int i = 0; i < albumArray.length(); i++) {

                        JSONObject albumObj = albumArray.getJSONObject(i);

                        String album_type = albumObj.getString("album_type");

                        String artistName = albumObj.getJSONArray("artists").getJSONObject(0).getString("name");

                        String external_ids = albumObj.getJSONObject("external_ids").getString("upc");

                        String external_urls = albumObj.getJSONObject("external_urls").getString("spotify");

                        String href = albumObj.getString("href");

                        String id = albumObj.getString("id");

                        String imgUrl = albumObj.getJSONArray("images").getJSONObject(1).getString("url");

                        String label = albumObj.getString("label");

                        String name = albumObj.getString("name");

                        int popularity = albumObj.getInt("popularity");

                        String release_date = albumObj.getString("release_date");

                        int total_tracks = albumObj.getInt("total_tracks");

                        String type = albumObj.getString("type");

                        

                        albumRVModalArrayList.add(new AlbumRVModal(album_type, artistName, external_ids, external_urls, href, id, imgUrl, label, name, popularity, release_date, total_tracks, type));

                    }

                    albumRVAdapter.notifyDataSetChanged();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError error) {

                Toast.makeText(MainActivity.this, "Fail to get data : " + error, Toast.LENGTH_SHORT).show();

            }

        }) {

            @Override

            public Map<String, String> getHeaders() throws AuthFailureError {

                

                HashMap<String, String> headers = new HashMap<>();

                headers.put("Authorization", getToken());

                headers.put("Accept", "application/json");

                headers.put("Content-Type", "application/json");

                return headers;

            }

        };

        

        queue.add(albumObjReq);

    }

  

    private void initializePopularAlbumsRV() {

        

        

        RecyclerView albumsRV = findViewById(R.id.idRVPopularAlbums);

        ArrayList<AlbumRVModal> albumRVModalArrayList = new ArrayList<>();

        AlbumRVAdapter albumRVAdapter = new AlbumRVAdapter(albumRVModalArrayList, this);

        albumsRV.setAdapter(albumRVAdapter);

        

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        

        JsonObjectRequest albumObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject response) {

                try {

                    JSONArray albumArray = response.getJSONArray("albums");

                    for (int i = 0; i < albumArray.length(); i++) {

                        JSONObject albumObj = albumArray.getJSONObject(i);

                        String album_type = albumObj.getString("album_type");

                        String artistName = albumObj.getJSONArray("artists").getJSONObject(0).getString("name");

                        String external_ids = albumObj.getJSONObject("external_ids").getString("upc");

                        String external_urls = albumObj.getJSONObject("external_urls").getString("spotify");

                        String href = albumObj.getString("href");

                        String id = albumObj.getString("id");

                        String imgUrl = albumObj.getJSONArray("images").getJSONObject(1).getString("url");

                        String label = albumObj.getString("label");

                        String name = albumObj.getString("name");

                        int popularity = albumObj.getInt("popularity");

                        String release_date = albumObj.getString("release_date");

                        int total_tracks = albumObj.getInt("total_tracks");

                        String type = albumObj.getString("type");

                        

                        albumRVModalArrayList.add(new AlbumRVModal(album_type, artistName, external_ids, external_urls, href, id, imgUrl, label, name, popularity, release_date, total_tracks, type));

                    }

                    albumRVAdapter.notifyDataSetChanged();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError error) {

                Toast.makeText(MainActivity.this, "Fail to get data : " + error, Toast.LENGTH_SHORT).show();

            }

        }) {

            @Override

            public Map<String, String> getHeaders() throws AuthFailureError {

                

                HashMap<String, String> headers = new HashMap<>();

                headers.put("Authorization", getToken());

                headers.put("Accept", "application/json");

                headers.put("Content-Type", "application/json");

                return headers;

            }

        };

        

        queue.add(albumObjReq);

    }

  

    private void initializeTrendingAlbumsRV() {

        

        

        RecyclerView albumsRV = findViewById(R.id.idRVTrendingAlbums);

        ArrayList<AlbumRVModal> albumRVModalArrayList = new ArrayList<>();

        AlbumRVAdapter albumRVAdapter = new AlbumRVAdapter(albumRVModalArrayList, this);

        albumsRV.setAdapter(albumRVAdapter);

        

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        

        JsonObjectRequest albumObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

            @Override

            public void onResponse(JSONObject response) {

                try {

                    JSONArray albumArray = response.getJSONArray("albums");

                    for (int i = 0; i < albumArray.length(); i++) {

                        JSONObject albumObj = albumArray.getJSONObject(i);

                        String album_type = albumObj.getString("album_type");

                        String artistName = albumObj.getJSONArray("artists").getJSONObject(0).getString("name");

                        String external_ids = albumObj.getJSONObject("external_ids").getString("upc");

                        String external_urls = albumObj.getJSONObject("external_urls").getString("spotify");

                        String href = albumObj.getString("href");

                        String id = albumObj.getString("id");

                        String imgUrl = albumObj.getJSONArray("images").getJSONObject(1).getString("url");

                        String label = albumObj.getString("label");

                        String name = albumObj.getString("name");

                        int popularity = albumObj.getInt("popularity");

                        String release_date = albumObj.getString("release_date");

                        int total_tracks = albumObj.getInt("total_tracks");

                        String type = albumObj.getString("type");

                        

                        albumRVModalArrayList.add(new AlbumRVModal(album_type, artistName, external_ids, external_urls, href, id, imgUrl, label, name, popularity, release_date, total_tracks, type));

                    }

                    albumRVAdapter.notifyDataSetChanged();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

            }

        }, new Response.ErrorListener() {

            @Override

            public void onErrorResponse(VolleyError error) {

                Toast.makeText(MainActivity.this, "Fail to get data : " + error, Toast.LENGTH_SHORT).show();

            }

        }) {

            @Override

            public Map<String, String> getHeaders() throws AuthFailureError {

                

                HashMap<String, String> headers = new HashMap<>();

                headers.put("Authorization", getToken());

                headers.put("Accept", "application/json");

                headers.put("Content-Type", "application/json");

                return headers;

            }

        };

        

        queue.add(albumObjReq);

    }

  

}

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: