Friday, May 25, 2018

Android Create RatingBar Review Like Google Play Store + Firebase

RatingBar review can make our product or apps become more better then before. From the reviews that given by users we able to know what problem of our product or apps.

This article will show you how to create ratingbar review that looks like rating on Google play store. As you know the user interface of review on Google Play store is great and full color.

To achieve rating look like Google Play Store, you have to know the formula of count rating view (especially for create rating that shown with color)

For create the icon of rating (icon star), you can use several library, but in this article I use library Zhanghai Material RatingBar.



Oh, In this article I also use Firebase Firestore as database to store the data of product and the reviews of product.

Create project in Firebase 


Open Firebase Console and create new project. You also can use the existing project in Firebase. After that download the google-services.json then place it on directory app in your project.

Here I created data of product in Firebase Firestore manually :

The sample product the created manually


The data above will displayed on mobile android, and then from android we will send the review of this product.

Create Android Studio Project


After the sample data in Firebase has created, now we crate the android application to display the sample data and send review that data.

Gradle Configuration


In this project, I use some libraries that support the application, you can see at the following gradle :

build.gradle (Project:RatingGooglePlayStore)

build.gradle (Module:app)

Create Page Product


The first page that will appears is page Product (MainActivity). In this page, the data that created manually in Firebase will displayed.

First, we create the model (object) that will handle the data from Firebase Firestore, here it is

ProductModel.java

After that, create Adapter that handle the list of product (even the sample product that create is one, but we still create adapter to handle much data)

item_product.xml

ProductAdapter.java

Now get the data of product from Firebase Firestore and then display it on Recyclerview by using the following code :

MainActivity.java

If you follow all the steps above, you will get result look like the following image :

The data that I created manually in firebase has displayed on mobile

Create Page Review


After the product has displayed, now we create page review that display the rating, insert review and displaying review.

ReviewModel.java

This class use to handle data of review data got from Firebase Firestore, here is the code

Now create the adapter of list review :

item_review.xml

ReviewAdapter.java

After the adapter created, then create the logic how to show rating by stars and rating by color, take a look at the following images :

Rating using stars and colors 


To achieve the goal like the image above, there are several process that you have to do :

1. Insert Review To Firestore

The code above will store the data to collection review. Take a look at the following image :

Data review has stored into Firebase Firestore


Recommended : How to insert and display file image from Firebase Firestore

After data review stored successfully, the update the rating in product

2. Update Rating Product

The following code will update the rating in data product, after that will change the display of rating view and rating color.


3. Update the rating color


4. Populating the recyclerview with data of reviews

Take a learn of those codes above, that will guide you to understand the formula of displaying rating by stars and by colors.

Here is the full code of ReviewActivity.java :

Don't forget to add permission of INTERNET in your AndroidManifest.xml

Okay, that's all about the tutorial how to create rating or review look like Google Play Store. I hope this article will help you. Thank you.

Download the project : LINK GITHUB

Wednesday, May 16, 2018

Android Create, Read, Update And Delete Data in Firestore Firebase

The concept of data storage between Firebase and MySQL is different. If MySQL uses query concept (primary key, foreign key, etc), then Firebase not like that. Firebase stores data by Collection and Document.

We can say that collection and document are path of data that has stored in Firebase Firestore. We can name the collection by our selves (write the name whatever you want). Collection define as object or list of our data. Document can name by generate automatically from Firebase OR we also can name it by our selves.

Recommended : How to insert file image into Firebase Storage

This article will show you how to do CRUD (Create, Read, Update and Delete) data in Firebase Firestore.

Create Model Of Object


Before we start to do CRUD, let create one object called WebsiteModel.java to store the fields of Object


After the object was created, next we the function of Create, Read, Update, Remove data into/from Firestore

Insert Data


By using the following code, you can insert data into Firebase Store

When insert data, we use method add() after define its collection and its document. The result of the code above is :

Data has stored into Firestore


Take a look at the image above, Why the field of  idDocument is null?

This is my reason why the field of IdDocument is null :
  • I create field IdDocument of object WebsiteModel for store the Document ID when the data has stored in Firestore
  • So why null? Because when store data to Firestore, we don't know the Document ID that generated by Firebase automatically, we able to know the value of the Document ID when we retrieve the data.
  • Just continue read this article (I guarantee you will understand what I mean)

Display Data


After data was stored, now we have to create new function/method that able to read the data from Firestore, here is the method :

