Android Starting with Firebase

As most of you are knowing Firebase is on boom these days. Firebase is a platform for the web,mobile applications which provide real-time database,storage,authentication, Fcm, push notifications easily and is fast and reliable. Firebase is considered as the alternative of Parse.com

So,let’s start using Firebase for android.

1. First of all, create Firebase account using Gmail id ( Firebase Console ).

firebaseandroid

2. Create a project in Firebase by clicking on create project button, then enter the project name and select a country and then click on next.

Androidworkingwithfirebase

3. Then enter package name of the project and click next, it will download JSON file which will be used in the android studio to integrate your project with Firebase.

creatingandroidproject

createproject

Now, Create a project in Android studio using same package name which you used while creating the project in Firebase.

After creating a project in Android Studio add a dependency in module build.Gradle file

compile 'com.google.firebase:firebase-auth:9.0.0'   //for autheticating the user

then add the JSON file under module app root directly and then add the classpath in project build.Gradle file

classpath 'com.google.gms:google-services:3.0.0'

Then add the plugin in Module’s build.gradle file.

apply plugin: 'com.google.gms.google-services'

Let’s Enable Sign-in-method at Firebase. Click on Auth present at the left side and then click on Sign-in-method and enable the First option under Providers i.e Email/Password.

Screen Shot 2016-07-30 at 3.26.51 PM

Now, let’s create a layout for Activity containing Two Editext for email and password and two buttons for signup and sign in.

activity_signin.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.database.sample.jagjit.singh.SignInActivity">

    <LinearLayout
        android:id="@+id/layout_email_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/icon"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/field_email"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="center_horizontal"
            android:hint="@string/hint_email"
            android:inputType="textEmailAddress"
            android:maxLines="1" />

        <EditText
            android:id="@+id/field_password"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="center_horizontal"
            android:hint="@string/hint_password"
            android:inputType="textPassword"
            android:maxLines="1" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_buttons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_email_password"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button_sign_in"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="@string/sign_in" />


        <Button
            android:id="@+id/button_sign_up"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="@string/sign_up" />

    </LinearLayout>


</RelativeLayout>

Create an Activity to Sign in and Sign up the user with required credentials.

Code to Validate whether the user entered both Email and Password ,returns true if both fields are entered.

private boolean validateForm() {
       boolean result = true;
       if (TextUtils.isEmpty(mEmailField.getText().toString())) {
           mEmailField.setError("Required");
           result = false;
       } else {
           mEmailField.setError(null);
       }

       if (TextUtils.isEmpty(mPasswordField.getText().toString())) {
           mPasswordField.setError("Required");
           result = false;
       } else {
           mPasswordField.setError(null);
       }

       return result;
   }

Code to Signup the user.

private void signUp() {
        Log.d("MainActivity", "signUp");
        if (!validateForm()) {
            return;
        }

        showProgressDialog();
        String email = mEmailField.getText().toString();
        String password = mPasswordField.getText().toString();

        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("MainActivity", "createUser:onComplete:" + task.isSuccessful());
                        hideProgressDialog();

                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "Successful! Please Sign In",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Sign Up Failed",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

Code to Sign In the user.

private void signIn() {
       Log.d("MainActivity", "signIn");
       if (!validateForm()) {
           return;
       }

       showProgressDialog();
       String email = mEmailField.getText().toString();
       String password = mPasswordField.getText().toString();

       mAuth.signInWithEmailAndPassword(email, password)
               .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                   @Override
                   public void onComplete(@NonNull Task<AuthResult> task) {
                       Log.d("MainActivity", "signIn:onComplete:" + task.isSuccessful());
                       hideProgressDialog();

                       if (task.isSuccessful()) {
                           Toast.makeText(MainActivity.this, "Sign In Successful",
                                   Toast.LENGTH_SHORT).show();
                       } else {
                           Toast.makeText(MainActivity.this, "Sign In Failed",
                                   Toast.LENGTH_SHORT).show();
                       }
                   }
               });
   }

Full code MainActivity.java

package com.coderzpassion.firebasesample;

