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
- Android Studio (Latest version)
- Google Chrome (for opening firebase console)
- 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.
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.
- <uses-permission android:name="android.permission.INTERNET"/>
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.
Step 4
Click the Remote config in Firebase assistance pane, click “setup Firebase Remote Config”. The new window will be opened inside of assistance.
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.
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.
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.
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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"
- tools:context=".MainActivity">
- <Button
- android:id="@+id/butnactivate"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textStyle="bold"
- android:text="My Account"/>
-
- <Button
- android:id="@+id/butnfetcher"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginBottom="8dp"
- android:text="Fetch"
- android:textStyle="bold"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- </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
- package com.paramappdevelopments.salem.firebaseremoteconfig;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
- import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
- import java.util.HashMap;
- import java.util.Map;
-
- public class MainActivity extends AppCompatActivity {
- Button btnact,fetcher;
- FirebaseRemoteConfig myconfiguration;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnact=(Button)findViewById(R.id.butnactivate);
- fetcher=(Button)findViewById(R.id.butnfetcher);
- myconfiguration=FirebaseRemoteConfig.getInstance();
- FirebaseRemoteConfigSettings confiuratonsettings = new FirebaseRemoteConfigSettings.Builder().build();
- myconfiguration.setConfigSettings(confiuratonsettings);
- Map<String,Object> defaultvalues = new HashMap<>();
- defaultvalues.put("btn_enable",false);
- myconfiguration.setDefaults(defaultvalues);
- fetcher.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- myconfiguration.fetch(0).addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(@NonNull Task<Void> task) {
- if(task.isSuccessful()){
- myconfiguration.activateFetched();
- btnact.setEnabled(myconfiguration.getBoolean("btn_enable"));
-
- }
- else
- {
- Toast.makeText(MainActivity.this, "Something went Wrong\nPlease try again", Toast.LENGTH_SHORT).show();
- }
- }
- });
-
- }
- });
-
- }
- }
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”.
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.
Step 11
Click the “Publish Button”. Your Parameter value is now live on your application.
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)
btn_enable parameter value: True in Firebase (Below image, Application button, set enabled)
Summary
We have now successfully enabled and disabled our Android application button using the Firebase remote config.