And the result when we successfully displaying the data is :

Result of retrieving the data

As you can see at image above, the idDocument has a value : mZPoPqUkc3ZCFmsC4fEz. That was I said that the value of idDocument will able to see when we successfully retrieve the data from Firestore.

By getting the value of idDocument, now we can do process delete and update the data in Firestore.

Update Data


By using the following code you can update your data properly


The result of the code is :

Updating data
The thing you must remember is, when you try to update data, just use method set(), NOT add(), okay?


Delete Data


Now the last, you can delete your data by using the code below :

The result of the code is :

Result of deleting data

That's all about the steps how to do create, read, update and delete data from/to Firebase Firestore. Thank you.

Saturday, May 12, 2018

What is Inheritance in Java? Here is Detail Explanation of Inheritance

What  Inheritance is? Perhaps for those who learning about object oriented programming (OOP) have heard about this concept. Maybe there are some people have understood about Inheritance or there are still confuse about how Inheritance really work.

This article will guide you who learning object oriented programming and still don't understand the concept of Inheritance.

What Inheritance is?


Inheritance is the process of inheriting data/method from super class to its sub class. What does it means? It means what is owned by super class (read : method/process) is also owned by its sub class.



What is the characteristic of class that use Inheritance concept?


The main characteristic that you can find is that class will use extends to its super class. For example :


From the snippet above, Class Tree as Super Class and Class Pine as Sub Class. Whatever data/method/process that owned by Class Tree is also owned by Class Pine.

Is all extends means that class has implement Inheritance concept?


Basically yes, but we have to get the concept of Inheritance, why developers created Inheritance model? Have you thinking about it?

Inheritance concept created in order to make easy developers to access the same method/function in various classes by extends the super class.

But there are some things that must be thought out before you implement Inheritance in your project, here are they :
  1. Make sure you have understood the flow of your program and its characteristic (is that related with the Car, People or the thing that have much entities inside it)
  2. If you create a class that contains function/method, thought first, is that method will used much time in different classes or not
  3. How often we will call the same method or function
  4. How important the method or function in class where will use it.
If you requirements like the 4 point above, you should use the Inheritance concept. But if now, there is no problem with that, here we talk about the best practice implementation of Inheritance.

Example :

If you create super class called Class Tree, the create sub class called Class Yamaha, and then the Class Yamaha extends Tree{}. it totally wrong! We should extends that much related with the Super Class, not just do extends like what you will. haha

Until here, understood?

So, what is the right characteristic that can implement Inheritance concept?


There are much example that can implement the Inheritance concept, here I will give you one :

Characteristic of Hospital

Super Class :
  • Class name : Hospital{...}
  • Method : hospitalName(...), hospitalAddress(....)
Sub Class :
  • Class name : Employee{...}
  • Method : employeeName(...), employeeAge(....)

Perhaps one of you have a question and asking "What the relation of Class Employee and Class Hospital?"

Well, the sure thing is the hospital must have employees, right? either it doctor, back office, front office or other staff. Those employees must be have an identity (looks like idcard) where are they work, so Class Employee has to extends Class Hospital as its Super Class.

Project Example


In order make you clear understand the concept of Inheritance, let implement the characteristic of the Hospital above

Class Hospital

Class Employee

Main Program

Take a loot at the codes above, method setHospitalName and setHospitalAddress called directly from its Super Class. Why? Because those methods will call repeated, so that methods wrote in its Super Class (Class Hospital).

The output of the program above is :


Okay that is the explanation about how Inheritance works, hope you'll understand. Thank you.

Sunday, April 29, 2018

Android Tutorial How To Insert Image Into Firebase Storage

Lately, Google has recommend to use Firebase as your storage when you build an application (web or mobile application). Firebase also known as serverless. Serverless means you no more need API for transfer/send data, with firebase it will be done.

Firebase has much features that can use in you application, there are :
  • Firebase Realtime message
  • Firebase Cloud Firestore
  • Firebase Cloud Function
  • Firebase Storage.
You can read all of these features in firebase official website to get more information about those features.

In this article I will show you how to insert image into Firebase Storage. 


Create your application On Firebase


1. Go to Firebase Console and create your project


2. Choose your platform (Choose Android)


3. Add your SHA-1 Key and your package name


4. Download the google-services.json


5. And place the google-services.json inside you app folder



Gradle Setting


Now let setup your gradle and add some dependencies of Firebase

