How to make BottomSheetDialogFragment cover the full screen? - android

I'm using BottomSheetDialogFragment to show some data.But when I'm starting the fragment it's appearing 50% of the screen .So, my question is how to make it full screen when it shows.
BottomSheetDialogFragment Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bot_frag, container, false);
TextView tv = v.findViewById(R.id.textVi);
back=v.findViewById(R.id.back_of_bot);
back.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
}
);
return v;
}

You can use dialog fragments, plz refer this:
public class DialogFragments extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.dialog_dialogfragment_layout, null);
getDialog().setTitle("Title");
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
getDialog().getWindow().setGravity(Gravity.BOTTOM);
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, (int) (metrics.heightPixels * 0.30));// here i have fragment height 30% of window's height you can set it as per your requirement
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationUpDown;
}
and when you want to open,open Bottomsheet dialog like this way :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bot_frag, container, false);
TextView tv = v.findViewById(R.id.textVi);
back=v.findViewById(R.id.back_of_bot);
back.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
DialogFragments dialogFragment = new DialogFragments(this);
dialogFragment.show(fm, "Bottomsheet Fragment");
}
}
);
return v;
}

You have to apply this style to you BottomSheetDialogFragment
android.R.style.Theme_Material_Light_NoActionBar_Fullscreen

Related

which method when display dialog show in fragment

I opened dialog in a fragment and when come back to fragment, my fragment's view is null Object.
when the dialog is displaying which lifecycle method fragment is calling?
public class MyFragment extends Fragment{
TextView textView;
View view;
FloatingActionButton actionButton;
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_read, container, false);
textView = view.findViewById(R.id.txtSaved);
actionButton = view.findViewById(R.id.floatingActionButton);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogSearch search = new DialogSearch();
search.show(getActivity().getSupportFragmentManager(),"MyDialog");
}
});
return view;
}
}
The problem is the dialog is being showned in the onCreateView life cycle, that is why is the Fragment view is null. You have to do it inside the onViewCreated method.
#Override
public View onViewCreated(View view, Bundle savedInstanceState) {
//Work here, the view argument in the method is the view inflated in the onCreateView method
});
Make a small change,
public class MyFragment extends Fragment{
TextView textView;
View view;
FloatingActionButton actionButton;
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_read, container, false);
textView = view.findViewById(R.id.txtSaved);
actionButton = view.findViewById(R.id.floatingActionButton);
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogSearch search = new DialogSearch();
search.show(getActivity().getSupportFragmentManager(),"MyDialog");
}
});
return view; / Add this line -------
}
}
You need to return view
when the dialog is displaying which lifecycle method fragment is calling?
onPause() of fragment gets called.

How use a button inside a fragment?

I'm trying to use 4 fragments
[F1]
[F2][F3][F4]
F1 will have 3 buttons to change between F2,F3 and F4 but i donĀ“t know how to use the buttons properties like in a normal Activity
That's what i have in my F1 code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_01, container, false);
}
Check this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_01, container, false);
// here you have the reference of your button
Button yourButton = (Button)view.findViewById(R.id.your_button_id);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_01, container, false);
Button firstButton = (Button)view.findViewById(R.id.firstbutton);
Button seconButton = (Button)view.findViewById(R.id.seconbutton);
Button thirdButton = (Button)view.findViewById(R.id.thirdbutton);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
Button button = (Button) rootView.findViewById(R.id.buttonSayHi);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frameLayoutFragmentContainer, new FragmentTwo(), "NewFragmentTag");
ft.commit();
ft.addToBackStack(null);
}
}
It's a simple way to reference a button or any widget in a fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
btnOne = view.findViewById(R.id.btnOne);
return view;
You should get the view first, and then get the button from the view
like this:
View rootView = inflater.inflate(R.layout.fragment_01, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
You should do some tutoriasl first, its not that hard.

Beginner trying out onClickListener in fragment. It keep crashing in runtime

I am new to Java/android develop and trying to follow instructions from android site but cannot find clear answer to implementing onclicklistener in fragment. Please help me why its keep crashing in runtime when I click.
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
try this
use this
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
instead of this
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
change your code as below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
return v;
}
Try this
public class TestFragment extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
Move your code from onCreate() method to onCreateView() method and change the following code
From
(TextView)getActivity().findViewById(R.id.text_show);
to
(TextView)v.findViewById(R.id.text_show);

Android ImageButton worked well in Activity. It doesn't work in Fragment

I'm trying to convert a layout so that it will include fragments.
One of the views is an ImageButton that has a listener.
The code worked fine as an Activity but makes trouble as a Fragment...
First problem was that I couldn't use findViewById,
but I was able to find the answer for that here and fixed it with getView().
I can't find an answer to the second problem...
after I declared:
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
I want to set an on click listener:
camBt.setOnClickListener(listener);
But it keeps acting like the ImageButton (camBt) doesn't exist...
I don't even know what's the problem... it worked fine in the Activity...
Here's the full code.
Many thanks!
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
return V;
}
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
}
Move
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView().
Like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.camera_fragment, container, false);
// Here it is
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(listener);
return v;
}
And one more thing you need to understand that variable name should starts with small letter. So you should use View v instead of View V.
You should add
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView() like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return V;
}
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)V.findViewById(R.id.button1);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
camBt.setOnClickListener(listener);
return V;
}
}

android PopupWindow from a fragment

I have an app that uses fragments, its working ok,
but I have now to implement some pop ups when a button gets tapped,
I am following this tutorial "Example of using PopupWindow"
But I get this errors:
Multiple markers at this line
- LAYOUT_INFLATER_SERVICE cannot be resolved to a variable
- The method getBaseContext() is undefined for the type new
View.OnClickListener(){}
here my .java
public class Tab2HeadH1 extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); //ERRORS HERE!!
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
SO, how can I fix this problem??, to show my pop up from tapped button in my fragment??
call
LayoutInflater layoutInflater = (LayoutInflater)**getActivity()**.getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
Atleast it won't show error onBaseContext() as now it take from the associated activity
EDIT
Try this,
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View myFragmentView = inflater.inflate(R.layout.yourlayout, container,false);
//inside button onclick write the code given below
View popupView = inflater.inflate(R.layout.address, null);
}

Categories

Resources