A new series on Android : Quote App

Nilay Paul
4 min readMay 12, 2021

Hello everyone I am starting a new android with Java series.

We will learn by building apps .

Let’s learn together. I will try to publish atleast one story every week so stay tuned.

What we will be building today?

Let’s kick start our journey with “Quotes app”.

I would recommend you to learn a bit about android layout .

Now let’s get started.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:background="#0C0C0C"
tools:context=".MainActivity">

<Button
android:id="@+id/loadbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:onClick="loadnext"
android:text="Load"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/Sharebutton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/Sharebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Share"
android:onClick="shareQuote"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/loadbutton" />

<TextView
android:id="@+id/textView"
android:layout_width="320dp"
android:layout_height="332dp"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:background="#FDFDFD"
android:textSize="25sp"
android:padding="5px"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

In this textview we will be displaying the quote from “https://api.quotable.io/random” this api i found online . You can use other apis too. Procedure will be same.

Run https://api.quotable.io/random in the browser to see the JSON response.

We will be using volley library inorder to handle http requests.

In you app level gradle add dependencies for volley

implementation 'com.android.volley:volley:1.2.0'
For more details checkout android volley library docs

We need to implement a Singleton class now.

So create a new java file name it MySingleton and add (Make sure you don’t delete existing package name at the top i.e package com.example.quoteapp; in my case)

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class MySingleton {
private static MySingleton instance;
private RequestQueue requestQueue;
private static Context ctx;

private MySingleton(Context context) {
ctx = context;
requestQueue = getRequestQueue();
}

public static synchronized MySingleton getInstance(Context context) {
if (instance == null) {
instance = new MySingleton(context);
}
return instance;
}

public RequestQueue getRequestQueue() {
if (requestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
}
return requestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}

}

In the AndroidManifest file add the following permission

<uses-permission android:name="android.permission.INTERNET"/>

Now we can start coding the java file

In MainActivity.java

first we need some member variables.

TextView textView;
Button sharebutton,loadButton;
String myText;

Note : if you get error after typing TextView or Button just hit Alt+Enter to import them

Now we need to assign them in our onCreate function

textView=findViewById(R.id.textView);
sharebutton=findViewById(R.id.Sharebutton);
loadButton=findViewById(R.id.loadbutton);

Let’s make a new method loadquote , loadnext , shareQuote that will load quotes for us and a function to share the quote .

public  void loadQuote() {
final String url="https://api.quotable.io/random";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try {

String data = response.getString("content");
myText=data;
textView.setText(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(MainActivity.this).addToRequestQueue(jsonObjectRequest);
}
public void loadnext(View view) {
loadQuote();
}

public void shareQuote(View view) {
if(myText!=null){
Intent intent =new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,myText);
Intent intentChoose = Intent.createChooser(intent,"Here is a Quote for you ");
startActivity(intentChoose);
}
}

Entire main activity

package com.example.quoteapp;// will be different for you

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

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

public class MainActivity extends AppCompatActivity {

TextView textView;
Button sharebutton,loadButton;
String myText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textView);
sharebutton=findViewById(R.id.Sharebutton);
loadButton=findViewById(R.id.loadbutton);

this.loadQuote();



}

public void loadQuote() {
final String url="https://api.quotable.io/random";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try {

String data = response.getString("content");
myText=data;
textView.setText(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(MainActivity.this).addToRequestQueue(jsonObjectRequest);
}

public void loadnext(View view) {
loadQuote();
}

public void shareQuote(View view) {
if(myText!=null){
Intent intent =new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,myText);
Intent intentChoose = Intent.createChooser(intent,"Here is a Quote for you ");
startActivity(intentChoose);
}
}
}

We are done with our app let’s run it and see if it works.

--

--