This is a pretty straightfoward question... i would like to use very simple fragments and tell them which layout to inflate without the need to create a class for each fragment and override the method onCreateView
in simple words i would like to do:
Fragment f = new Fragment();
f.loadfromlayout(R.layout.layout);
is there any way to achieve that?
the closest i get is:
new Fragment() {
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_send_screenshot_step1, container, false);
}
}
======================UPDATE===================
Just to clarify a bit more my requirements. What i'm trying to achieve is this:
https://developer.android.com/training/animation/screen-slide
https://developer.android.com/training/animation/anim_screenslide.mp4
Use ViewPager to create a step-by-step wizard
as shown in the official android documentation my example will load a very simple layout for each "step" (every step is a fragment)... and in order to get this using the default android components i need to create a new class extending Fragment for every single step in my wizard all of them are exaclty the same thing just specifying a different layout in the onCreateView
that is very unnelegant and a lot of repetitive code in my opinion. i would like to avoid
As many people pointed in the comments this behavior was not originally desinged to happen in android.
I found a solution that will work for my case and still fine.
public class GenericFragment extends Fragment {
public static GenericFragment newInstance(#LayoutRes int layout) {
Bundle b = new Bundle();
b.putInt("layout", layout);
GenericFragment g = new GenericFragment();
g.setArguments(b);
return g;
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(getArguments().getInt("layout"), container, false);
}
}
this way i need only 1 single class extending Fragment and i can have as many pages with different layouts i want in my wizards... as long as the pages dont have "specific behavior"
Related
I've noticed every time I create a new blank fragment it gives me some parameters at the top that I find a bit distracting and always delete. I was wondering if there's any way we can set to have these parameters not to appear when a new fragment is created?
Thanks very much.
Create a class and extends it from Fragment. You will get a clean class without any parameters.
public class SampleFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.<xml layout>, container, false);
//Your code
return rootView;
}
}
It is known issue in Android Studio 3.3.1 - 3.4
It is fixed in new android studio version.
I've been looking for how to implement an activity which won't be in full screen but not a dialog style. It is desired to be as follows:
I've tried some methods like AppTheme Dialog but I'm not getting the result I'm expecting. Any ideas how can I achieve that?
The best solution is to use BottomSheetBehaviour
It is pretty easy to use and has a nice animation. Take a look at a simple guide here. Sorry, I can't post the whole process here in detail. But for a quick implementation.
Create a class
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_layout, container, false);
return v;
}
}
Fill in your bottom by calling the class
new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog");
if i click FridgeWarningStatus Fragment then it will occupy full screen? how can i do that???
please any one help me thanks in Advance
public class FridgeContainer extends Fragment {
private View view;
private FridgeTimerStatus fridgeTimerStatus;
private FridgeWarningStatus fridgeWarningStatus;
private FridgeStatus fridgeStatus;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fridge_container,container,false);
fridgeWarningStatus = (FridgeWarningStatus)getFragmentManager().findFragmentById(R.id.fridge_timer_status);
fridgeTimerStatus = (FridgeTimerStatus)getFragmentManager().findFragmentById(R.id.fridge_timer_status);
fridgeStatus = (FridgeStatus)getFragmentManager().findFragmentById(R.id.fridge_status);
return view;
}
If you want full screen why don't you use Activity, Instead of Fragment?
If you want to use fragment then remove action bar of activity, design your fragment view like wise.
If any dought please ask.
In other words, you want FridgeWarningStatus fills the screen? you may also post the file R.layout.fridge_container?
Is it possible/recommanded to let different fragments inherit from each other in Android?
What would be the best way to initialize things that are already initialized in the superclass and add things to it ? (-> for example like the normal subclasses that use super() in their constructor and then initializing other objects )
I looked on the internet but i didn't found much information on this.
I know that it's possible to do return super.onCreateView() but you can't initialize other objects/views after that....
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
super.onCreateView()???
//initialize other objects here
//you have to return a view ...
}
Yes, it is allowed. Why not? For example, if you have a number of Fragments, that display lists, you could put all common methods in FragmentList, and then inherit other fragments, adding only unique methods or overriding the ones from super if needed.
But overriding onCreateView() could raise difficulties in layouts handling. In my recent project I instead created a method inflateFragment() in the super class as follows:
BaseFragment.java
protected View inflateFragment(int resId, LayoutInflater inflater, ViewGroup container) {
View view = inflater.inflate(resId, container, false);
FrameLayout layout = (FrameLayout)view.findViewById(R.id.fragment_layout);
/*
* Inflate shared layouts here
*/
. . .
setHasOptionsMenu(true);
return view;
}
Because of the structure, each and every fragment layout resource is wrapped in a FrameLayout with id = fragment_layout. But you're free to use LinearLayout or whatever parent view you need.
And then in inherited fragments:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflateFragment(R.layout.my_fragment, inflater, container);
/*
* Do things related to this fragment
*/
...
return view;
}
I am using CommonsWare's Android CWAC-Camera Library and trying to use my own UI for the camera fragment. Please forgive me if this is a dumb question. I am following the README file specifically this part:
"You can subclass CameraFragment and override onCreateView(). Chain to the superclass to get the CameraFragment's own UI, then wrap that in your own container with additional widgets, and return the combined UI from your onCreateView()."
Has anyone used this and can provide an example? Thanks!
They mean something like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View camera = super.onCreateView(inflater, container, savedInstanceState);
// wrap camera in some other container that you inflate or instantiate
return yourView;
}