build.gradle (Project:YourProjectName)

build.gradle (Module:App)

Ok your gradle has been setup, now turn to create the process how to insert image into Firebase Storage.


Java And XML Code


activity_main.xml

MainActivity.java

Chose your image from gallery or taken from camera using Edmodo Image Cropper


After image taken, process the image inside method OnActivityResult and upload it into Firebase Storage


Take a look at this code :


Here is the result of the above code :


That code save your image into path photo_profile/my_photo.jpg (last child will be set as the name of the image). By using this path (same path), the new image that you store will replace the old image, why? Because you put the file into the same path like before.

So, how to store much image into Firebase Storage? Easy. Just replace the last child using Current Milliseconds


The result of the above code will be like this :


After your image has stored into Firebase Storage, now use the following code to display the image into your ImageView

Full Code of MainActivity :

Run your application, select image and click button upload, then the image will stored into Firebase Storage, Goodluck!

Friday, April 27, 2018

Newbie, Here are some references how to asking programming questions

This article will talking about how to be great at asking programming questions, especially for those who new programming world.

Why I said for those who new in programming language?


Yeah, mostly of them asking their programming problem with bad (sometime worst) questions and no explain clearly about their problem.


For example (bad questions) :
  • I need to create simple chat program, could you provide me the source code? (hahaha hell yeah)
  • Sir I got error in every sign in with Facebook from my application, how to fix it? (with no code attached, seriously? haha)
  • Here my code, when I using this code, the application always crash, I don't know how to fix it. Please help me! (We don't know exactly what the problem, have you debugged it?)
  • How to learn ABC programming?
  • How to learn ABC programming instantly? (hahaha)
  • etc
If you are join in some programming groups / forums, you must be face the question similar like that, right? haha,

How to be great at asking programming questions?


There are some type of good programming questions, as long as the questions is clearly describe about your problem, it will be fine.

For reference, here are some good way how to asking programming questions (copied from stackoverflow) :
  • Bad : C# Math Confution
  • Good : Why does using float instead of int give me different results when all of my inputs are integers?
  • Bad :  [php] session doubt
  • Good : How can I redirect users to different pages based on session data in PHP? (You have to attached to code, if not, your question will be in to bad category questions)
  • Bad : android if else problem (your question not clearly enough)
  • Good : Why does str = "value" evaluate to false when str is set to "value"?
To avoid bad reputation because of your question, try to do some steps that make your questions a little bit clearly.


Explain The Problem


Sometimes people who asking their question with no detail explanation, like : How to create Facebook login in our application?, insane!

You should tried coding first by your selves, then find the error and explain the difficulty, by providing the clear question it will be much people excited to answer your questions. Don't forget to provide the code!

Provide the code snippet


This is the most important thing. You have to provide the codes and explain your problem related to your code snippet.

If you are follow some tutorials, just attached the link where you find the tutorial after that tell people which line of those tutorial that give you an error. But, in some case people need you attached full code of your class.


If people mark you as duplicate questions, what should you do?


Talking about programming, we also talking about abstract problem. Perhaps your questions look similar like others that has been solved and your question marked as "possible duplicated". If it happened to you, don't worry, if you think your questions is different, then tell other what the different of your question by edit your question and provide some descriptions.

Also, you have to tell people that you have tried all answers that similar with your questions. don't forget to provide the link's answers.

But, if your question indeed similar like others, and you never try the others answer, you question will be down voted or maybe closed.

Be interactive with people who answer your question.


This makes you easier to find the most appropriate answer to your question. For example if there any answers and it not what you looking for, you should comment the answer and tell what you really needed.

If you have no idea of the result from the answer that given to you, you have to try it first into your program, whatever the result you still have to tell the owner of the answer, like "I have tried your answer, but it still doesn't work." or "Nice, your code works, thank you".

Actually there are much reference how to be great at asking programming language, this article just a little bit my experience when asking programming questions. Hope this article will guide you how to be great at asking programming questions.

Monday, January 22, 2018

Android Simply Login Using Firebase UI

Hello everyone, today i gonna be post a nice tutorial relate with login process that provided by Firebase called Firebase UI.

 

What is Firebase UI ?


As simple as i can explain, Firebase UI will provide us the easy way  to creating login process even  login using Facebook, Google account, Email or Twitter

With Firebase UI you don't need create your login form or other API to do login, Firebase UI will handle it all.

