How to develop an Android App for your retail business (B2C) using Firebase Database?-source code

This video shows the steps to develop a simple Android App for small business and store owners using the free hosted Firebase database provided by google.

In this video the main focus is to show the steps to create database, connect it to your App and then read and write from your database seamlessly. This tutorial shows a very simple design of the layout and mainly focuses on showing the steps to connect to the database.

Part 2 of this video can be seen at: https://youtu.be/xfwqKX4-Dac

In the 2nd part it shows – how to implement search functionality in your App using Firebase Database in Android Studio?

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

package com.example.mystore;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

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 class MainActivity extends AppCompatActivity {

private EditText editTextProduct;
private EditText editTextPrice;

private DatabaseReference myRef;
private String ValueDatabase;
private String refinedData;
private ListView listView;

private SearchView searchView;
private TextView textViewSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myRef = FirebaseDatabase.getInstance().getReference(“MyStore1”);

editTextPrice = findViewById(R.id.editTextPrice);
editTextProduct = findViewById(R.id.editTextProduct);
listView =findViewById(R.id.listView);
searchView = findViewById(R.id.serachview);
textViewSearch = findViewById(R.id.textViewSearch);

myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ValueDatabase = dataSnapshot.getValue().toString();
refinedData = ValueDatabase.substring(1,ValueDatabase.length()-1);
String List[] = refinedData.split(“,”);
listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, List));

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
int SearchIndex = refinedData.indexOf(query);
String SearchResult = refinedData.substring(SearchIndex);
String SearchSplit[] = SearchResult.split(“,”);
textViewSearch.setText(SearchSplit[0]);

return false;
}

@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}

public void InsertButton(View view){
try {
myRef.child(editTextProduct.getText().toString()).setValue(editTextPrice.getText().toString());
}
catch (Exception e){
e.printStackTrace();
}
}
}

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.mystore”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res-auto&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<EditText
android:id=”@+id/editTextProduct”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”20dp”
android:layout_marginTop=”50dp”
android:autofillHints=””
android:ems=”10″
android:hint=”@string/product_name”
android:inputType=”textPersonName” />

<EditText
android:id=”@+id/editTextPrice”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”200dp”
android:layout_marginTop=”50dp”
android:autofillHints=””
android:ems=”10″
android:hint=”@string/price”
android:inputType=”textPersonName” />

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”150dp”
android:layout_marginTop=”20dp”
android:onClick=”InsertButton”
android:text=”@string/insert” />

<ListView
android:id=”@+id/listView”
android:layout_width=”300dp”
android:layout_height=”400dp”
android:layout_marginStart=”50dp”
android:layout_marginTop=”200dp” />

<SearchView
android:id=”@+id/serachview”
android:layout_width=”350dp”
android:layout_height=”20dp”
android:layout_marginStart=”20dp”
android:layout_marginTop=”100dp” />

<TextView
android:id=”@+id/textViewSearch”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”20dp”
android:layout_marginTop=”150dp”
android:text=”@string/search_result” />
</RelativeLayout>

Leave a Reply

%d bloggers like this: