I have 2 android apps connected to the same Firebase database, and I want to add a button in the first app to hide another button in the inside the second app.
This is my code on the first app which hides 'btnact' depending on the status of the settings in Remote Config of Firebase:
myconfiguration=FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configuratonsettings = new FirebaseRemoteConfigSettings.Builder().build();
myconfiguration.setConfigSettings(configuratonsettings);
Map<String,Object> defaultvalues = new HashMap<>();
defaultvalues.put("btn_enable",false);
myconfiguration.setDefaults(defaultvalues);
fetcher.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myconfiguration.fetch(0).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
myconfiguration.activateFetched();
btnact.setEnabled(myconfiguration.getBoolean("btn_enable"));
}
else
{
Toast.makeText(MainActivity.this, "Something went Wrong\nPlease try again", Toast.LENGTH_SHORT).show();
}
}
});
}
});
I want a way to change this 'btnact' to the id of the button of the other app.. Can anyone have a clue for it?
You can fetch the value of the button you want to hide (boolean value: false or true) inside your second app, then set that button visibility to invisible or gone based on the fetched boolean value.
You can change the value that controls your second app's enabled status/visibility inside your first app.
I don't test this right now, but I think it should work and it is easy to accomplish what you want.
Related
Firebase user.currentuser() is always null in oncreate. I want to implement this access controll so that only a user called a credit controller can add items to the recyclerview using the floating action button, while other users can only see the items added.
I want the floating action button's visibility to be determined as soon as the activity is open. Is there any other way to achieve this?
I used the reveal button to achieve this but it's a very bad user experience to click it every time you want to go into this activity.
This is my code below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collections);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
userEmail = user.getEmail();
FloatingActionButton buttonAddNote = findViewById(R.id.button_add_note);
Button revealButton = findViewById(R.id.revealButton);
revealButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(userEmail.equals("creditcontroller#outlook.com")){
buttonAddNote.setVisibility(View.VISIBLE);
revealButton.setVisibility(View.GONE);
}
else revealButton.setVisibility(View.GONE);
}
});
buttonAddNote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(CollectionsActivity.this, AddEditNoteActivity.class);
intent.putExtra("USER_EMAIL", userEmail);
startActivityForResult(intent, ADD_NOTE_REQUEST);
}
});
Unfortunately I can't upload images yet but the reveal button is just a button that's centered in the activity. Upon clicking it its visibility is set to gone and everything else is set to visible
The code is working but the user experience is pretty bad.
Firebase calls are asynchronous which means that you can't get results instantly.
You should add a callback to your calls.
FirebaseUser user = firebaseAuth.getCurrentUser();
userEmail = user.getEmail();
database.child(userEmail).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String email = dataSnapshot.child("email").getValue(String.class);
Log.d(TAG, "email: " + email);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
The code is just an example. It may not work in your case, because you must know the structure of your nodes.
You can read more in the Firebase documentation: https://firebase.google.com/docs/database/android/read-and-write
Hello everyone I am making an android application with the help of Firebase. The application is working fine but ever since I tried to add the functionality of a favorite button, I am unable to get the bug out of that "add favorite" part of the code.
current_state="not av"
The above part states the current favorite state of the user.
The below code is for "add favorite" part.
Holder.mFavourites.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (current_state.equals("not fav")){
mFav.child(puid).child(kk).child("fav_status").setValue("Added as " +
"fav").addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Holder.mFavourites.setImageDrawable(getContext().getDrawable(R.drawable.ic_star_black));
current_state = "fav";
Toast.makeText(getContext(),"Added to favourites",Toast
.LENGTH_SHORT).show();
}
});
}else if (current_state.equals("fav")){
mFav.child(puid).child(kk).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()){
Holder.mFavourites.setImageDrawable(getContext().getDrawable(R.drawable.ic_star_border_black_24dp));
current_state = "not fav";
Toast.makeText(getContext(),"Removed from favourites",Toast
.LENGTH_SHORT).show();
}
}
});
}
}
});
I am getting a problem with the above code-
1. Whenever the first time user click on the code. He gets added to favourite and if he click again on the same button then he is removed from the favourites, but if the user after clicking once (i.e. after getting added as favourites)goes on and click on the add to fav button again,then the toast of removed from favorites appears regardless of whether the user is in favorites database or not.
Is current_state a class variable that equals "not fav"? Cause it looks like your if statement is just reading a string and doesnt actually know whats going on in the database. What if you query all the favs into an arraylist and test your if statement against the arraylist
i am implementing Fire-Base Authentication in my app, With SignUp and SignIn, however i am having a problem, every time i fill the form and click on the SignUp button the Fire-Base gives back an error even though the account was successfully created. My code for the SignUp method:
public class Signup extends AppCompatActivity {
EditText input_email,input_pass;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
//View
input_email = (EditText)findViewById(R.id.id_email);
input_pass = (EditText)findViewById(R.id.id_pass);
//Init Firebase
auth = FirebaseAuth.getInstance();
}
public void iLogin (View v){
startActivity(new Intent(Signup.this, Login.class));
finish();
}
public void iForgot(View v){
//startActivity(new Intent(Signup.this, ForgotPassword.class));
finish();
}
public void iSignUp(View v){
signUpUser(input_email.getText().toString(),input_pass.getText().toString());
}
private void signUpUser(String email, String password) {
auth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful())
{
Toast.makeText(Signup.this, "Successfully created your account", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Signup.this, "ERROR has occurred", Toast.LENGTH_LONG).show();
}
}
});
}
}
So what happens is:
1- The user Opens the app for the first time, the app opens mainly on the MainActivity.class which then redirects the user to the SignUp.class (The one i have added above).
2- The user then fills two fields: (Email) and (Password) and clicks on the SignUp button
3- The SignUp.class (The one i have added above) then creates a new account and shows the Toast: ERROR has occurred, even though the account has been created successfully. In order for the class to display the correct Toast (Successfully created your account) i need to press on the button a second time.
So just to be clear: The first time you click, the account is created and i can see it in Fire-Base Console but the app still gives the wrong Toast, for it to show the correct one i need to click it again, Any thoughts on what's wrong with the code? Thanks in advance!
Just change:
if(!task.isSuccessful())
to:
if(task.isSuccessful())
and it should work. There is a success when task.isSuccessful() is true, so the ! sign make it false. That's why it works for the second time.
I have a key 'newUser' within my firebase database, which can be set to either 'true' or 'false', as shown here:
firebase database structure click here for screenshot
I have referenced to the database within a LoginActivity class, upon clicking a login button the userLogin method is called, within this method I perform the following:
mDatabase.orderByChild("email").equalTo(email)
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot: dataSnapshot.getChildren()){
String newUser = singleSnapshot.getValue(User.class).newUser;
if(newUser.equals("true")){
Log.d("Yes!", "this is a new user");
startnewActivity(singleSnapshot);
}else {
Log.d("No!", "this is an old user");
startnewActivity(singleSnapshot);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Note that equalTo(email) ... email is a local variable which grabs the text within the textfield where the user inputs the email.
the startnewActivity method is defined below, to initialise the activities dependent on whether the user is new or not:
private void startnewActivity(DataSnapshot singleSnapshot) {
String s = singleSnapshot.getValue(User.class).newUser;
if (s.equals("true")) {
Intent i = new Intent(LoginActivity.this, AudiometryNewUser.class);
singleSnapshot.getRef().child("newUser").setValue("false");
Log.d("iterated!?", "");
startActivity(i);
return;
}
else if(s.equals("false")){
Intent i = new Intent(LoginActivity.this, MainDashboard.class);
Log.d("iterated", "");
startActivity(i);
}
}
my intention is when the user is a newUser = true, then they will be taken to the welcome screen AudiometryNewUser, and for the newUser value to adopt "false".
The result however is that the user is ALWAYS taken to the dashboard, even if they are a new user. If they were a newUser and logged in, upon clicking the back button the user is navigated to the expected welcome new user activity.
on top of this, several instances of the activity are sometimes launched.
I just wondered if anyone could explain why this was happening, and what am I failing to see?
If clarity is needed please say so, I'll do my best to update the post.
Many thanks in advance.
From experimentation and further reading:
The addValueEvent listener is called for every value changed, therefore when setting the singleSnapShot to false this was in affect changing the value, thus the listener was being called again. Instead the correct approach was to user a Listener for a single value event.
When swapped for this method, the solution works as expected.
I want to use custom function in my android application for sign up in firebase. Currently I am using the createUserWithEmailAndPassword function. It takes two parameters by default i.e Email & Password but my sign up form includes other attributes also like, phone number, name etc. So what should I do? Currently I am using the below code.
firebaseAuth.createUserWithEmailAndPassword(Email,Password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
progressDialog.cancel();
Toast.makeText(SignUp.this,"Registration Successful",Toast.LENGTH_SHORT).show();
}
else
{
progressDialog.cancel();
Toast.makeText(SignUp.this,"Could Not Register. Try Again.",Toast.LENGTH_SHORT).show();
}
}
});
Thank You :)