Video Result



For achieve that goal, please follow the following steps :

Create Your Application On Firebase


Please go to firebase console and create your project

Create firebase project

After that, choose your platform, in this case choose Android Platform.

Choose platform

Add your SHA-1 key and your package name

Add package name and SHA-1

Download the google-service.json

Download google-service.json

After downloaded, place it inside your app folder. Please take a look at the following figure

App folder

Configuration Gradle


Make sure your gradles are similar with the following gradles :

build.gradle(Project : YourProjectName)


build.gradle(Module : App)


Create your Android Studio Project


You just need 1 XML layout and 1 java class to create login process using Firebase UI. Here they are :

activity_main.xml


Write the following code to create a simple view.

MainActivity.java


Run your project, and may it running well. Thank you.

Download the project here : LINK GITHUB

Wednesday, November 8, 2017

Insert Image From Android To Database (PHP + MySQL)

This article will show a simple tutorial about how to insert image from android to database. Insert image from android to database possible achieve by creating local database using PHP and MySQL.

So this tutorial using two projects, 1 PHP project and the other one is Android project. Don't forget to create your database to store the image that received from Android.

Image will be send from android by send its json to server. In php project, we also create a file that handle json from mobile (android).

Scenario 


We will save the file of image in the directory inside the php project. In database we just store the path of the image from php project.

For example
  • Users choose image obama.jpg from their android
  • In PHP project, there is a directory named imagefromandroid.
  • In database, just store its path, so it's become : imagefromandroid/obama.jpg.

Create Database


In this project i just create simple database, only one table. Please adjust your project and your database.

Database

PHP Project


Create a directory that used to save the file of image that received from android. Take a look at the following image

Directory

I above figure, i create a directory where image from mobile android will stored name imagefromandroid. Also, we will use two php classes, Connection.php and insertImageFromMobile.php.

Connection.php


This file used to connect your PHP Project to your database. The code look like the follows :

InsertImageFromMobile.java


This class used to handle data that received from mobile in json form. The code is look like the follows :


Android Project


There are several classes and xml files that we create in order to able to send image to the server or database.

LoggingInterceptors.java


This class created for handle response of retrofit. In other word, we able to see request process of retrofit to the endpoint.


ResultModel.java


This class will handle response message of request. The response message that will shown by this class are success or failed.



ApiService.java


This class is an interface. In this class will declare endpoint of BASE URL.


ApiClient.java



This class contains the BASE URL that used in this project. And also in this class contains initialized of the retrofit

MainActivity.java


All class or method that used to send image to the server will called in this class. Full code of MainActivity :


Okay, that's all. Hope it helps you to learn how to insert image from android to database.

Monday, November 6, 2017

Android Displaying Multiple Markers From Database

This article will share you how to displaying multiple markers from database. Like the title data latitude and longitude will get from server (Server created with PHP and MySQL).

What is marker means?


Marker on Maps is a mark of a place on Google Maps. The main requirement in order to able to show the marker on Google maps are data latitude and longitude.

For example :

The coordinates of Badung Regency are -8.58193 (Latitude) and 115.177059 (Longitude). You also can check coordinates of your location on this web : Distancesto.

Ok, that's a short description about markers and its requirements, now i will share about creating the project.

To achieve the goal of this project, i create 2 projects, that are : 1 PHP project (for creating server) and 1 Android project (for client).

Creating Server


In this article i will create a simple server just for learning and not recommend to implementing in production project.

First you have to create your database structure and second create the php to connecting your project to your database.

Database


I only use one table. And inside the table i have inserted the data of latitude and longitude. Take a look at the following image

Data LatLng

PHP Project


There are two files in php project. First class for connect to database and the second class for displaying data location in JSON.

Connection.php


Please adjust your username, password and your database name.

JsonDisplayMarker.php


The above class will displaying data in json. Here is the output of the above class :

See also : Create file Json Object and Array Using PHP

Android Project


I will create the project by using the default template of Android Studio. Fyi, i use Android Studio Versi 2.2.2, not the latest version.

First, click start a new android project

Create New Project
Second, name your project by filling field Application name and Company domain.

Name the project

Third, select your platform, in this case just check the phone and tablet.

Select the platform

Fourth, select the template that you want to use. Related with this project, just select Google Maps Activity.

Template

Fifth, give name your activity, layout and title by filling the field Activity name, Layout name, Title.

Attribute name

