Advertisement
zeev

My first Chatter

Dec 5th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. MainActivity.java
  2. ======================
  3. package com.example.zeevm.mygroupchat;
  4.  
  5. import android.content.Context;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.ListView;
  11. import android.widget.Toast;
  12.  
  13. import com.google.firebase.database.DataSnapshot;
  14. import com.google.firebase.database.DatabaseError;
  15. import com.google.firebase.database.DatabaseReference;
  16. import com.google.firebase.database.FirebaseDatabase;
  17. import com.google.firebase.database.ValueEventListener;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24.     EditText myMsg;
  25.     String chatNode="message";
  26.     String messageHold="";
  27.     List<String> msgList;
  28.     Context context;
  29.     ListView myMsgList;
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_main);
  35.         setPointer();
  36.         listenMessage();
  37.     }
  38.  
  39.     private void setPointer()
  40.     {
  41.         myMsg=(EditText)findViewById(R.id.helloMsg);
  42.         this.context=this;
  43.         msgList=new ArrayList<>();
  44.         myMsgList=(ListView)findViewById(R.id.msgList);
  45.     }
  46.  
  47.     //sending message to our firebase
  48.     public void sendMsg(View view)
  49.     {
  50.         FirebaseDatabase database=FirebaseDatabase.getInstance();
  51.         DatabaseReference myRef = database.getReference(chatNode);
  52.         String myString = myMsg.getText().toString();
  53.         myRef.setValue("(zeev) "+myString);
  54.         myMsg.setText("");
  55.     }
  56.  
  57.         private void listenMessage()
  58.     {
  59.         FirebaseDatabase database = FirebaseDatabase.getInstance();
  60.         DatabaseReference myRef = database.getReference(chatNode);
  61.         myRef.addValueEventListener(new ValueEventListener() {
  62.             @Override
  63.             public void onDataChange(DataSnapshot dataSnapshot) {
  64.                 messageHold=dataSnapshot.getValue(String.class);
  65.                 Toast.makeText(context, messageHold, Toast.LENGTH_SHORT).show();
  66.                 msgList.add(messageHold);
  67.                 //display in listView
  68.                 MsgAdapter msgAdapter=new MsgAdapter(context,msgList);
  69.                 myMsgList.setAdapter(msgAdapter);
  70.             }
  71.  
  72.             @Override
  73.             public void onCancelled(DatabaseError databaseError) {
  74.                 Toast.makeText(context, "Error accord", Toast.LENGTH_SHORT).show();
  75.             }
  76.         });
  77.  
  78.     }
  79.  
  80.  
  81. }
  82.  
  83.  
  84. activity_main.xml
  85. ====================
  86. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  87.     android:layout_width="match_parent"
  88.     android:layout_height="match_parent"
  89.     android:orientation="vertical">
  90.  
  91.     <EditText
  92.         android:layout_width="match_parent"
  93.         android:layout_height="wrap_content"
  94.         android:hint="Enter Message...."
  95.         android:id="@+id/helloMsg"/>
  96.     <Button
  97.         android:layout_width="match_parent"
  98.         android:layout_height="wrap_content"
  99.         android:text="SEND MESSAGE..."
  100.         android:onClick="sendMsg"
  101.         android:background="#009fff"
  102.         android:textColor="#fff" />
  103.     <ListView
  104.         android:layout_width="match_parent"
  105.         android:layout_height="match_parent"
  106.         android:id="@+id/msgList"></ListView>
  107. </LinearLayout>
  108.  
  109. PROJECT GRADLE
  110. ==================================
  111. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  112.  
  113. buildscript {
  114.     repositories {
  115.         jcenter()
  116.     }
  117.     dependencies {
  118.         classpath 'com.android.tools.build:gradle:2.2.2'
  119.         classpath 'com.google.gms:google-services:3.0.0'
  120.         // NOTE: Do not place your application dependencies here; they belong
  121.         // in the individual module build.gradle files
  122.     }
  123. }
  124.  
  125. allprojects {
  126.     repositories {
  127.         jcenter()
  128.     }
  129. }
  130.  
  131. task clean(type: Delete) {
  132.     delete rootProject.buildDir
  133. }
  134.  
  135.  
  136. APP GRADLE
  137. =====================
  138. apply plugin: 'com.android.application'
  139.  
  140. android {
  141.     compileSdkVersion 25
  142.     buildToolsVersion "25.0.0"
  143.     defaultConfig {
  144.         applicationId "com.example.zeevm.mygroupchat"
  145.         minSdkVersion 19
  146.         targetSdkVersion 25
  147.         versionCode 1
  148.         versionName "1.0"
  149.         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  150.     }
  151.     buildTypes {
  152.         release {
  153.             minifyEnabled false
  154.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  155.         }
  156.     }
  157. }
  158.  
  159. dependencies {
  160.     compile fileTree(dir: 'libs', include: ['*.jar'])
  161.     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  162.         exclude group: 'com.android.support', module: 'support-annotations'
  163.     })
  164.     compile 'com.android.support:appcompat-v7:25.0.0'
  165.     testCompile 'junit:junit:4.12'
  166.     compile 'com.google.firebase:firebase-core:9.0.2'
  167.     compile 'com.google.firebase:firebase-database:9.0.2'
  168. }
  169. apply plugin: 'com.google.gms.google-services'
  170.  
  171.  
  172.  
  173. manifest.xml
  174. ================
  175. <?xml version="1.0" encoding="utf-8"?>
  176. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  177.     package="com.example.zeevm.mygroupchat">
  178.  
  179.     <uses-permission android:name="android.permission.INTERNET"/>
  180.  
  181.     <application
  182.         android:allowBackup="true"
  183.         android:icon="@mipmap/ic_launcher"
  184.         android:label="@string/app_name"
  185.         android:supportsRtl="true"
  186.         android:theme="@style/AppTheme">
  187.         <activity android:name=".MainActivity">
  188.             <intent-filter>
  189.                 <action android:name="android.intent.action.MAIN" />
  190.  
  191.                 <category android:name="android.intent.category.LAUNCHER" />
  192.             </intent-filter>
  193.         </activity>
  194.     </application>
  195.  
  196. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement