Add a default user picture like Gmail Android App - android

How to dynamically create a user picture from the first letter of his user name like Gmail App on android does?
I'm working under Django and Bootstrap, But i'm open for any solution in any language or framwork.
Now I'm managing user profile by using a default picture for new users
and letting them uploading their own profile picture if they want to.
class Profile(models.Model):
user = models.OneToOneField(User)
....
def get_picture(self):
no_picture = settings.STATIC_URL + 'img/user.png'
try:
filename = settings.MEDIA_ROOT + '/profile_pictures/' + self.user.username + '.jpg'
picture_url = settings.MEDIA_URL + 'profile_pictures/' + self.user.username + '.jpg'
if os.path.isfile(filename):
return picture_url
else:
return no_picture
except Exception, e:
return no_picture
.....
PS: I can do that by uploading 26 picture with different Letters, but here i want a dynamic solution for some reasons

For context,
I do not believe the Gmail Android app is actually generating an image. This effect can be achieved in Android with the following shape as a drawable background:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/listview_background_shape">
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<solid android:color="#f5d2c3" />
<size android:width="75dp"
android:height="75dp" />
</shape>
And apply it to a textview:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:background="#drawable/square_background"
android:textSize="40dp"
android:text="J" />
A better solution would be for you to just center text in a div with a background applied. This is much easier then generating an image for something so simple.

Related

Set Entry Cursor visibility // color

i want to create like a "hidden entry" which I use in a data loading screen to preload debit cards after going through a band reader.
My problem it's that i cannot hidden the entry cursor and they see on the screen that "someone it's typing" when they read the card with the band reader.
My code:
<Entry x:Name="cardToRead"
Text="{Binding card}"
InputTransparent="True"
TextColor="Transparent" />
I want to hide the following (purple line cursor):
Is there any property i'm missing, don't found any solution to this! thanks
UPDATE:
Actual solution (not appropriate):
<Entry x:Name="cardToRead"
Text="{Binding card}"
InputTransparent="True"
TextColor="Transparent"
WidthRequest=1 />
Cfun upload the best actual solution to solve this if your android api is higher than 29
You can use a custom Entry:
CustomEntry
internal class CustomEntry : EntryHandler
{
public CustomEntry()
{
}
protected override void ConnectHandler(AppCompatEditText platformView)
{
base.ConnectHandler(platformView);
platformView.ShowSoftInputOnFocus = false;
//platformView.SetCursorVisible(false); //not sure why it is not working
//replaced by this approah:
#if ANDROID29_0_OR_GREATER
platformView.SetTextCursorDrawable(Resource.Drawable.invisible_cursor);
#else
//code to handle it for API< 29, check link in edit section of my answer
#endif
}
}
MauiProgram.cs
#if ANDROID
builder.ConfigureMauiHandlers((handlers) =>
{
handlers.AddHandler(typeof(Entry), typeof(CustomEntry));
}
#endif
invisible_cursor.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/transparent"></solid>
</shape>
EDIT
SetTextCursorDrawable() was introduced in API29+, to support earlier Android version I invite you to take a look at set textCursorDrawable programmatically, you can handdle that case in the #else of #if ANDROID29_0_OR_GREATER (code above edited).

Cannot resolve method in Picasso method .load

Picasso image loading library shows error while use with:
ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(mContext,R.color.light_thirty_alpha));
Picasso.with(mContext)
.load(cd)
.fit().centerCrop()
.transform(new RoundedTransformation(5, 0))
.error(cd)
.placeholder(cd)
.into(imageView_ovelay_slider);
It is showing error that cannot resolve method .load(colorDrawable). I already knows that it is not accept colorDrawable but I am asking can we cast colorDrawable to something else that accepts by .load().
If you look at the source code, you will see that, Picasso's load method can accept any one of the following parameters:
Uri
String
File
int (Resource Id)
It does not accept ColorDrawable
.load method doesn't accepts ColorDrawable as parameter. Look at the official documentation for accepted parameters.
load(File file) - image request using the specified image file.
load(int resourceId) - mage request using the specified drawable resource ID.
load(String path) - image request using the specified path.
load(android.net.Uri uri) - image request using the specified URI.
http://square.github.io/picasso/2.x/picasso/
Solution was so easy finally found by using
solution 1(worked & tested):
ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(mContext,R.color.light_thirty_alpha));
Picasso.with(mContext)
.load(String.valueOf(cd))
.fit().centerCrop()
.transform(new RoundedTransformation(5, 0))
.error(cd)
.placeholder(cd)
.into(imageView_ovelay_slider);
solution 2(worked & tested):
alternate way i found is to create shape drwable.xml file with radius.
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape android:shape="rectangle">
<stroke
android:width="0dp"
android:height="0dp"
android:color="#color/transparent" />
<!-- apply button background transparent, full opacity -->
<solid android:color="#color/box" />
<corners android:radius="2.5dp" />
<padding android:bottom="2dp"
android:left="4dp"
android:right="4dp"
android:top="2dp" />
</shape>
</item>
and use it as background drawable. Thank you #Prerak Sola & Alok

No resource identifier found for attribute 'indeterminateTintMode' in package Android

I am getting an error in my android application developed in Xamarin 5.10.2. I am trying to run MapsAndLocationDemo_v3 but it fails to build the application successfully.
Please help me to solve this.
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/place_autocomplete_progress"
android:padding="3dp"
android:visibility="visible"
android:indeterminate="true"
android:indeterminateTint="#4184F3"
android:indeterminateTintMode="src_atop"
/>
In order to use android:indeterminateTint your minimum api must be Api 21. What I do to change the color of my ProgressBar is using SetColorFilter(), like this:
var color = Color.ParseColor("#4184F3");
var progressBar = FindViewById<ProgressBar>(Resource.Id.place_autocomplete_progress);
progressBar.IndeterminateDrawable.SetColorFilter(color, PorterDuff.Mode.SrcIn);

show an image in a toast

I have used toasts in my android projects to display text. Can I use toasts to display an image popup message. If so can someone give me some useful code segment. Thank You!
You can see my sample code via this link:
https://github.com/Hesamedin/CustomToast
The most important class is this class which drived from Toast class and modified it.
Then use it in normal way as Toast usage like this:
btnCustomToast = (Button) findViewById(R.id.btnCustomToast);
btnCustomToast.setTypeface(pacificoFont);
btnCustomToast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MyToast.makeText(getApplicationContext(), "This is custom toast message.", Toast.LENGTH_LONG).show();
}
});
As I commented in this post you can use my SuperToasts library if you do not feel like creating your own Toast View. You can easily display an image with the following code:
SuperToast superToast = new SuperToast(getActivity());
superToast.setDuration(SuperToast.DURATION_LONG);
superToast.setText("Hello world!");
superToast.setIconResource(R.drawable.image, SuperToast.IconPosition.LEFT);
superToast.show();
The library also comes with a bunch of icons already made and has a bunch of configurable options such as animations, backgrounds, text colors etc. Alternatively you can use SuperActivityToasts if you need to use a Toast within an Activity which will give you access to clickable buttons, progress bars, dismiss listeners etc.
Its possible to do with toast
Code:Add this in your code part
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.like_popup, (ViewGroup) activity.findViewById(R.id.like_popup_layout));
ImageView imageView = (ImageView) layout.findViewById(R.id.like_popup_iv);
imageView.setBackgroundResource(R.drawable.white_delete_icon);
Toast toast = new Toast(activity.getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 200);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
Design:like_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cred_menu_like_popup_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/customshapetransparent"
android:paddingTop="35dp"
android:paddingBottom="25dp"
android:paddingLeft="35dp"
android:paddingRight="35dp"
>
<ImageView
android:id="#+id/like_popup_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Design: customshapetransparent.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#60000000" />
<corners android:radius="8dp" />
</shape>
Hope this will give you what you want to achive

NotFoundException when looking for Drawable resource

I am starting with android and I want to add a border to cells as described in this answer. So I created my cell_background.xml file, which Eclipse created in res\drawable and that contains
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="#000"/>
<stroke android:width="1dp" android:color="#ff9"/>
</shape>
Having read that there are several issues with the drawable folder, I copied it verbatim into the res\drawable-*dpi directories
Now, my app crashes in the following line
Drawable drawable = Resources.getSystem().getDrawable(R.drawable.cell_background);
with this exception
12-16 14:26:28.624: E/AndroidRuntime(533): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020000
Both the project and the emulator are set to v3.0
Any ideas? I have already cleaned and rebuilt the project but it still crashes.
The problem is that you use Resources.getSystem(), which will give you a reference to the system resources. You should use context.getResources() instead.
Try with the following code to check whether the resource exists or not
int drawRes = getDrawableResourceID(context, "cell_background"));
if(drawRes>0){
getResources().getDrawable(drawRes);
}
//To detect whether the reource exits in drawable or not
public static int getDrawableResourceID(Context context,
String identifierName) {
return context.getResources().getIdentifier(identifierName,
"drawable", context.getPackageName());
}
not sure about issue regarding putting in Drawable folder only i haven't got issue any, Still try using this way tht i use generally
Drawable drawable = getResources().getDrawable(R.drawable.cell_background);

Categories

Resources