Skip to main content

Posts

GSON - Simple Serialization + Deserialization

GSON library, which is provided by Google and makes the process of turning Java objects into their  JSON representation (serialization) and JSONs into Java objects (deserialization) very easy. we are going to create a simple Java class, create an object of it, turn it into a JSON and then turn that JSON back into the java object. We will also learn how to use the @SerializedName annotation to use different keys and variable names. Links and dependencies:-   github.com/google/gson In Employee.java class file add these lines of codes:-   Employee.java import com.google.gson.annotations.SerializedName ; public class Employee { @SerializedName ( "first_name" ) private String mFirstName; @SerializedName ( "age" ) private int mAge; @SerializedName ( "mail" ) private String mMail; public Employee ( String firstName, int age, String mail ) { mFirstName = firstName; mAge = age; mMail = mail; } } ...

Fragment newInstance

Since the Android system uses the default no-arg constructor when recreating a fragment, we should not pass values over an overloaded constructor with arguments when we instantiate it. Otherwise we get a warning that says “Avoid non-default constructors in fragments: use a default constructor plus Fragment setArguments(Bundle) instead” or we even get an InstantiationException. The correct way to instantiate a new fragment and passing values to it, is by creating a bundle, setting arguments on this bundle and then passing it to our fragment. Inside the fragment we then retrieve this bundle and get the values out of it. A clean way to do this is by creating a so called “factory method”. In MainActivity.java class  add these lines of codes:- import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) { ...

Fragment-to-fragment-communication-with-interfaces

In this Tutorial we will learn  how to send data between two fragments. Since fragments should be modular, reusable components, the communication happens over an interface that the underlying activity implements. This way we can send data from a fragment to an activity and from there to another fragment either by calling a public method or by instantiating a new fragment and sending the values as arguments to it. In previous tutorials we have seen what is fragments and how will we create a fragment. In MainActivity.java class file add these lines of codes:- import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {     private FragmentA fragmentA;     private FragmentB fragmentB;     @Override     protected void onCreate(Bundle savedInstanceState) {         ...

Notifications & Notification Channels

Part 1 – Notification Channels Part 2 – Tap Action & Action Buttons Part 3 – Large Icon, BigTextStyle & InboxStyle Part 4 – BigPictureStyle & MediaStyle Part 5 – MessagingStyle & Direct Reply Part 6 – Progress Bar Notification Part 7 – Notification Groups Part 8 – NotificationChannelGroups Part 9 – Notification Settings Part 10 – Delete Notification Channels                                     Part 1 – Notification Channels In part 1 we will start by creating notification channels (Categories) which are necessary since Android Oreo (API level 26) to be able to show any notifications. These channels should be created as soon as we start our app, so we will do it in the onCreate method of a class that extends Application. When we create these channels, we have to pass an ID, a name and an importance level. Additionally we can cus...