Posts

Showing posts from January, 2020

Bottom App Bar for Android - A Material Design Component

Image
Similar to an App Bar (Top App Bar or Toolbar), Google introduced the Bottom App Bar as part of the Material Design Components back in 2018.
Let's get into the implementation by adding a dependency to the build.gradle file.
implementation "com.google.android.material:material:1.2.0-alpha03"
In the next step, let's add the BottomAppBar and the FloatingActionButton to the activity layout.
<com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomAppBar" style="@style/Widget.MaterialComponents.BottomAppBar.PrimarySurface" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:hideOnScroll="true" app:fabAnimationMode="slide" app:menu="@menu/menu_main" app:navigationIcon="@drawable/ic_menu_white" /> <com.google.android.material.floatingactionbutton.FloatingActionButt…

StringRequest Volley - Retrieve Response as String

Image
Volley is an Android HTTP networking library. In the previous article, we had a glimpse of the Volley library and performed a POST request using JsonObjectRequest.
In this article, we will fetch the response as string from https://reqres.in/api/users/2 and display it as the TextView text. For this purpose, we will be using StringRequest which makes a GET request to an endpoint and returns the response as a string.
// Build the request final StringRequest stringRequest = new StringRequest(URL_USER, new Response.Listener<String>() { @Override public void onResponse(@NonNull String response) { // Api call successful Log.d(TAG, "Response: " + response); // Show the response on the screen responseTextView.setText(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(@NonNull VolleyError error) { // Api call failed // Print the error to stacktrace error.printStackTrace(); // Show th…

Perform a Post Request Using Volley

Image
Volley is a very simple customizable HTTP client introduced by Google. Volley comes with request caching, prioritization and ordering. In addition to that, making concurrent network requests and canceling them is easier with Volley.

First, create a project and add volley dependency to the module level build.gradle file.

implementation 'com.android.volley:volley:1.1.1'
Create a login screen with username, password and a login button. User is able to enter their email address and password as shown in the figure below.


Build the login payload with entered username and password.
// Build the request payload final JSONObject jsonObject = new JSONObject(); try { jsonObject.put("email", email); jsonObject.put("password", password); } catch (JSONException e) { e.printStackTrace(); }
Lets us make an API call to https://reqres.in/api/login with the built request payload to log the user in.

// Build the request final JsonObjectRequest jsonObjectRequest = new J…