How to use Glide in Fragment - Android Studio? - java

I am building a video playing app in Java where I use three Fragments. In one of them I have a list of videos to be played (using a simple ListView widget) where I use a Custom Adapter to pass the titles and the bitmaps thumbnails/uri paths of those videos. When I use Glide to generate and show the bitmaps in the ImageView section of the Custom Adapter, it works (passing only uri paths from Fragment), because the context in the getView() function works as well.
But I want to generate the bitmap thumbnails using Glide in the ListView Fragment, not in the Custom Adapter class, because I need those thumbnails in other Fragments of the app. Unfortunately I didn't managed to create the thumbnails in the ListView Fragment, because the context I am writing at Glide.with() in the onCreateView() function doesn't work!
What is the proper context to be used for Glide in the ListView Fragment to see it and generate the bitmap thumbnails?
I used this code in the ListView Fragment:
Glide.with(this)
.asBitmap().load(uri)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
bitmapThumbnail = resource;
}
});
but it doesn't generate any thumbnails.. :((

In your Fragment you can get the parent Activity Context by calling getContext()

I just found out that the problem wasn't the context I added to Glide.with() section, but the fact that the bitmap generated inside the "onResourceReady(...)" method stays in there and it cannot be used outside the method, only inside and as an argument for an "imageView.setImageBitmap()" method...
As I understood by now, Glide only intermediates between the url/uri/path and the imageView widget, it's not generating any bitmaps outside itself! This is a valuable thing I learned about Glide!
Many thanks for all the help I received!

Related

I am trying to initialize admob ads in Fragment of a Android App

I am trying to initialize admob ads in Fragment of Android App Is this the right way to do it
#Override public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); MobileAds.initialize(getActivity()); }
and do we need to initialize in every Fragment or whether one is enough?
Tried in one fragement
It is preferred to initialize when the App starts (In Splash Screen Activity)
You need to initialize the AdMob one time only not in every Activity or Fragment (After this just load and show the Ads)
If you want it to initialize in Fragment I'll suggest you use
requireActivity() instead of getActivity() method

Getting the Activity inside Adapter in Android

I am trying to access the activity on which my Imageview is, so I can use the URL of an Image of type SVG and display it to the user using the GlideToVectorYou library.
GlideToVectorYou.justLoadImage(activity, IMAGE_URI, targetImageView)
But when I try to get access to the activity using R.layout.activityname, a syntax error appears.
this is the code that I'm using
Uri myurl = Uri.parse(match.getFlag());
GlideToVectorYou.justLoadImage(R.layout.item_basketball, myurl, iv_location);
Thank you!
R.layout.item_basketball is just an integer ID for your activity layout - not the activity instance itself. If you want the activity in your adapter you would need to pass it in when you construct the adapter and save it as a class member (example below), or check if your adapter base class already can provide it via getActivity() or getContext() or a similar method.
class MyAdapter(private val activity: Activity) : BaseAdapter() {
fun someMethod() {
// then you can access "activity" in your adapter methods
GlideToVectorYou.justLoadImage(activity, IMAGE_URI, targetImageView)
}
}
and when you create it in your Activity, you would just do something like this
val adapter = MyAdapter(this)
You need a activity reference. R.layout.somethinghere is the layout reference.
On your adapter constructor add a activity parameter and use it inside the adapter.
If you call adapter constructor from an activity, just pass "this" as parameter. If call from a fragment, use "requireActivity" (if using kotlin) or analogous method (getActivity, for example) if using Java

Android theme not used in library project

I am building a library project which returns Views based on a user request. However the Views I create in the library project do not use the theme I use in my application.
For example if the library project returns a Fragment which has an EditText in it, then the EditText will not use the AppCompat theme even though the app is using it.
Here is the relevant part of the base class I use to inflate the Views in the library project:
public abstract class AbsInputFieldViewController<T extends AbsInputField> {
protected Context getContext(){
return mFragment.getActivity().getApplicationContext();
}
protected LayoutInflater getInflater(){
return (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public abstract View getView(AbsInputField field, Fragment fragment);
}
getView() is implemented in a few sub classes and it uses the LayoutInflater from getInflater() to inflate a View instance from a layout xml.
The problem is that you are getting the LayoutInflater with getSystemService(). If you want the LayoutInflater to use a specific theme you have to create it from a Context which is using that theme. Usually that would be the Context from an Activity or from your Application. Try this instead:
LayoutInflater inflater = LayoutInflater.from(context);

Skipping Android Layouts xml resource

When developing for the android. Are we bound to using xml layout files? (In res\layout )
Or can we skip them entirely and programmatically create and implement our layouts for UI?
Sure you can.
In an Activity you can use setContentView(View view) instead of setContentView(int resource) in the onCreate() callback.
If you use a Fragment you can programmatically create a View instead of inflating a resource. This has to be done in the onCreateView() callback of the fragment

Get access to application layout from Android Phonegap Plugin

I'm currently writing a plugin that is attempting to add a view to the current LinearLayout of my application.
Though I am struggling to get access to the linear layout from within a plugin, I can add the view fine if I do so within my main activity as so:
MyView view = new MyView(this);
root.addView(myView);
But to get the root LinearLayout in my plugin I have assumed that:
this.cordova.getActivity();
is my main activity and have been trying to cast it to the type of my main activity and call a function I added that will return the root LinearLayout object as so:
MyActivity myAct = (MyActivity)this.cordova.getActivity();
MyView view = new MyView(myAct);
myAct .GetLinearLayout().addView(view);
Though this doesn't seem to work and I receive no errors or such to help figure out why?
Anyone know how I can get access to the layout to add my view?
Okay so I solved this slightly differently to what I was attempting to do above.
Firstly casting the activity returned by cordova:
MyActivity myAct = (MyActivity)this.cordova.getActivity();
Does actually work and returns the instance of your main activity, so that wasn't the problem.
In the end I couldn't figure out why adding another view to the root cordova layout from the main activity worked but not when I did so in a plugin so what I did was to create my view from within the activities onCreate() then I provided an accessor to the view class back to my plugin and worked on from there.
The layout no longer exists after init() completes. The last thing that happens in init() is setContentView() is called with the layout. When setContentView() is called with a layout, the layout is inflated and the individual views in the layout are added to the activity.

Categories

Resources