import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private FirebaseAuth mAuth;

    private EditText mEmailField;
    private EditText mPasswordField;
    private Button mSignInButton;
    private Button mSignUpButton;

    private ProgressDialog mProgressDialog;

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

        // Views
        mEmailField = (EditText) findViewById(R.id.field_email);
        mPasswordField = (EditText) findViewById(R.id.field_password);
        mSignInButton = (Button) findViewById(R.id.button_sign_in);
        mSignUpButton = (Button) findViewById(R.id.button_sign_up);

        // Click listeners
        mSignInButton.setOnClickListener(this);
        mSignUpButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_sign_in:
                signIn();
                break;
            case R.id.button_sign_up:
                signUp();
                break;
        }

    }

    private boolean validateForm() {
        boolean result = true;
        if (TextUtils.isEmpty(mEmailField.getText().toString())) {
            mEmailField.setError("Required");
            result = false;
        } else {
            mEmailField.setError(null);
        }

        if (TextUtils.isEmpty(mPasswordField.getText().toString())) {
            mPasswordField.setError("Required");
            result = false;
        } else {
            mPasswordField.setError(null);
        }

        return result;
    }

    private void signIn() {
        Log.d("MainActivity", "signIn");
        if (!validateForm()) {
            return;
        }

        showProgressDialog();
        String email = mEmailField.getText().toString();
        String password = mPasswordField.getText().toString();

        mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("MainActivity", "signIn:onComplete:" + task.isSuccessful());
                        hideProgressDialog();

                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "Sign In Successful",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Sign In Failed",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void signUp() {
        Log.d("MainActivity", "signUp");
        if (!validateForm()) {
            return;
        }

        showProgressDialog();
        String email = mEmailField.getText().toString();
        String password = mPasswordField.getText().toString();

        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d("MainActivity", "createUser:onComplete:" + task.isSuccessful());
                        hideProgressDialog();

                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "Successful! Please Sign In",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Sign Up Failed",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    public void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setMessage("Loading...");
        }

        mProgressDialog.show();
    }

    public void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Output

workingwithfirebase   successfullysignedup   successfulsignin

For official documentation:- Refer here

Next Step, Once User is log in, next time we can directly show the user any Activity as per Requirement.

To check if user is already logged In

 if (mAuth.getCurrentUser() != null) {
            // user is already log in
        // show Home Activity
        }
else
{
//ask the user to sign in
signIn();
}

 

 

 

Android Working with Fragments Part three

If you are not Familiar with Fragments please check my Android Working with Fragment post.

In my earlier post, i described how to replace the fragments in container containing one fragment at a time to check refer here Android Working with Fragments Part Two

In this post, I will describe how to add the fragments in the container than on pressing the hard Back Button restore to previous Fragment. In short, how to manage the Fragments in the container and implementing the back button pressed method of activity to push the user to restore to previous UI or previous Fragment on Back button.

So,Let’s Start:-

  1. First, we will create a Fragment layout containing two buttons and Textview, one button to add the fragment to the container, other to remove the fragment from container and Textview to show help text.
  2.  Then we will use this layout and create fragment and implement the functionality to add the fragment and for another button to remove the fragment.
  3. Then we will create an Activity layout containing Framelayout which will act as a Container .

Fragment layout as fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove Fragment from Container"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="50dp"
        android:text="Click on Below Button To add another fragment to the container"/>

    <Button
        android:id="@+id/addfragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add another fragment"
        />

</LinearLayout>

Fragment as FirstFragment

package com.coderzpassion.fragmentssample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by coderzpassion on 13/06/16.
 */
public class FirstFragment extends Fragment {
    public final ScheduledExecutorService worker =
            Executors.newSingleThreadScheduledExecutor();
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_first, container, false);
        Button title=(Button)view.findViewById(R.id.addfragment);
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FirstFragment firstFragment = new FirstFragment();
              final  FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.add(R.id.frame_container,firstFragment).addToBackStack("firstfragment").commit();

          // slightly added a delay as it takes some time to add fragment to container
                // then checked for no of fragments in the container
                Runnable r = new Runnable() {
                    @Override
                    public void run() {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getActivity(), "Total Fragments in Container " + fragmentManager.getBackStackEntryCount(), Toast.LENGTH_SHORT).show();
                            }
                        });

                    }
                };
                worker.schedule(r, 1000, TimeUnit.MILLISECONDS);
            }
        });


        Button back=(Button)view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // created s static method in Actvity to remove the current fragment from Container
                ((AddingFragmentContainer)getActivity()).popSingleFragment();
            }
        });
        return view;

    }



}

Activity layout as activity_addfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Activity as AddingFragmentContainer

package com.coderzpassion.fragmentssample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by coderzpassion on 13/06/16.
 */
public class AddingFragmentContainer extends FragmentActivity {

    FragmentManager fragmentManager;
    FragmentTransaction transaction;
  public FirstFragment firstFragment;
    public final ScheduledExecutorService worker =
            Executors.newSingleThreadScheduledExecutor();

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

         firstFragment=new FirstFragment();
        fragmentManager = getSupportFragmentManager();
        transaction=fragmentManager.beginTransaction();

          transaction.add(R.id.frame_container, firstFragment).addToBackStack("firstfragment").commit();

        // slightly added a delay as it takes some time to add fragment to container
        // then checked for no of fragments in the container
        Runnable r=new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(AddingFragmentContainer.this, "Fragments in Container " + fragmentManager.getBackStackEntryCount(), Toast.LENGTH_SHORT).show();
                    }
                });

            }
        };

        worker.schedule(r, 1000, TimeUnit.MILLISECONDS);
    }


    @Override
    public void onBackPressed() {


        if(fragmentManager.getBackStackEntryCount()>0)
        {
            fragmentManager.popBackStack();
            Log.e("poping back"," Done");
        }
        else
        {
            super.onBackPressed();
        }

    }

    public void popSingleFragment()
    {
        if(fragmentManager.getBackStackEntryCount()>0)
        {
            fragmentManager.popBackStack();
            Log.e("poping back"," Done");
        }
        else
        {
            super.onBackPressed();
        }
        // slightly added a delay as it takes some time to add fragment to container
        // then checked for no of fragments in the container
        Runnable r=new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(AddingFragmentContainer.this, "Fragments Remaining in Container " + fragmentManager.getBackStackEntryCount(), Toast.LENGTH_SHORT).show();
                    }
                });

            }
        };

        worker.schedule(r, 1000, TimeUnit.MILLISECONDS);

       }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coderzpassion.fragmentssample" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
  
        <activity android:name=".AddingFragmentContainer">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Output

addingfragmentstocontainer    managingbackbuttonforfragments   removingfragmentsfromcontainer

Android Working with Fragments Part Two

If you are not Familiar with Fragments please check my Android Working with Fragment  post.

In this post, I will be describe working with fragments using a Container layout i.e having a container, and replacing the fragments in the container according to user actions. In brief, we will have one fragment in the container at a time when user clicks on the button we replace the current fragment with the required fragment.

So,Let’s Start:-

  1. First we will create a Fragment layout containing textview and Fragment to recieve the argument and set to Textview.
  2. Then we will create another Fragment using the same layout as to describe we can use the Required Fragment.
  3. Then we will create a Activity layout containing FrameLayout which will act as a Container and four buttons on which click we will replace the fragments in the container.

Fragment layout as fragment_container.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40px"
            android:text="FirstFragment"
            android:layout_centerInParent="true"
            android:id="@+id/titlename"/>
    
</RelativeLayout>

ContainerFragment

package com.coderzpassion.fragmentssample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by coderzpassion on 13/06/16.
 */
public class ContainerFragment extends Fragment {

    String maintitle="";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        maintitle=getArguments().getString("title");
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_container, container, false);

        TextView title=(TextView)view.findViewById(R.id.titlename);
        title.setText(maintitle);

        return view;

    }
}

ContainerTwoFragment

package com.coderzpassion.fragmentssample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by coderzpassion on 21/06/16.
 */

public class ContainerTwoFragment extends Fragment {

    String maintitle="";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        maintitle=getArguments().getString("title");
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_container, container, false);

        TextView title=(TextView)view.findViewById(R.id.titlename);
        title.setText(maintitle);

        return view;

    }
}

Activity layout as activity_container

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
        android:weightSum="4">

        <Button
            android:id="@+id/first"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="First"/>

        <Button
            android:id="@+id/second"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Second"/>

        <Button
            android:id="@+id/third"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="third"/>

        <Button
            android:id="@+id/fourth"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Fourth"/>

    </LinearLayout>

</RelativeLayout>

FragmentContainerActivity

package com.coderzpassion.fragmentssample;

import android.app.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;

/**
 * Created by coderzpassion on 13/06/16.
 */
public class FragmentContainerActivity extends FragmentActivity implements View.OnClickListener{


    Button first,second,third,fourth;
    Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        first=(Button)findViewById(R.id.first);
        second=(Button)findViewById(R.id.second);
        third=(Button)findViewById(R.id.third);
        fourth=(Button)findViewById(R.id.fourth);
        first.setOnClickListener(this);
        second.setOnClickListener(this);
        third.setOnClickListener(this);
        fourth.setOnClickListener(this);

        //this fragment will be called first when application runs
        ContainerFragment fragment1=new ContainerFragment();
        Bundle b=new Bundle();
        b.putString("title", "Welcome To Fragment Tutorials! CoderzPassion Click on Buttons To Replace Current fragment in Container");
        fragment1.setArguments(b);
        if (fragment1 != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment1).commit();
        }

    }


    //on click of buttons
    @Override
    public void onClick(View v) {

        switch (v.getId())
        {

          case  R.id.first:
              fragment=new ContainerFragment();
              Bundle b=new Bundle();
              b.putString("title","first");
              fragment.setArguments(b);
            if (fragment != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .replace(R.id.frame_container, fragment).commit();
            }
            break;

            case  R.id.second:
                fragment=new ContainerFragment();
                Bundle b1=new Bundle();
                b1.putString("title","second");
                fragment.setArguments(b1);
                if (fragment != null) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.frame_container, fragment).commit();
                }
                break;

            case  R.id.third:
                fragment=new ContainerTwoFragment();
                Bundle b2=new Bundle();
                b2.putString("title","third");
                fragment.setArguments(b2);

                if (fragment != null) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.frame_container, fragment).commit();
                }
                break;

            case  R.id.fourth:
                fragment=new ContainerTwoFragment();
                Bundle b3=new Bundle();
                b3.putString("title","fourth");
                fragment.setArguments(b3);

                if (fragment != null) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.frame_container, fragment).commit();
                }
                break;
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coderzpassion.fragmentssample" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity android:name=".FragmentContainerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Output

androidfragmentsusingcontainer   androidaddingfragmentstocontainer   androidreplacingthefragmentincontainer

How to implement Fragments in Android.

A fragment is a part or portion of Activity. It is also known as sub-activity.

The Primary classes related to fragment are:

  1. android.app.Fragment: Base class for fragment definitions.
  2. android.app.FragmentManager: Class for interacting with fragments within the activity.
  3. android.app.FragmentTransaction: Class for performing operations like adding or replacing the fragments.

Google also provides compatibility support v4 package for Fragments.Its main classes are :

  1. android.support.v4.app.FragmentActivity: The base class for all activity to containing fragment related features.
  2. android.support.v4.app.Fragment: The base class for all fragment operations.
  3. android.support.v4.app.FragmentManager: The class for interacting with fragments in activity.
  4. android.support.v4.app.FragmentTransaction: The class for performing fragments operations like adding,replacing the fragment.

Note: To use compatibility support v4 package your activity must use FragmentActivity as a Base Class.

if you are already familiar with fragments check Android Working with Fragments Part Two

Main Features of Fragment:-

  1. A Fragment has its own layout and lifecycle similar to Activity.
  2. A Fragment can be added or removed at runtime thus making UI look appropriate.
  3. There can many fragments in one activity.
  4. Fragment are reusable, Fragments must be embedded in activities.

So, Let’s by taking an example:

Steps:-

  1. Layout for fragment named as data_fragment.
  2. Create a Fragment named as DataFragment using above layout.
  3. Layout for fragment named as list_fragment.
  4. Create a Fragment named as Listing Fragment using above layout.
  5. Now create a layout for Activity named as activity_main containing above two Fragments we created.
  6. Now Create an Activity named as MainActivity and run the Project.

Open AndroidStudio or Eclipse and create an android application project and follow the instructions and then click on finish.

  1. First of all, we will create a layout for fragment as data_fragment
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:background="#5ba4e5"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40px"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:id="@+id/mainitems"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#ffffff"
        android:textSize="30px"
        android:id="@+id/subitems"/>

