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");
Related
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"
i just need an Alert Dialog with title, msg and buttons, but showed as bottom sheet.
where is a way to obtain this (without custom view)?
Instead of using AlertDialog inside of bottomsheetdialogfragment.
Create one bottomsheetdialog which behaves like your requirement.
Please refer
https://medium.com/glucosio-project/moving-from-dialogs-to-bottomsheetdialogs-on-android-15fb8d140295
How to use BottomSheetDialog?
the best way is to use BottomSheetDialogFragment as you said and to set it a custom view that you wants with only title, msg and buttons
BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();
The AlertDialog and the BottomSheetDialog extend AppCompatDialog both but have different implementation.
Since the layout is simple (just title, message and buttons), it is easier to use a BottomSheetDialog with a custom layout, rather than to use a AlertDialog and adapt all the behaviour and animations of the BottomSheet.
Just use the BottomSheetDialogFragment (which creates a BottomSheetDialog):
public class MyBottomSheetDialog extends BottomSheetDialogFragment {
#Nullable #Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
//Use your custom layout
View view = inflater.inflate(R.layout.yourLayout, container, false);
return view;
}
}
And then
MyBottomSheetDialog myBottomSheetDialog = new MyBottomSheetDialog();
myBottomSheetDialog.show(getSupportFragmentManager(), "TAG");
I answer to my question: Simply no, you're forced to use a CustomView
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?
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;
}
Seems like a simple question to me, but I can't figure it out. I have this code inside my Fragment:
myTextView.setText("Test");
However, this code brakes my app and I am unsure why. Perhaps this is a conflict with Views, but I still don't understand why it wont work. Any suggestions?
How did you set the content?
Edited my answer :) try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.xmlName,
container, false);
TextView tv= (TextView) **view**.findViewById(R.id.textViewName);
tv.setText("yourText");
return view;
}
here is a tutorial for fragments : fragments tutorial