Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- ======================
- package com.example.zeevm.mygroupchat;
- import android.content.Context;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- 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;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity {
- EditText myMsg;
- String chatNode="message";
- String messageHold="";
- List<String> msgList;
- Context context;
- ListView myMsgList;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- listenMessage();
- }
- private void setPointer()
- {
- myMsg=(EditText)findViewById(R.id.helloMsg);
- this.context=this;
- msgList=new ArrayList<>();
- myMsgList=(ListView)findViewById(R.id.msgList);
- }
- //sending message to our firebase
- public void sendMsg(View view)
- {
- FirebaseDatabase database=FirebaseDatabase.getInstance();
- DatabaseReference myRef = database.getReference(chatNode);
- String myString = myMsg.getText().toString();
- myRef.setValue("(zeev) "+myString);
- myMsg.setText("");
- }
- private void listenMessage()
- {
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference myRef = database.getReference(chatNode);
- myRef.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- messageHold=dataSnapshot.getValue(String.class);
- Toast.makeText(context, messageHold, Toast.LENGTH_SHORT).show();
- msgList.add(messageHold);
- //display in listView
- MsgAdapter msgAdapter=new MsgAdapter(context,msgList);
- myMsgList.setAdapter(msgAdapter);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Toast.makeText(context, "Error accord", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- activity_main.xml
- ====================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter Message...."
- android:id="@+id/helloMsg"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="SEND MESSAGE..."
- android:onClick="sendMsg"
- android:background="#009fff"
- android:textColor="#fff" />
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/msgList"></ListView>
- </LinearLayout>
- PROJECT GRADLE
- ==================================
- // Top-level build file where you can add configuration options common to all sub-projects/modules.
- buildscript {
- repositories {
- jcenter()
- }
- dependencies {
- classpath 'com.android.tools.build:gradle:2.2.2'
- classpath 'com.google.gms:google-services:3.0.0'
- // NOTE: Do not place your application dependencies here; they belong
- // in the individual module build.gradle files
- }
- }
- allprojects {
- repositories {
- jcenter()
- }
- }
- task clean(type: Delete) {
- delete rootProject.buildDir
- }
- APP GRADLE
- =====================
- apply plugin: 'com.android.application'
- android {
- compileSdkVersion 25
- buildToolsVersion "25.0.0"
- defaultConfig {
- applicationId "com.example.zeevm.mygroupchat"
- minSdkVersion 19
- targetSdkVersion 25
- versionCode 1
- versionName "1.0"
- testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
- }
- buildTypes {
- release {
- minifyEnabled false
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- }
- }
- }
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
- exclude group: 'com.android.support', module: 'support-annotations'
- })
- compile 'com.android.support:appcompat-v7:25.0.0'
- testCompile 'junit:junit:4.12'
- compile 'com.google.firebase:firebase-core:9.0.2'
- compile 'com.google.firebase:firebase-database:9.0.2'
- }
- apply plugin: 'com.google.gms.google-services'
- manifest.xml
- ================
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.zeevm.mygroupchat">
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- 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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement