How to Enable and Disable Buttons Using Firebase Remote Configuration in Android

Introduction

 
Firebase is a Mobile and Web Development cloud database. It can provide additional features for a developer to develop high-performance applications. Firebase Remote config allows you to change the appearance and behavior of your application without disturbing an update of your application. 
 
The Firebase RemoteConfig parameter values are:
  • ·getBoolean()
  • ·getByteArray()
  • ·getDouble()
  • ·getLong()
  • ·getString()
In this article, you will learn about how to configure the Firebase Remote Config in Android application by using Android Studio. 
 
Perquisites for Android Application Development
  1. Android Studio (Latest version)
  2. Google Chrome (for opening firebase console)
  3. Android device (for test our developed application)
We can do a step-by-step implementation:
 
Step 1
 
In my previous article, I explained how to develop an android app project in Android studio.
 
Click to read my previous article “How to Create an Android App Project in Android Studio”.
 
Step 2
 
After Android project syncing is completed, open the Android Manifest File in app-> manifests->AndroidManifest.xml and add permission for Android application to access the internet for when remote config needs internet connection access for fetching parameter values from firebase.
  1. <uses-permission android:name="android.permission.INTERNET"/>  
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 3
 
Next, add a Firebase connection to your Android Application. Click the Tool option in Menubar and then select the Firebase option. The Firebase assistance will be opened on the right side of the Android studio.
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 4
 
Click the Remote config in Firebase assistance pane, click “setup Firebase Remote Config”. The new window will be opened inside of assistance.
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 5
 
Next, the New Firebase connection window has opened. Then, select Create a new Firebase project radio button. After selecting the project location as “India”, click the “Connect to Firebase Button”. Android studio can start a connection with the Firebase database.
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 6
 
After you click the “Add Remote Config to your App” Button in Firebase assistance, Android studio will prompt a window to Accept Changes in Gradle. Click on the Accept Changes Button.
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 7
 
Automatically Remote Config dependencies are added in Gradle Files, after completing the Gradle syncing you may open an “activitymain.Xml” file for adding 2 buttons to the layout file. The first button displays the actions of parameter & the second button is for fetching parameter values from Firebase.
 
Copy and paste tge below XML code in an activitymail.XML file.
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"    
  4.     xmlns:tools="http://schemas.android.com/tools"    
  5.     android:layout_width="match_parent"    
  6.     android:layout_height="match_parent"    
  7.     tools:context=".MainActivity">    
  8.     <Button    
  9.         android:id="@+id/butnactivate"    
  10.         android:layout_width="match_parent"    
  11.         android:layout_height="wrap_content"    
  12.         android:textStyle="bold"    
  13.         android:text="My Account"/>    
  14.     
  15.     <Button    
  16.         android:id="@+id/butnfetcher"    
  17.         android:layout_width="match_parent"    
  18.         android:layout_height="wrap_content"    
  19.         android:layout_marginStart="8dp"    
  20.         android:layout_marginTop="8dp"    
  21.         android:layout_marginEnd="8dp"    
  22.         android:layout_marginBottom="8dp"    
  23.         android:text="Fetch"    
  24.         android:textStyle="bold"    
  25.         app:layout_constraintBottom_toBottomOf="parent"    
  26.         app:layout_constraintEnd_toEndOf="parent"    
  27.         app:layout_constraintStart_toStartOf="parent"    
  28.         app:layout_constraintTop_toTopOf="parent" />    
  29.     
  30. </android.support.constraint.ConstraintLayout>    
Step 8
 
Next, we are write Hava code for fetching parameters from Firebase.Open the “MainActivity.java” file and copy and paste the below Java code in the MainActivity.Java file
  1. package com.paramappdevelopments.salem.firebaseremoteconfig;    
  2. import android.support.annotation.NonNull;    
  3. import android.support.v7.app.AppCompatActivity;    
  4. import android.os.Bundle;    
  5. import android.view.View;    
  6. import android.widget.Button;    
  7. import android.widget.Toast;    
  8. import com.google.android.gms.tasks.OnCompleteListener;    
  9. import com.google.android.gms.tasks.Task;    
  10. import com.google.firebase.remoteconfig.FirebaseRemoteConfig;    
  11. import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;    
  12. import java.util.HashMap;    
  13. import java.util.Map;    
  14.     
  15. public class MainActivity extends AppCompatActivity {    
  16.     Button btnact,fetcher;    
  17.     FirebaseRemoteConfig myconfiguration;    
  18. @Override    
  19.     protected void onCreate(Bundle savedInstanceState) {    
  20.         super.onCreate(savedInstanceState);    
  21.         setContentView(R.layout.activity_main);    
  22.         btnact=(Button)findViewById(R.id.butnactivate);    
  23.         fetcher=(Button)findViewById(R.id.butnfetcher);    
  24.         myconfiguration=FirebaseRemoteConfig.getInstance();    
  25.         FirebaseRemoteConfigSettings confiuratonsettings = new FirebaseRemoteConfigSettings.Builder().build();    
  26.         myconfiguration.setConfigSettings(confiuratonsettings);    
  27.         Map<String,Object> defaultvalues = new HashMap<>();    
  28.         defaultvalues.put("btn_enable",false);    
  29.         myconfiguration.setDefaults(defaultvalues);    
  30.         fetcher.setOnClickListener(new View.OnClickListener() {    
  31.             @Override    
  32.             public void onClick(View v) {    
  33.                 myconfiguration.fetch(0).addOnCompleteListener(new OnCompleteListener<Void>() {    
  34.                     @Override    
  35.                     public void onComplete(@NonNull Task<Void> task) {    
  36.                         if(task.isSuccessful()){    
  37.                             myconfiguration.activateFetched();    
  38.                             btnact.setEnabled(myconfiguration.getBoolean("btn_enable"));    
  39.     
  40.  }    
  41.  else    
  42.                         {    
  43.                             Toast.makeText(MainActivity.this"Something went Wrong\nPlease try again", Toast.LENGTH_SHORT).show();    
  44.                         }    
  45.                     }    
  46.                 });    
  47.     
  48.             }    
  49.         });    
  50.     
  51.     }    
  52. } 
Step 9
 
We have successfully written the java code. Next, open the Google Chrome browser and log in to the firebase console and select the project of Remote Config Application.
 
Scroll down the firebase left pane and then click “Remote Config”.
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 10
 
Click the “Add Your First Parameter” button and enter the values in both “Parameter Name” and “Default value”.
 
Next, click the “Add Parameter ” button to save the values. However, the parameter is not published. The parameter name is the same at both the Application and Firebase.
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 11
 
Click the “Publish Button”. Your Parameter value is now live on your application.
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
Step 12
 
Run Your Application in a virtual or physical Android device.
 
Output 
 
btn_enable parameter value: false  in Firebase (Below image, Application button, set disabled)
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 
btn_enable parameter value: True in Firebase (Below image, Application button, set enabled)
 
How To Enable And Disable Button Using Firebase Remote Configuration In Android
How To Enable And Disable Button Using Firebase Remote Configuration In Android
 

Summary

 
We have now successfully enabled and disabled our Android application button using the Firebase remote config.