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);
Related
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!
I Want to implement multi category in rss feed app, i created a method Downloader which works properly in mainactivity.
But Downloader Method not works in tab fragments.
Here is screenshot of my project and Downloader method.
click here for project screenshot
When i implement downloader method in fragment, it shows an error.
context c is not resolve here
here is code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
recyclerview = view.findViewById(R.id.rv);
recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
new Downloader(c,urlAddress,recyclerview).execute();
return view;
}
comment if you need any other information.
You defined in the signature of your class that the first parameter should be context. You try to pass an undefined variable called c in your code thats why c gets highlighted as error. To fix your code either define c or directly pass in an context into the constructor.
You can get the context by calling getContext()and it should work:
new Downloader(getContext(), urlAddress, recyclerview).execute();
I'm working on a "home feed"-like feature where there's a main Fragment with several other fragments added to its layout, making up the content page. I'd like the main fragment class to be able to instanstiate all the fragment classes that inherit from a certain parent fragment class. This way the code would be more dynamic instead of adding a bunch of <fragment> tags to my xml files.
I'm kinda stuck on making up a decent architecture. How would you go on about doing this?
UPDATE:
Here's what I'm basically trying to do, but don't know how:
public class FeedFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View parentView = inflater.inflate(R.layout.fragment_home, container, false);
// Get fragments and dynamically add them to
// the FeedFragment's layout
getEntryFragmentsList();
// ...
return parentView;
}
}
public abstract class FeedEntryFragment extends Fragment {
// Somehow add fragment to list of entry fragments
}
public class TestFragment extends FeedEntryFragment {
// Already added to list of entry fragments
}
I don't think that possible to decrease the count of xmls by inheritance. I think you should try to split your xml configurations and use some <merge> or <include> to build the full one from the parts.
May be I can provide more help if you will describe your problem in more details.
You should use FrameLayout to add fragment(s) dynamically by using the FragmentTransaction.
You can also use a ViewPager with tabs or bottom tabs to show multiple fragments. Please check sample from my Dynamic Support library for the complete code.
Abstract fragments
DynamicFragment - Abstract base fragment from the Dynamic support library.
DynamicViewPagerFragment - Abstract fragment which extends the DynamicFragment to implement the ViewPager functionality.
Implementation
HomeFragment - Sample fragment extends the DynamicFragment to implement the home screen.
SettingsFragment - Sample fragment extends the DynamicViewPagerFragment to implement the settings functionality using multiple fragments inside a view pager.
Tutorial Implementation
This better suits your need. TutorialActivity returns a list of fragments to be displayed inside a ViewPager.
DynamicSimpleTutorial generates a DynamicTutorialFragment according to the supplied parameters.
I want to change the text in text view in fragment1 by clicking a button in fragment2, i managed to do that by declaring the text view as static so i can change the text by Fragment1.textv.setText("hi"); , is that ok and what is the best way to do this.
static textv ;
#Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
textv= (TextView)v.findViewById(R.id.textview);
return v;
}
Most common approach is using interfaces to 'speak' from fragment A to fragment B (you would need to communicate through the activity):
http://developer.android.com/training/basics/fragments/communicating.html
You can also use an Event-bus library like Otto, register the fragments that you want to update, and from the other fragment post an update:
http://square.github.io/otto/
as user3806331 stated you need to use interfaces ,follow the link and you will find what you need.but be aware that having static reference to a widget is a bad practice ,because it may lead to memory leaks in some situations.
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