</LinearLayout>

2. Create Fragment class DataFragment

package com.coderzpassion.fragmentssample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by coderzpassion on 13/06/16.
 */
public class DataFragment extends Fragment {

    TextView text,vers;

    @Override

    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.data_fragment, container, false);
        text= (TextView) view.findViewById(R.id.mainitems);
        vers= (TextView)view.findViewById(R.id.subitems);


        return view;

    }
    public void change(String txt, String txt1){
        text.setText(txt);
        vers.setText(txt1);

    }

}

3. Create a layout named as list_fragment

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listfragment" />
</LinearLayout>

4. Create a Fragment named as ListingFragment

package com.coderzpassion.fragmentssample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Created by coderzpassion on 13/06/16.
 */
public class ListingFragment extends Fragment  implements AdapterView.OnItemClickListener{


    String[] MainItems = new String[] { "main item 1","main item 2","main item 3","main item 4","main item 5","main item 6","main item 7","main item 8","main item 9" };
    String[] SubItems = new String[]{"sub item 1","sub item 2","sub item 3","sub item 4","sub item 5","sub item 6","sub item 7","sub item 8","sub item 9"};
    ListView list;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.list_fragment, container, false);
         list=(ListView)view.findViewById(R.id.listfragment);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, MainItems);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
        return view;

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        DataFragment txt = (DataFragment)getFragmentManager().findFragmentById(R.id.fragment2);
        txt.change(MainItems[position], "SubItems : " + SubItems[position]);
    }

}

5. Create a layout named as activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2"
    >
    
            <fragment
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                class="com.coderzpassion.fragmentssample.ListingFragment"
                android:id="@+id/fragment"/>
            <fragment
                android:layout_width="0dp"
                android:layout_height="match_parent"        
                android:layout_weight="1"
                class="com.coderzpassion.fragmentssample.DataFragment"
                android:id="@+id/fragment2"/>


    </LinearLayout>

6. Create an Activity class named as default MainActivity.

package com.coderzpassion.fragmentssample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Now run this Project you will find the Output like this

androidfragments  androidworkingwithfragmentsexample   workingwithfragments

How to Implement SnackBar in Android.

Android 5.0 introduced the material design bring the all new design, patterns, widgets and much more. If you not familiar with material design start here.  Here I will talk about SnackBar introduced in Android Material Design. Snack bar is similar android Toast.

Before you can use Snack bar in your Application you need to add following support design dependency

compile 'com.android.support:design:23.1.1'

Snackbar can also contain  Button, which makes it different from Toast.

Snackbar can be shown as follows consisting a message like Toast.

Snackbar.make(view, "CoderzPassion Snackbar", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();

 SnackBar with Button

snackbarwithbutton=Snackbar.make(findViewById(android.R.id.content), "Snackbar with Button", Snackbar.LENGTH_LONG)
                       .setAction("Dismiss", customSnackbarClick)
                       .setActionTextColor(Color.RED);
               snackbarwithbutton.show();

Now Final Code

Layout for MainActivity.java

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    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/snackbarbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Snackbar with Button"
    />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/ic_done"
        app:fabSize="normal"
        app:layout_anchor="@id/coordinatorLayout"
        app:layout_anchorGravity="bottom|right|end"/>


</android.support.design.widget.CoordinatorLayout>

Code for MainActivity.java

package com.coderzpassion.snackbarsample;

import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button snackbarbutton;
    View.OnClickListener customSnackbarClick;
    Snackbar snackbarwithbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        snackbarbutton=(Button)findViewById(R.id.snackbarbutton);
        snackbarbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                snackbarwithbutton=Snackbar.make(findViewById(android.R.id.content), "Snackbar with Button", Snackbar.LENGTH_LONG)
                        .setAction("Dismiss", customSnackbarClick)
                        .setActionTextColor(Color.RED);
                snackbarwithbutton.show();
            }
        });


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Snackbar snackBar=  Snackbar.make(view, "CoderzPassion Snackbar", Snackbar.LENGTH_LONG)
                        .setAction("Action", null);
                snackBar.show();
            }
        });

         customSnackbarClick=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                snackbarwithbutton.dismiss();
            }
        };
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Output

androidcustomsnackbar    androidsnackbar    workingwithsnackbar