A new series on Android : Quote App


<?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>
implementation 'com.android.volley:volley:1.2.0'
For more details checkout android volley library docs
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);
}

}
<uses-permission android:name="android.permission.INTERNET"/>
TextView textView;
Button sharebutton,loadButton;
String myText;

Note : if you get error after typing TextView or Button just hit Alt+Enter to import them
textView=findViewById(R.id.textView);
sharebutton=findViewById(R.id.Sharebutton);
loadButton=findViewById(R.id.loadbutton);
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);
}
}
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);
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store