Upload Url From User Input Android Studio Firebase
Firebase is one of the famous backend platforms which is used past and then many developers to provide backend support to their applications and websites. Information technology is the product of Google which provides services such as database, storage, user authentication, and many more. In this article, nosotros will create a uncomplicated app in which we will exist adding our Data to Firebase Realtime Database. Note that we are going to implement this project using theJava linguistic communication.
What is Firebase Realtime Database?
Firebase Realtime Database is a NoSQL deject database that is used to store and sync the data. The information from the database tin can be synced at a time across all the clients such as android, spider web too as IOS. The data in the database is stored in the JSON format and information technology updates in real-time with every connected customer.
What are the Advantages of using the Firebase Realtime Database?
- The main advantage of using the Firebase Realtime database is that the data is updated in a real-time manner and you don't accept to make any requests for the data updates or changes. The database uses data synchronization every time when data changes and these changes will reverberate the continued user within milliseconds.
- While using Firebase Realtime Database your apps remain responsive even if the device loses its connectivity to the database. Once the user has established the connectedness he will receive the changes fabricated in the information from the database.
- The data stored in the Firebase database tin exist easily accessible through the web portal of Firebase. Yous can manage your database from PC as well as mobile devices. You can manage the rules of the database which gives permissions to read and write operations to the database.
What We are Going to Build in This Article?
In this commodity, we are going to build a simple application in which nosotros will exist getting the information from the users with the help of some TextFields and store that information in the Firebase Realtime Database. Note that we are using Firebase Realtime Database and the app is written using Java linguistic communication.
Pace by Footstep Implementation
Stride one: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Notation that select Java as the programming language.
Step two: Connect your app to Firebase
Later creating a new projection navigate to the Tools selection on the tiptop bar. Inside that click on Firebase. Afterward clicking on Firebase, you can get to meet the correct column mentioned beneath in the screenshot.
Inside that cavalcade Navigate to Firebase Realtime Database. Click on that option and you volition get to see 2 options on Connect app to Firebase and Add Firebase Realtime Database to your app. Click on Connect now and your app will be connected to Firebase. After that click on the second choice and at present your App is connected to Firebase.
Later on connecting your app to Firebase y'all will get to see the beneath screen.
After that verify that dependency for Firebase Realtime database has been added to our Gradle file. Now navigate to the app > Gradle Scripts and within that file check whether the beneath dependency is added or non. If the below dependency is not added in your build.gradle file. Add the beneath dependency in the dependencies section.
implementation 'com.google.firebase:firebase-database:xix.6.0'
After calculation this dependency sync your project and at present we are ready for creating our app. If yous want to know more about connecting your app to Firebase. Refer to this article to become in detail about Calculation Firebase to Android App.
Footstep 3: Working with AndroidManifest.xml file
For adding data to Firebase we should have to requite permissions for accessing the net. For adding these permissions navigate to the app > AndroidManifest.xml and inside that file add together the beneath permissions to it.
XML
<
uses-permission
android:proper noun
=
"android.permission.INTERNET"
/>
<
uses-permission
android:name
=
"android.permission.ACCESS_NETWORK_STATE"
/>
Stride four: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. Beneath is the code for the activity_main.xml file.
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
tools:context
=
".MainActivity"
>
<
EditText
android:id
=
"@+id/idEdtEmployeeName"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_centerHorizontal
=
"true"
android:layout_margin
=
"10dp"
android:hint
=
"Enter Employee Name"
android:importantForAutofill
=
"no"
android:inputType
=
"textPersonName"
/>
<
EditText
android:id
=
"@+id/idEdtEmployeePhoneNumber"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_below
=
"@id/idEdtEmployeeName"
android:layout_margin
=
"10dp"
android:hint
=
"Enter employee phone number"
android:importantForAutofill
=
"no"
android:inputType
=
"phone"
/>
<
EditText
android:id
=
"@+id/idEdtEmployeeAddress"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_below
=
"@id/idEdtEmployeePhoneNumber"
android:layout_margin
=
"10dp"
android:hint
=
"Enter employee address"
android:inputType
=
"textPostalAddress"
/>
<
Button
android:id
=
"@+id/idBtnSendData"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_below
=
"@id/idEdtEmployeeAddress"
android:layout_margin
=
"10dp"
android:text
=
"Add employee details"
android:textAllCaps
=
"false"
/>
</
RelativeLayout
>
Step 5: Create a new Coffee grade for storing our data
For sending multiple information to the Firebase Realtime database nosotros have to create an Object form and send that whole object grade to Firebase. For creating an object form navigate to the app > java > your app's bundle name > Right-click on information technology and Click on New > Coffee Class > Requite a name to your form. In my case, it is EmployeeInfo, and add below code to it.
Java
public
form
EmployeeInfo {
private
String employeeName;
private
String employeeContactNumber;
private
String employeeAddress;
public
EmployeeInfo() {
}
public
String getEmployeeName() {
return
employeeName;
}
public
void
setEmployeeName(Cord employeeName) {
this
.employeeName = employeeName;
}
public
Cord getEmployeeContactNumber() {
return
employeeContactNumber;
}
public
void
setEmployeeContactNumber(String employeeContactNumber) {
this
.employeeContactNumber = employeeContactNumber;
}
public
String getEmployeeAddress() {
return
employeeAddress;
}
public
void
setEmployeeAddress(Cord employeeAddress) {
this
.employeeAddress = employeeAddress;
}
}
Footstep 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following lawmaking. Below is the code for the MainActivity.coffee file. Comments are added inside the code to understand the code in more item.
Java
import
android.bone.Parcel;
import
android.text.TextUtils;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
import
androidx.annotation.NonNull;
import
androidx.appcompat.app.AppCompatActivity;
import
com.google.firebase.database.DataSnapshot;
import
com.google.firebase.database.DatabaseError;
import
com.google.firebase.database.DatabaseReference;
import
com.google.firebase.database.FirebaseDatabase;
import
com.google.firebase.database.ValueEventListener;
public
form
MainActivity
extends
AppCompatActivity {
private
EditText employeeNameEdt, employeePhoneEdt, employeeAddressEdt;
private
Button sendDatabtn;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
EmployeeInfo employeeInfo;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employeeNameEdt = findViewById(R.id.idEdtEmployeeName);
employeePhoneEdt = findViewById(R.id.idEdtEmployeePhoneNumber);
employeeAddressEdt = findViewById(R.id.idEdtEmployeeAddress);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference(
"EmployeeInfo"
);
employeeInfo =
new
EmployeeInfo();
sendDatabtn = findViewById(R.id.idBtnSendData);
sendDatabtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
String name = employeeNameEdt.getText().toString();
Cord telephone = employeePhoneEdt.getText().toString();
String address = employeeAddressEdt.getText().toString();
if
(TextUtils.isEmpty(name) && TextUtils.isEmpty(phone) && TextUtils.isEmpty(accost)) {
Toast.makeText(MainActivity.
this
,
"Please add some data."
, Toast.LENGTH_SHORT).bear witness();
}
else
{
addDatatoFirebase(name, phone, address);
}
}
});
}
individual
void
addDatatoFirebase(Cord name, String telephone, String address) {
employeeInfo.setEmployeeName(name);
employeeInfo.setEmployeeContactNumber(phone);
employeeInfo.setEmployeeAddress(address);
databaseReference.addValueEventListener(
new
ValueEventListener() {
@Override
public
void
onDataChange(
@NonNull
DataSnapshot snapshot) {
databaseReference.setValue(employeeInfo);
Toast.makeText(MainActivity.
this
,
"data added"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onCancelled(
@NonNull
DatabaseError error) {
Toast.makeText(MainActivity.
this
,
"Neglect to add information "
+ error, Toast.LENGTH_SHORT).show();
}
});
}
}
After calculation this code go to this link for Firebase. Later on clicking on this link you volition get to see the beneath folio and on this page Click on Go to Console pick in the top right corner.
After clicking on this screen you volition go to see the below screen with your all project inside that select your projection.
Inside that screen click northward Realtime Database in the left window.
After clicking on this option you will become to see the screen on the right side. On this folio click on the Rules option which is present in the tiptop bar. You will get to come across the below screen.
Within this screen click on the Rules tab you volition go to see the above screen and change the rules to true equally shown in the screenshot. The rules are changed to true because nosotros are not providing authentication within our app and we have to write data to our database. That's why we are specifying it to true. Later on changing your rules click on the publish rules push button. Click on that choice and your rules volition be published. At present come back to the Data tab of your database.
Output:
Beneath is the video for our app for adding information to the Firebase Realtime Database.
Run the app and make sure to connect your device to the internet. After that add together some data in your text fields and click on the Insert data button. The information volition be added to our Firebase Database. Below is the screenshot we volition get to see afterwards calculation data to Firebase Database from the app.
heidenreichates1941.blogspot.com
Source: https://www.geeksforgeeks.org/how-to-save-data-to-the-firebase-realtime-database-in-android/
0 Response to "Upload Url From User Input Android Studio Firebase"
Post a Comment