How to get the current user profile image and name from Firebase? - java

Below is my Firebase structure image,
Firebase Structure
I will like to use the name and profile image of user for my Second Activity.
Here how I did for my another app,
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
String name = user.getDisplayName();
And I later used the name in my TextView to use the current user name but here it didn't worked and besides I also needed the profile image too.
So, How do I get the user image and name from above Firebase structure image to my Second activity layout TextView and ImageView and set the default text name and image if it returns null.
I tried below but it didn't worked to
public void setUserData(String name, String image) {
ImageView userimage = (ImageView) findViewById(R.id.imageView);
userimage.setImageURI(userimage);
}

Your url is a string not Uri. So, you need to fetch the image from the given url.
You can use AsyncTask like:
link
or use Glide library to make async calls. Glide

get userRecord by calling getUserAsync(String uid)
https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/auth/UserRecord

Related

How to get drawable name from imageView

I have an app that create a random image when the activity opens. There are like 30 images in the random image array. The image is generated, but what I want is to write the drawable name into a textview. It looks like this:
ImageView RImage= (ImageView) findViewById(R.id.image);
RImage.setImageResource(generator());
Drawable myDrawable = RImage.getDrawable();
TextView writeID = (TextView) findViewById(R.id.idtext);
writeID.setText(String.valueOf(generator()));
}
private int generator() {
TypedArray imgs = getResources().obtainTypedArray(R.array.list3);
int imgid = imgs.getResourceId(new Random().nextInt(imgs.length()), -1);
imgs.recycle();
return imgid;
}
Currently I receive numbers, and I cant figure out how can I convert the int to a string, or how can I use the generator to generate a string, display in the textview and then convert it to int to display it in the imageview.
I created an another activity where I can pick the image I want to see from a spinner, it simply send the selected option with string to the next activity on button press, and in the other activity i remove the .png and res/drawable/ from the string, and convert the string into an int to display the image.
Bundle extras = getIntent().getExtras();
String transportItemChosen = extras.getString("SpinnerValue");
transportItemChosen = transportItemChosen.replace(".png", "");
transportItemChosen = transportItemChosen.replace("res/drawable/", "");
String uri = ("#drawable/" + transportItemChosen);
int id = getResources().getIdentifier(uri, null, getPackageName());
ImageView mImageView;
mImageView = (ImageView) findViewById(R.id.selectedimage);
mImageView.setImageResource(id);
So I need the generator to generate to string and then convert it to int. Thanks!
Change your code by converting your int ids into String names by Using the method
getResourceEntryName(int id);
Change the line:
writeID.setText(String.valueOf(generator()));
To Something like:
writeID.setText(getResources().getResourceEntryName(generator()));
And for Viceversa:
And if you are having a String lets assume this String you get from a textView or using above method to convert it back to an int id you do the following:
int id= getResources().getIdentifier("your_string_here", "drawable", getPackageName());
And if you are calling the code from a Fragment not an Activity remember to put getActivity() before calling getResources()or before calling getPackageName().
I have not tested the code as I am typing, but I am sure it will work (may be a typo) for more information consider visting the Documentation on Resources from this official link.
If I understand you correctly, what you need is to take a look at getResourceName(int) method - it returns resource name by its id. Also you can use getIdentifier(String,String,String) method to retrieve resource id by its name.
If you want to display name of the randomly picked image, you should save the generated resource id into the variable and get corresponding resource name for it instead of calling generator() method twice as you'll get two different ids.

Android ID and ImageView

I am making an Android app of Checkers based on XML widget (like imageview).
I made a table of 8X8 of imageviews and call their id like so (i00 , i01 etc).
I have two questions:
I've tried to make a reference of the imageviews in the code like this:
ImageView img = (ImageView) findViewById(R.id.i00);
and it gives me an error:
"i00 cannot be resolved or is not a field"
Is there any way of getting id by a string?
For example I have a imageview which id is i01, can I get it by:
String str = "i01"
and then give str as a parameter for some kind of method?
1: That call is correct, check this thread which talks further about that error: “id cannot be resolved or is not a field” error
2: Something like this should work:
String str = "i02";
int id = getResources().getIdentifier(str, "id", getPackageName());
ImageView img = (ImageView) findViewById(id);
Check this link for more information findViewById with String

Iterate through all TextViews in a ListView

Okay, I'm searching and searching for the solution.
I'm using Firebase, but don't know if its neccessary for you to know. When I edit something on the internet on Firebase, my Android app gets a callback that something got edited. So now, I have to edit it also from my app.
So, now I need to search a TextView in the Layout that a certain identifier. It's an object that holds the name and an identifier. This is how I remove it
Todolists itemToRemove = (Todolists)v.getTag();
Firebase listRef = new Firebase("https://av-dots.firebaseio.com/users/" + userId + "/lists/" + itemToRemove.getIdentifier());
listRef.removeValue();
adapter.remove(itemToRemove);
But how do I edit it? I have the identifier that is the id where I can identifiy it, but how would I edit it?
You can get the corresponding TextView using findViewById:
Resources res = getResources();
int resId = res.getIdentifier(itemToRemove.getIdentifier(), "id", getPackageName());
TextView tv = (TextView)findViewById(resId);
// Edit TextView as appropriate...

How to change a background image of a button in Android without knowing the image name earlier?

I have some image files in drawable folder. The name of the images are like button1, button2, button3......There is also a count variable which increases by one whenever a button is clicked.What I want to do is that whenever someone click on a button image of that button should change to the image corresponding to the current value of count.
I tried
buttons[0][0].setBackground(R.drawable.button+count);
But it is not working
You can't access it directly. You'll have to get the resource using its name:
private Drawable getDrawableResourceByName(int count) {
String packageName = getPackageName();
int resId = getResources().getIdentifier("button" + String.valueOf(count), "drawable", packageName);
return getResources().getDrawable(resId);
}
Then use it as:
buttons[0][0].setBackgroundDrawable(getDrawableResourceByName(count)); //setBackground() only works with drawables from API 16

Image View set Image Resource in code

I have an imageView that i want to display a little icon of the country that you are currently in. I can get the country code, but problem is i can't dynamically change the imageView resource. My image files are all lowercase
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
abve is my code.
Now, of course this will not work because set Image Resource wants an int, so how can i do this? Thanks in advance
I mean you know the name of files(Drawable) which you wanna use for one and other country.
ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(lowerCountryCode.toLowerCase(), "drawable", getPackageName());
imgView.setImageDrawable(res.getDrawable(id));
The following code should work well (assuming that you really have a file with name lowerCountryCode in your res/drawable/ folder)
String lowerCountryCode = countryCode.toLowerCase();
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
ivCard[0].setImageResource(id);
i use that code....
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);

Resources