Unable to see the data retrived from the firebase in the textview - android

It is kind of a chatting feature in an application where users with different ids send msg and it is stored in the database in realtime.
I have stored the data into the realtime database but when i try to retrive and display it is not displayed.I am not sure if the data is getting retrived in the first place.
I have tried storing the data through the application and it is being stored in the firebase database.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".chatroom">
<Button
android:id="#+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="9dp"
android:layout_marginLeft="9dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="8dp"
android:text="SEND"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/text_send" />
<EditText
android:id="#+id/text_send"
android:layout_width="286dp"
android:layout_height="49dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/send_button"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/msgview"
android:layout_width="390dp"
android:layout_height="699dp"
android:layout_marginEnd="3dp"
android:layout_marginRight="3dp"
android:text="look here"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code:
package com.example.sdl;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.ChildEventListener;
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 java.util.Iterator;
public class chatroom extends AppCompatActivity {
FirebaseDatabase database=FirebaseDatabase.getInstance();
Button send_button;
EditText text_send;
TextView msgview;
String ctext_send,cmsgview;
String sdomain,ssubdomain,sprofession,sbranch,srollno,spass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatroom);
SharedPreferences sp = getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
sdomain=sp.getString("domain"," xyz");
ssubdomain=sp.getString("subdomain","xyz ");
sprofession=sp.getString("profession"," xyz");
sbranch=sp.getString("branch","xyz ");
srollno=sp.getString("rollno","xyz ");
spass=sp.getString("pass"," xyz");
send_button =(Button) findViewById(R.id.send_button);
send_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openchat();
}
});
msgview=(TextView) findViewById(R.id.msgview);
text_send=(EditText) findViewById(R.id.text_send);
DatabaseReference ref = database.getReference().child(sdomain).child(ssubdomain).child(sbranch).child("chat");
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, String s) {
append_chat_conv(dataSnapshot);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, String s) {
append_chat_conv(dataSnapshot);
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private String chatmsg;
private void append_chat_conv(DataSnapshot dataSnapshot){
Iterator i=dataSnapshot.getChildren().iterator();
while (i.hasNext()){
chatmsg= (String) ((DataSnapshot)i.next()).getValue();
msgview.append(chatmsg);
}
}
public void openchat(){
ctext_send=text_send.getText().toString();
Task<Void> ref=database.getReference().child(sdomain).child(ssubdomain).child(sprofession).child(sbranch).child("chat").push().child(srollno).setValue(ctext_send);
}
}

Related

RecyclerView is not displaying database contents as expected

