Reputation: 59
I am making a chat application. I have one imageview for users profile.
If users image is null/empty, I want show first letter of users name, for example: suppose users name is "UNKNOWN" so i want to show only U on that image view.
Click here to see sample of what I want
I have tried :
Firstly I have checked if users image is null
if (item.getSender_img_url() == null || item.getSender_img_url().isEmpty()||item.getSender_img_url().equals("null") ) {
System.out.println(TAG + " photo is not available ");
//here i am getting first alphabet of Users Name
String[] result = item.getSenderName().split("\\s+");
// This is a regex for matching spaces
// The string we'll create
String abbrev = "";
// Loop over the results from the string splitting
for (int i = 0; i < result.length; i++) {
System.out.println(TAG + " abbrev 1 "+ abbrev);
// Grab the first character of this entry
char c = result[i].charAt(0);
System.out.println(TAG + " abbrev c "+ c);
// If its a number, add the whole number
if (c >= '0' && c <= '9') {
abbrev += result[i];
}
// If its not a number, just append the character
else {
abbrev += c;
}
System.out.println(TAG + " abbrev 3 "+ abbrev);
}
//here i am converting string value into image
Bitmap bm = StringToBitMap(abbrev.toString());
//here i am setting covertes bitmat image into image view
((ViewHolder) holder).friends_image.setImageBitmap(bm);
} else {
System.out.println(TAG + " photo is available ");
try {
Picasso.with(mContext).load(item.getSender_img_url())
.error(R.drawable.profile_avatar)
.placeholder(R.drawable.profile_avatar).resize(40, 40).centerCrop()
.into(((ViewHolder) holder).friends_image);
} catch (Exception e) {
e.printStackTrace();
}
}
.................................
/**
* @param encodedString
* @return bitmap (from given string)
*/
public static Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
Any help?
Upvotes: 1
Views: 3414
Reputation: 2689
Use textview to show the first letter and add a background color to it. Use Random class to get a random color as background each time.
private String senderFirstLetter;
private TextView senderProfilePic;
// To retrieve first letter of the sender,
senderFirstLetter = (String) holder.sender.getText().subSequence(0, 1);
holder.senderProfilePic.setText(senderFirstLetter);
// To get random color
GradientDrawable drawable = (GradientDrawable) holder.senderProfilePic.getBackground();
Random randomBackgroundColor = new Random();
int color = Color.argb(255, randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256));
drawable.setColor(color);
Upvotes: 1
Reputation: 7081
This is actually very simple. You can draw text into a bitmap.
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.BLACK);
Paint circlePaint = new Paint();
circlePaint.setColor(Color.GREEN);
int w = 30; // Width of profile picture
int h = 30; // Height of profile picture
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(w/2, h/2, w/2, circlePaint);
canvas.drawText("A", 0, 0, textPaint);
You can choose to make it centered by finding the text size. All you have to do next is to draw the bitmap to the screen canvas in onDraw.
Upvotes: 0
Reputation: 35549
Instead of showing image with string you should use View
with background color and inside that take TextView
. Something like
<RelativeLayout ...
...>
<TextView></TextView>
</RelativeLayout>
Upvotes: 2