Build a Blockchain Bitcoin Tracker CryptoCurrency Android App Using CoinLayer API in Java & XML Using Android Studio Full Project For Beginners

 

build.gradle

 

 

// Volley library
implementation 'com.loopj.android:android-async-http:1.4.9'

 

 

And also we need to add the internet permission to get data from the api

 

<!--Allow Internet Permission-->
<uses-permission android:name="android.permission.INTERNET" />

 

 

Change the style to NoActionBar in the themes.xml file: 

 

 

<style name=”AppTheme” parent=”Theme.AppCompat.NoActionBar”>

 

 

strings.xml

 

 

<resources>
    <string name="app_name">Am I Rich?</string>
    <string name="label_default_text">304.74</string>
    <string name="label_error_text">Error</string>
    <string name="base">Base Currency</string>
    <string name="imageview_desc">Bitcoin Logo</string>

    <string-array name="currency_array">
        <item>AUD</item>
        <item>BRL</item>
        <item>CAD</item>
        <item>CNY</item>
        <item>EUR</item>
        <item>GBP</item>
        <item>HKD</item>
        <item>JPY</item>
        <item>PLN</item>
        <item>RUB</item>
        <item>SEK</item>
        <item>USD</item>
        <item>ZAR</item>
    </string-array>
    
</resources>

 

 

XML Code

 

 

<?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:background="@color/bkgndColour"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.bitcointracker.MainActivity">

    <TextView
        android:id="@+id/priceLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/label_default_text"
        android:textColor="@color/fontColour"
        android:textSize="45sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/logoImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/imageview_desc"
        android:src="@drawable/bitcoin_image" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/priceLabel"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="@string/base"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="30sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/currency_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:dropDownSelector="@color/fontColour"
            android:gravity="center_horizontal"
            android:spinnerMode="dropdown" />
        
    </LinearLayout>

</RelativeLayout>

 

 

app> res > layout > spinner_dropdown_item.xml.

 

 

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/color_selector"
    android:ellipsize="marquee"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:singleLine="true"
    android:text="@string/label_error_text"
    android:textColor="@color/black"
    android:textSize="30sp" />

 

 

spinner_item.xml file:

 

 

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:padding="10dip"
    android:text="@string/label_error_text"
    android:textColor="@color/black"
    android:textSize="30sp"
    android:textStyle="bold" />

 

 

 

Java Code

 

 

MainActivity.java

 

 

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends AppCompatActivity {

    // Constants:
    // TODO: Create the base URL
    private final String BASE_URL = "http://api.coinlayer.com/live?access_key=";

    // Member Variables:
    TextView mPriceTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPriceTextView = (TextView) findViewById(R.id.priceLabel);
        Spinner spinner = (Spinner) findViewById(R.id.currency_spinner);

        // Create an ArrayAdapter using the String array and a spinner layout
        final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.currency_array, R.layout.spinner_item);

        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);

        // TODO: Set an OnItemSelected listener on the spinner
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String publicKey = "cd9ebbd0c5c20340b9d638e409f41fb1";
                String finalUrl = BASE_URL + publicKey + "&TARGET=" + adapterView.getItemAtPosition(i) + "&symbols=BTC";
                Log.d("Clima", "Request fail! Status code: " + finalUrl);
                try {
                    letsDoSomeNetworking(finalUrl);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }

    // TODO: complete the letsDoSomeNetworking() method
    private void letsDoSomeNetworking(String url) throws IOException, JSONException {
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(url, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                // called when response HTTP status is "200 OK"
                Log.d("Clima", "JSON: " + response.toString());
                try {
                    JSONObject price = response.getJSONObject("rates");
                    String object = price.getString("BTC");
                    mPriceTextView.setText(object);
                } catch (JSONException E) {
                    E.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject response) {
                // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                Log.d("Clima", "Request fail! Status code: " + statusCode);
                Log.d("Clima", "Fail response: " + response);
                Log.e("ERROR", e.toString());
            }
        });
    }
}

 

See also  How to Parse JSON From Node.js Express Domain Age Checker REST API in Android Application Using Volley Library in Java Full Tutorial For Beginners

 

Leave a Reply