Here's the data in my database:
Here's the code of my project. I found no error. It is not displaying the contents in it, even it is showing a blank page.
Adapter
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import java.util.ArrayList;
public class MylistAdapter extends RecyclerView.Adapter<MylistAdapter.MylistViewHolder> {
private ArrayList<RequestUser> users;
class MylistViewHolder extends RecyclerView.ViewHolder{
private TextView dispname1;
private TextView dispphn1;
private TextView dispcity1;
private TextView dispaddr1;
private TextView dispnumber1;
private MylistViewHolder(#NonNull View itemView) {
super(itemView);
dispaddr1=itemView.findViewById(R.id.dispaddr);
dispcity1=itemView.findViewById(R.id.dispcity);
dispname1=itemView.findViewById(R.id.dispname);
dispphn1=itemView.findViewById(R.id.disphn);
dispnumber1=itemView.findViewById(R.id.dispnumber);
}
}
public MylistAdapter(ArrayList<RequestUser> usrs) {
this.users= usrs;
}
#Override
public MylistViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
MylistViewHolder mlvh=new MylistViewHolder(v);
return mlvh;
}
#Override
public void onBindViewHolder(#NonNull MylistViewHolder holder, int position) {
RequestUser curuser=users.get(position);
holder.dispphn1.setText(curuser.getRphn1());
holder.dispname1.setText(curuser.getRname1());
holder.dispcity1.setText(curuser.getRcity1());
holder.dispaddr1.setText(curuser.getRaddr1());
holder.dispnumber1.setText(curuser.getRnumber1());
}
#Override
public int getItemCount() {
return users.size();
}
}
Main Class
Which contains the main functionality
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.Objects;
public class Transport extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
DatabaseReference dbreferance;
FirebaseAuth firebaseAuth;
FirebaseDatabase firebaseDatabase;
private ArrayList<RequestUser> usrs=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
firebaseAuth=FirebaseAuth.getInstance();
firebaseDatabase=FirebaseDatabase.getInstance();
dbreferance= FirebaseDatabase.getInstance().getReference().child("Donate");
dbreferance.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
usrs.clear();
for(DataSnapshot child:dataSnapshot.getChildren())
{
//String id=child.getKey();
RequestUser usr=child.getValue(RequestUser.class);
usrs.add(usr);
//RequestUser usr= (RequestUser) child.getValue();
//usrs.add(usr);
//System.out.println(usr.rname1);
Toast.makeText(Transport.this,usr.getRphn1(),Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
adapter=new MylistAdapter(usrs);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
XML Layouts
for the Recycler view and the card views
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/dispname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line1"
android:textColor="#android:color/black"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/dispcity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dispname"
android:textSize="25sp"
android:layout_marginStart="8dp"
android:text="Line2"
android:layout_marginLeft="8dp" />
<TextView
android:id="#+id/dispaddr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dispcity"
android:textSize="15sp"
android:text="Line3"
android:layout_marginLeft="16dp"/>
<TextView
android:id="#+id/disphn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dispaddr"
android:textSize="15sp"
android:text="Line4"
android:layout_marginLeft="24dp"/>
<TextView
android:id="#+id/dispnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dispaddr"
android:textSize="15sp"
android:text="Line5"
android:layout_marginLeft="24dp"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Another Layout
ie for card view
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Transport">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints" />
</RelativeLayout>
When we add listener Android create a separate thread for that and that thread run separately. So in your case we you are adding the listener android is creating the separate thread for that and then it immediately set the adapter for the recyclerView.
Create the adapter before adding the listener and set the adapter with recyclerView in the onDataChanged( ) method after the for loop.
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
usrs.clear();
for(DataSnapshot child:dataSnapshot.getChildren())
{
//String id=child.getKey();
RequestUser usr=child.getValue(RequestUser.class);
usrs.add(usr);
//RequestUser usr= (RequestUser) child.getValue();
//usrs.add(usr);
//System.out.println(usr.rname1);
Toast.makeText(Transport.this,usr.getRphn1(),Toast.LENGTH_SHORT).show();
}
recyclerView.setAdapter(adapter);
}

"Unfortunately, ROC has stopped." When code to retrive data from firebase(Login). NoActionBar Solution also not working

When I jump from MainActivity to AdminLog it works when adminLog.java contains the code as shown below. I have also searched for solution Like enabling noAction bar but it also doen't work. Not only for login code but it is shows same issue when I try to retrieve data from my firebase database.
package com.example.roc;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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;
public class adminLog extends AppCompatActivity {
EditText txtEmail, txtPassword;
ProgressBar progressBar;
Button btnSignin;
private FirebaseAuth mAuth;
private Button mgo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_log);
mgo=(Button)findViewById(R.id.adminLoginBtn);
mgo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(adminLog.this,adminWorking.class));
}
});
}
}
But it stops Working when I add login code in adminLog.java file which is also shown below.
package com.example.roc;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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;
public class adminLog extends AppCompatActivity {
EditText txtEmail, txtPassword;
ProgressBar progressBar;
Button btnSignin;
private FirebaseAuth mAuth;
private Button mgo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_log);
mAuth = FirebaseAuth.getInstance();
initializeUI();
btnSignin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loginUserAccount();
}
});
}
private void loginUserAccount() {
progressBar.setVisibility(View.VISIBLE);
String email, password;
email = txtEmail.getText().toString();
password = txtPassword.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Please enter email...", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Please enter password!", Toast.LENGTH_LONG).show();
return;
}
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Login successful!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
Intent intent = new Intent(adminLog.this, adminWorking.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "Login failed! Please try again later", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
private void initializeUI() {
txtEmail= findViewById(R.id.adminETxt);
txtPassword = findViewById(R.id.adminPTxt);
btnSignin = findViewById(R.id.adminLoginBtn);
progressBar = findViewById(R.id.progressBar);
}
}
adminlog.xml file code is also given below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".adminLog">
<Button
android:id="#+id/adminLoginBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="132dp"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/adminPTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/customerLoginBtn"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="96dp"
android:hint="Enter your password"
app:layout_constraintBottom_toTopOf="#+id/adminLoginBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/adminETxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginTop="332dp"
android:hint="Entere Your E-Mail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="133dp"
android:layout_height="119dp"
android:layout_marginStart="139dp"
android:layout_marginLeft="139dp"
android:layout_marginTop="164dp"
android:layout_marginEnd="139dp"
android:layout_marginRight="139dp"
android:layout_marginBottom="448dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
</android.support.constraint.ConstraintLayout>
What should I do to get rid of this problem.

android:inputType does not function after adding caligraphy

