Skip to main content

Featured

Game Development Tutorials In Android

Game Development Tutorials In Android Hi Guys,                 We are shortly started Game Development Tutorials in Android Studio. So everyone supports me and share this blogs to your friends. Guys, At this time everyone is freely available to source code and we are provide a video for step by step game developments. you are learn free and feel free to support us. Guys, if you have any question to me, so feel free to comment us. I will try to answer all the question as long as possible.                                                     Thank's                                                                          ...

Android Chatting Application Development Using Firebase Database Tutorials & Source Code

Android Chatting Application Development Using Firebase Tutorials & Source Code



Now,  We are going to develop Android Chatting Application Like Whatsapp using firebase cloud database services. this  is a long part , so understand each activity is smartly , If you are a Java Developer so it's very easy to understand , how to make chatting Application in Android Application.


Now Let's Start :

open new project in your android studio with empty activity.

Add Dependencies 

build.gradle

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

classpath 'com.android.tools.build:gradle:2.3.3'


Add compile file :

app:

build.gradle

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

 compile 'com.firebaseui:firebase-ui:0.6.2'

AndroidManifest.xml

Add Permission : 

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


activity_main.xml

Just Copy And Paste this file Only Application Context is change to your application context path.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context="com.example.vashnodevi.touchup.MainActivity">

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_send"
        android:id="@+id/fab"
        android:tint="@android:color/white"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        app:fabSize="mini"/>


    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/fab"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Message..."
            android:id="@+id/input" />

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

    <ListView
        android:id="@+id/list_of_message"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_above="@+id/fab"
        android:dividerHeight="16dp"
        android:divider="@android:color/transparent"
        android:layout_marginBottom="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

</RelativeLayout>


Now, We create One more resource file , the name of the resource file is : 

list_item.xml


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

    <TextView
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/message_user"
        android:textStyle="normal|bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_alignParentBottom="@+id/message_user"
        android:layout_alignParentRight="true"
        android:id="@+id/message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <TextView
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/message_user"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textSize="18sp"
        android:id="@+id/message_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



</RelativeLayout>


That's it , for the resource file.

Now we are going to create .java file under your project folder where the MainActivity.java , Now Edit Your MainActivity.java file.

                                       MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static int SIGN_IN_REQUEST_CODE = 1;
    private FirebaseListAdapter<ChatMessage> adapter;
    RelativeLayout activity_main;
    FloatingActionButton fab;

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getItemId() == R.id.menu_sign_out)
        {
            AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    Snackbar.make(activity_main,"You Have been Signed Out ", Snackbar.LENGTH_SHORT).show();
                    finish();
                }
            });
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
    protected void onActivityResult(int requestcode, int resultcode, Intent data) {
        super.onActivityResult(requestcode, resultcode, data);
        if(requestcode == SIGN_IN_REQUEST_CODE)
        {
            if(resultcode == RESULT_OK)
            {
                Snackbar.make(activity_main,"Successfully Sign In. Welcome ! ", Snackbar.LENGTH_SHORT).show();
                displayChatMessage();
            }
            else
            {
                Snackbar.make(activity_main,"We couldn't Sign you in ! Please Try Again later ", Snackbar.LENGTH_SHORT).show();
                finish();
            }
        }

    }

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

        activity_main = (RelativeLayout) findViewById(R.id.activity_main);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText input = (EditText) findViewById(R.id.input);
                FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(input.getText().toString(),
                FirebaseAuth.getInstance().getCurrentUser().getEmail()));
                input.setText("");
            }
        });

        //Check if not sign in then navigate sign in page

        if (FirebaseAuth.getInstance().getCurrentUser() == null) {
            startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE);
        } else {
            Snackbar.make(activity_main,"Welcome"+FirebaseAuth.getInstance().getCurrentUser().getEmail(), Snackbar.LENGTH_SHORT).show();
            //Load Contents
            displayChatMessage();
        }
    }
    public void displayChatMessage() {
        ListView listOfMessage = (ListView) findViewById(R.id.list_of_message);
        adapter = new FirebaseListAdapter<ChatMessage>(this,ChatMessage.class,R.layout.list_item,FirebaseDatabase.getInstance().getReference())
        {

            @Override
            protected void populateView(View v, ChatMessage model, int position) {
                //Get References to the view of list item.xml
                TextView messageText,messageUser,messageTime;

                messageText =(TextView) v.findViewById(R.id.message_text);
                messageUser =(TextView) v.findViewById(R.id.message_user);
                messageTime =(TextView) v.findViewById(R.id.message_time);

                messageText.setText(model.getMessageText());
                messageUser.setText(model.getMessageUser());
                messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getMessageTime()));

            }
        };
        listOfMessage.setAdapter(adapter);
    }
}

Create One More Java File .....

                            ChatMessage.java

public class ChatMessage {
    private String messageText;
    private String messageUser;
    private long messageTime;

    public ChatMessage(String messageText, String messageUser)
    {
        this.messageText = messageText;
        this.messageUser = messageUser;

        messageTime = new Date().getTime();
    }
    public ChatMessage() {

    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getMessageUser() {
        return messageUser;
    }

    public void setMessageUser(String messageUser) {
        this.messageUser = messageUser;
    }

    public long getMessageTime() {
        return messageTime;
    }

    public void setMessageTime(long messageTime) {
        this.messageTime = messageTime;
    }
}


That's it, Now Build Your Application And Run this code for Your Android Emulator. Your Chatting Application is ready to use ....


Comments

  1. Your post is really useful for beginners too. To get PHP Training in Chennai is absolutely the right choice and it will be helpful in growing our career. Thank you for your post. Keep posting.

    ReplyDelete

Post a Comment

Popular Posts