After that, click button finish.

Library Gradle


I have added some libraries. Modify your gradle to be the same as bellow :


Generate API_KEY


In order to able to display Google Maps in your project, you have to generate the API_KEY in Google Developer Console. You can user the existing project, or create new project.

First, generate API_KEY by clicking button GET A KEY

image via putuguna.com

Second, you can use the existing project or create new project.

image via putuguna.com

Third, copy your API_KEY and place in meta-data androidManifest.

image via putuguna.com


Confuse? Take a look at the following meta-data in AndroidManifest file :

AndroidManifest


Please adjust your AndroidManifest to be same as below :

Okay, until here, you have done preparing your requirement for creating Google Maps in your application. Next we will create several java classes.

Create two classes as model to handle the data from json


LocationModel.java


This class used to handle one object data from json. The code look like the follows :


ListLocationModel.java


Because json that we will receive is an array, means more than one object, then one object in class LocationModel will created as list with class ListLocationModel.

If you confuse with json format array and json format object, please continue read here : Create file json format array and json format object using PHP MySQL.

Class Client and Interface


The following are two classes that define the main URL (client) and its part (endpoint).

ApiClient.java


if you clearly see the URL, it contains digits, right? These digits is the IP of my computer that connected with my local internet (Tethering from my phone).

ApiService.java



Class Logging Interceptors



Class MainActivity


This class is the main class that used to displaying all data makers on Google Maps. The full code is show as bellow :

Okay, that's all. Running your project and hope works well.

Download project by clicking the following image 

https://github.com/griajobag/DisplayListMarkerFromDatabase

Thursday, November 2, 2017

Create JSON Object And Array Using PHP MySQL

If you want to transfer your data to another client (mobile or server) you have to create your own JSON. JSON or JavaScript Object Notation is a file that used to transfer data between clients/server or between two different programming languages.

JSON have two types, they are :

  • JSON type Object
  • JSON type Array
What the different of these type?

JSON Type Object


JSON Type Object has characteristic always using mark {}, please take a look at the following sample json :



JSON type Array


We can call a json as an array is the JSON use the mark [] in its data. Why this json use the mark []? Because the data that will be send is more that one object. Please take a look at the following sample :


If you will transfer data more that one object, use the JSON type array. But, if you just transfer only one object, use JSON type Object as its format.

Create JSON Array using PHP and MySQL 


First, you have to preparing your data and has been saved in your MySQL database. In this project i will using data in my database and look like the follows :


Create your connection to connect the database and your project. Here is the code :

Connection.php


After you connection has works well. Now create your JSON type Array

JSONArray.php


Running the above file, and you will show your data as list in json.

Create JSON type Object Using PHP and MySQL


Here is the file that you can use to create JSON type Object.

The above json will catch data type object from android and will send its data to database. I have been implemented it and has been post the project in this blog. Please open this : Tutorial insert data from android using retrofit to Server (PHP + MySQL).

That's all. Hope this article helps you. Thank you.

Tuesday, October 3, 2017

Implementing Load More When Woking On Blogger's API

In this tutorial, i will share a tutorial about how to implementing load more and pagination function in Blogger API using retrofit.

When you working with API Blogger, you will face an endpoint that contains much data in list.

The URL that contains a lot of data in list is :
https://www.googleapis.com/blogger/v3/blogs/your-id-blog/posts?key=YOUR_AUTH_ID
That URL will display output json look like the following image :

Output List Json

If you run the URL above, the result will give you only 10 datas in first page. Take a look at attribute items.

To get the next page you have to request it again (load more) and display the result as pagination. To implementing the pagination (request the next page), you can use the attribute nextPageToken. As long as the attribute nextPageToken exist, it means the data still exist in the next page.

Request the first page


Just request the first data in normal URL (without params nextPageToken). In this case i request the data using retrofit :

On the code above, I request the first page and set the data into the adapter. Don't forget to save the value of nextPageToken.

In load more function, i check the nextPageToken is exist or not. Is exist, then request the next page.

Request the next page


To request the next page, you have to add params pageToken, and the value set from nextPageToken.

The URL to be :
https://www.googleapis.com/blogger/v3/blogs/your-id-blog/posts?key=YOUR_AUTH_ID&pageToken=CgkIChjigf_aoisQu_ugxZqtye83
The code is look like the follows :


That's all what i did to implementing load more pagination when request data using Blogger API. Hope it helps. Thank you.