After adding calligraphy to applied the font, password become blurred and cannot be viewed. I have applied android:fontFamily="sans-serif" but still it is still the same. Calligraphy that i am using is
implementation uk.co.chrisjenx:calligraphy:2.3.0, while my sdkVersion is 21, library is com.android.support:appcompat-v7:27.1.1
signin.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_borenos"
tools:context="com.example.liew.idelivery.SignIn">
<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/edtPhone"
android:hint="Phone Number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#android:color/white"
android:text="0128688032"
android:textColor="#android:color/white"
android:textSize="34sp"
android:inputType="phone"
app:met_baseColor="#android:color/white"
app:met_floatingLabel="highlight"
app:met_maxCharacters="12"
app:met_primaryColor="#android:color/white"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/edtPassword"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#android:color/white"
android:text="chau95"
android:fontFamily="sans-serif"
android:textColor="#android:color/white"
android:textSize="34sp"
android:inputType="textPassword"
app:met_baseColor="#android:color/white"
app:met_floatingLabel="highlight"
app:met_maxCharacters="12"
app:met_primaryColor="#android:color/white"
app:passwordToggleEnabled="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<com.rey.material.widget.CheckBox
android:id="#+id/ckbRemember"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Remember me"
android:textColor="#android:color/white"
app:cbd_strokeColor="#android:color/white"
app:cbd_tickColor="#android:color/black"
style="#style/Material.Drawable.CheckBox"
android:gravity="center_vertical"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#android:color/white"
android:id="#+id/txtForgotPwd"
android:text="#string/forgot_pwd"/>
</LinearLayout>
</LinearLayout>
<info.hoang8f.widget.FButton
android:id="#+id/btnSignIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_alignParentBottom="true"
android:text="Sign In"
android:textColor="#android:color/white"
app:fButtonColor="#color/btnSignUp"
app:cornerRadius="4dp"
app:shadowColor="#android:color/black"
app:shadowEnabled="true"
app:shadowHeight="5dp" />
</RelativeLayout>
SignIn.java
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 com.rengwuxian.materialedittext.MaterialEditText;
import com.rey.material.widget.CheckBox;
import io.paperdb.Paper;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
public class SignIn extends AppCompatActivity {
EditText edtPhone,edtPassword;
Button btnSignIn;
CheckBox ckbRemember;
TextView txtForgotPwd;
FirebaseDatabase database;
DatabaseReference table_user;
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//add calligraphy
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/restaurant_font.otf")
.setFontAttrId(R.attr.fontPath)
.build());
setContentView(R.layout.activity_sign_in);
edtPhone = (MaterialEditText)findViewById(R.id.edtPhone);
edtPassword = (MaterialEditText)findViewById(R.id.edtPassword);
btnSignIn = (Button)findViewById(R.id.btnSignIn);
ckbRemember = (CheckBox)findViewById(R.id.ckbRemember);
txtForgotPwd = (TextView)findViewById(R.id.txtForgotPwd);
//Init paper
Paper.init(this);
//Init firebase
database =FirebaseDatabase.getInstance();
table_user = database.getReference("User");
txtForgotPwd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showForgotPwdDialog();
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Common.isConnectedToInternet(getBaseContext())) {
//save user & password
if(ckbRemember.isChecked()){
Paper.book().write(Common.USER_KEY, edtPhone.getText().toString());
Paper.book().write(Common.PWD_KEY, edtPassword.getText().toString());
}
final ProgressDialog mDialog = new ProgressDialog(SignIn.this);
mDialog.setMessage("Please waiting...");
mDialog.show();
table_user.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//check if user not exist in database
if (dataSnapshot.child(edtPhone.getText().toString()).exists()) {
//Get user information
mDialog.dismiss();
User user = dataSnapshot.child(edtPhone.getText().toString()).getValue(User.class);
// set phone
user.setPhone(edtPhone.getText().toString());
if (user.getpassword().equals(edtPassword.getText().toString())) {
Intent homeIntent = new Intent(SignIn.this, Home.class);
Common.currentUser = user;
startActivity(homeIntent);
finish();
table_user.removeEventListener(this);
} else {
Toast.makeText(SignIn.this, "Wrong Password!", Toast.LENGTH_SHORT).show();
}
} else {
mDialog.dismiss();
Toast.makeText(SignIn.this, "User not exist!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
else{
Toast.makeText(SignIn.this, "Please check your internet connection!", Toast.LENGTH_SHORT).show();
return;
}
}
});
}
private void showForgotPwdDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Forgot Password");
builder.setMessage("Enter your secure code");
LayoutInflater inflater = this.getLayoutInflater();
View forgot_view = inflater.inflate(R.layout.forgot_password_layout, null);
builder.setView(forgot_view);
builder.setIcon(R.drawable.ic_security_black_24dp);
final MaterialEditText edtPhone = (MaterialEditText)forgot_view.findViewById(R.id.edtPhone);
final MaterialEditText edtSecureCode = (MaterialEditText)forgot_view.findViewById(R.id.edtSecureCode);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//check if user still available
table_user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.child(edtPhone.getText().toString())
.getValue(User.class);
if (user.getSecureCode().equals(edtSecureCode.getText().toString()))
Toast.makeText(SignIn.this, "Your password: "
+user.getpassword(), Toast.LENGTH_LONG).show();
else
Toast.makeText(SignIn.this, "Wrong secure code!", Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
}
EditTextPassword.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);

How to make a TextView swipe-able and display a second TextView in it's place

I have 5 different TextView's on a page and I want to make them swipe-able so that when I swipe them all to the left it will display the same information but with different values (E.g. Name = Ben, but when swiped left Name = Tim), is this possible? If so how would I go about doing it? The information for the TextView's are pulled from a Firebase database but can be changed to static values if needs be.
This is what I have so far:
CalendarActivity:
package com.example.adamf.authtest;
import android.content.Intent;
import android.renderscript.Allocation;
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.CalendarView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 org.w3c.dom.Text;
import android.text.util.Linkify;
import java.util.GregorianCalendar;
public class CalendarActivity extends AppCompatActivity implements View.OnClickListener {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mUserRef = mRootRef.child(uid).child("2018-04-23").child("Jane Jenkins");
DatabaseReference mNameRef = mUserRef.child("Name");
DatabaseReference mAddressRef = mUserRef.child("Address");
DatabaseReference mPostCodeRef = mUserRef.child("PostCode");
DatabaseReference mMobileRef = mUserRef.child("Mobile");
DatabaseReference mTimeRef = mUserRef.child("Time");
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private Button InformationBtn = null;
TextView mName;
TextView mAddress;
TextView mPostCode;
TextView mMobile;
TextView mTime;
#Override
protected void onDestroy() {
super.onDestroy();
mAuth.signOut();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.buttonSignOut) {
startActivity(new Intent(CalendarActivity.this, LoginActivity.class));
FirebaseAuth.getInstance().signOut();
Toast.makeText(CalendarActivity.this, "Sign out successful", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "Please use the Sign Out button above", Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar_v2);
mName = findViewById(R.id.textViewName);
mAddress = findViewById(R.id.textViewAddress);
mPostCode = findViewById(R.id.textViewPostcode);
mMobile = findViewById(R.id.textViewMobile);
mTime = findViewById(R.id.textViewTime);
InformationBtn = findViewById(R.id.buttonInformation);
InformationBtn.setOnClickListener(this);
mAuth = FirebaseAuth.getInstance();
Linkify.addLinks(mMobile, Linkify.PHONE_NUMBERS);
Linkify.addLinks(mAddress, Linkify.MAP_ADDRESSES);
InformationBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(CalendarActivity.this, ClientInfo.class));
}
});
CalendarView view = new CalendarView(this);
view.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
public GregorianCalendar calendar;
#Override
public void onSelectedDayChange(CalendarView calendarView, int year, int month, int day) {
this.calendar = new GregorianCalendar(year, month, day);
}
});
}
protected void onStart() {
super.onStart();
mNameRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
mName.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mAddressRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
mAddress.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mPostCodeRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
mPostCode.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mMobileRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
mMobile.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mTimeRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text = dataSnapshot.getValue(String.class);
mTime.setText(text);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onClick(View v) {
}
}
activity_calendar_v2.xml (layout file):
<?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"
xmlns:tools="http://schemas.android.com/tools">
<CalendarView
android:id="#+id/calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:minWidth="400dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textName"
android:layout_below="#+id/calendar"
android:text="Name of patient:"
android:layout_marginTop="20dp"
android:layout_marginStart="30dp"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewName"
android:layout_toEndOf="#+id/textName"
android:layout_alignBottom="#+id/textName"
android:text=" "
android:textSize="12dp"
android:layout_marginStart="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textAddress"
android:layout_below="#+id/textName"
android:text="Patients address:"
android:layout_marginTop="10dp"
android:layout_marginStart="30dp"
android:autoLink="map"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewAddress"
android:text=" "
android:textSize="12dp"
android:layout_alignStart="#+id/textViewName"
android:layout_alignBottom="#+id/textAddress"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textPostcode"
android:layout_below="#+id/textAddress"
android:text="Postcode:"
android:layout_marginTop="10dp"
android:layout_marginStart="30dp"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewPostcode"
android:text=" "
android:textSize="12dp"
android:layout_alignStart="#+id/textViewName"
android:layout_alignBottom="#+id/textPostcode"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textMobile"
android:layout_below="#+id/textPostcode"
android:text="Mobile number:"
android:layout_marginTop="10dp"
android:layout_marginStart="30dp"
android:autoLink="phone"
android:linksClickable="true"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewMobile"
android:text=" "
android:textSize="12dp"
android:layout_alignStart="#+id/textViewName"
android:layout_alignBottom="#+id/textMobile"
android:autoLink="phone"
android:linksClickable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textTime"
android:layout_below="#+id/textMobile"
android:text="Appointment time:"
android:layout_marginTop="10dp"
android:layout_marginStart="30dp"
android:textSize="14dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewTime"
android:text=" "
android:textSize="12dp"
android:layout_alignStart="#+id/textViewName"
android:layout_alignBottom="#+id/textTime"/>
<Button
android:id="#+id/buttonInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textTime"
android:text="View Patient Information"
android:textSize="12dp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Will I also have to put all the TextView's inside of something to group them all together?
Thanks

How to restrict the duplicate entries in the Firebase Database

In my app my user is entering the category and that category is stored in the FirebseDB. After storing the value in the DB the user can view the inserted value in the Spinner.
At this point all works fine.
I want user to enter only unique value .so , if user wants to enter the value which is already there he should get a Toast.
Following is my activity_main.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.baicactivitytest.MainActivity">
<Spinner
android:id="#+id/sp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="#+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="Add" />
</RelativeLayout>
Following is my inputdialogue.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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="50dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/nameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint= "Name" />
</android.support.design.widget.TextInputLayout>
<Button android:id="#+id/saveBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"
android:clickable="true"
android:background="#color/colorAccent"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
</LinearLayout>
</LinearLayout>
Spacecraft.java
package com.example.lenovo.baicactivitytest;
/**
* Created by LENOVO on 29-12-2017.
*/
public class Spacecraft {
String name;
public Spacecraft() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
FirebaseHelper
package com.example.lenovo.baicactivitytest;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseException;
import com.google.firebase.database.DatabaseReference;
import java.util.ArrayList;
/**
* Created by LENOVO on 29-12-2017.
*/
public class FirebaseHelper {
DatabaseReference db;
Boolean saved = null;
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//SAVE
public Boolean save(Spacecraft spacecraft)
{
if(spacecraft==null)
{
saved=false;
}else
{
try
{
db.child("Spacecraft").push().setValue(spacecraft);
saved=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}
return saved;
}
//READ
public ArrayList<String> retrieve()
{
final ArrayList<String> spacecrafts=new ArrayList<>();
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot,spacecrafts);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot,spacecrafts);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return spacecrafts;
}
private void fetchData(DataSnapshot snapshot,ArrayList<String> spacecrafts)
{
spacecrafts.clear();
for (DataSnapshot ds:snapshot.getChildren())
{
String name=ds.getValue(Spacecraft.class).getName();
spacecrafts.add(name);
}
}
}
MainActivity.java
package com.example.lenovo.baicactivitytest;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity {
DatabaseReference db;
FirebaseHelper helper;
private Button madd_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner sp =(Spinner) findViewById(R.id.sp);
//SETUP FB
db= FirebaseDatabase.getInstance().getReference();
helper = new FirebaseHelper(db);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,helper.retrieve());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
madd_btn = (Button) findViewById(R.id.add_btn);
madd_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
displayInputDialog();
}
});
}
//DISPLAY INPUT DILAOG
private void displayInputDialog()
{
Dialog d=new Dialog(this);
d.setTitle("Firebase database");
d.setContentView(R.layout.inputdialog);
final EditText nameTxt= (EditText) d.findViewById(R.id.nameEditText);
Button saveBtn = (Button) d.findViewById(R.id.saveBtn);
//SAVE
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//GET DATA
String name=nameTxt.getText().toString();
//set data
Spacecraft s = new Spacecraft();
s.setName(name);
//SAVE
if(name != null && name.length()>0)
{
if(helper.save(s))
{
nameTxt.setText("");
}
}else
{
Toast.makeText(MainActivity.this, "Name Cannot Be Empty", Toast.LENGTH_SHORT).show();
}
}
});
d.show();
}
}
Can do this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Category");
ref.orderByChild("categoryname").equalTo(name).addValueEventListener(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exist()) {
Toast.makeText(MainActivity.this, "Name already exists", Toast.LENGTH_SHORT).show();
}
}
Can do the above, it will search in the Category node in the db, and if it exists then it will give you the Toast message.

Categories

Resources