BottomSheetDialogFragment Open at specific height java - java

I am having a BottomSheetDialogFragment with the code below
public class ClientDetailsModalBSheet extends BottomSheetDialogFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.modal_bottom_sheet, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
The problem i am having is that the Modal BottomSheet is opening half way the screen can i make it open maybe lets say 3/4 of the screen or something like 70% of the screen. I have researched and learnt something like peekHeight but its only applicable when one is using Persistent Bottom Sheet
I have also tried below code inside onCreateView
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.modal_bottom_sheet, container, false);
BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(view);
behavior.setPeekHeight(320, true);
behavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
return view;
}
But the app is crashing and i am getting below error
java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout
at com.google.android.material.bottomsheet.BottomSheetBehavior.from(BottomSheetBehavior.java:1997)
at .fragments.ClientDetailsModalBSheet.onCreateView(ClientDetailsModalBSheet.java:23)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3104)
at androidx.fragment.app.DialogFragment.performCreateView(DialogFragment.java:510)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:524)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1899)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1823)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1760)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:547)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Related

How to use Picasso in onCreateView?

I have problem with Picasso code. In my fragment_home (fragment of navigation bar) is ImageView where I want to put image from "image.com" URL addres.
Picasso code look like that
Picasso.get().load("image.com")
.resize(300,200)
.centerInside()
.into(photo);
I cant write that into my HomeFragmentActivity where code suppose to be because findViewById isn't "working".
HomeFragmentActivity > Place where code have to be
public class HomeFragment extends Fragment {
private ImageView photo;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home /*in this layout is ImageView where Picasso is inserting image from URL addres*/) , container, false);
}
}
When code is somewhere else (for example in MainActivity in onCreate method) app will crash. Please help. Thanks.
Write below you'r suggests, maybe Picasso code don't must be in HomeFragmentActivity ?
You can declare it like this :
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
return view;
}
or
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
photo = view.findViewById(R.id.photo);
Picasso.with(context).load("url")
.resize(300,200)
.centerInside()
.into(photo);
}

How to show my spinner within one fragment

I am on creating an application and at the moment, I have created two fragments which is going to be a "navigation" sort of thing. On the second of the two fragments, I have put a spinner on which is where the user would be able to select a particular option. All of the spinners have unique ID's but the findViewById method cannot be resolved
All of the strings are in the strings xml file and when I tried this spinner in my testing app, it worked
public class Frag2 extends Fragment implements AdapterView.OnItemSelectedListener{
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag2_layout, container, false);
Spinner spinner = findViewById (R.id.spinner1);
ArrayAdapter.createFromResource(getActivity(), R.array.insulin, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter;
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
The spinner should work within the one fragment and then I would like to have it another few times
This is fragment you're dealing with, not an activity. Fragment doesn't extend Context or View classes and does not have this findViewById method.
What you should do is to move your code from onCreateView to onViewCreated which gets a reference to the root view as a parameter. Then use the same method but with that view, as below:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
Spinner spinner = view.findViewById(R.id.spinner1);
...
}
Another thing you may do is replacing:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag2_layout, container, false);
Spinner spinner = findViewById (R.id.spinner1);
// I'm not sure how you didn't see all errors when writing code after a return statement.
...
}
With:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag2_layout, container, false);
Spinner spinner = view.findViewById(R.id.spinner1);
...
return view; // after finishing all logic.
}
UPDATE:
Didn't see that there is also a problem with the ArrayAdapter.
As mentioned in Csongi77 answer, you need to to instantiate the adapter properly. Again, the fragment cannot use as context, you must use the containing activity for that:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.insulin, android.R.layout.simple_spinner_item);
ArrayAdapter.createFromResource creates and returns a new adapter and currently you just disposing it immediately by not saving its reference.
I think first you should instantiate adapter like this: ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.insulin, android.R.layout.simple_spinner_item); please let me know if it works. Regards, Csongi

.getView.findViewById in fragment returning NullPointerException [duplicate]

I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can use it in Fragment as well?
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ImageView imageView = (ImageView)findViewById(R.id.my_image);
return inflater.inflate(R.layout.testclassfragment, container, false);
}
}
The findViewById method has an error on it which states that the method is undefined.
Use getView() or the View parameter from implementing the onViewCreated method. It returns the root view for the fragment (the one returned by onCreateView() method). With this you can call findViewById().
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ImageView imageView = (ImageView) getView().findViewById(R.id.foo);
// or (ImageView) view.findViewById(R.id.foo);
As getView() works only after onCreateView(), you can't use it inside onCreate() or onCreateView() methods of the fragment .
You need to inflate the Fragment's view and call findViewById() on the View it returns.
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
return view;
}
Inside Fragment class you will get onViewCreated() override method where you should always initialize your views as in this method you get view object using which you can find your views like :
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.yourId).setOnClickListener(this);
// or
getActivity().findViewById(R.id.yourId).setOnClickListener(this);
}
Always remember in case of Fragment that onViewCreated() method will not called automatically if you are returning null or super.onCreateView() from onCreateView() method.
It will be called by default in case of ListFragment as ListFragment return FrameLayout by default.
Note: you can get the fragment view anywhere in the class by using getView() once onCreateView() has been executed successfully.
i.e.
getView().findViewById("your view id");
I realise this is an old question, but the prevailing answer leaves something to be desired.
The question is not clear what is required of imageView - are we passing it back as the view, or merely saving a reference for later?
Either way, if the ImageView is coming from the inflated layout, the correct way to do this would be:
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
return v;
}
}
Get first the fragment view and then get from this view your ImageView.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
return view;
}
Inside Fragment class we get onViewCreated() override method where we should always initialize our views because in this method we get view object. Using this object we can find our views like below:
class MyFragment extends Fragment {
private ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment_layout, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//initialize your view here for use view.findViewById("your view id")
imageView = (ImageView) view.findViewById(R.id.my_image);
}
}
You could also do it in the onActivityCreated Method.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
Like they do here: http://developer.android.com/reference/android/app/Fragment.html (deprecated in API level 28)
getView().findViewById(R.id.foo);
and
getActivity().findViewById(R.id.foo);
are possible.
getView() will give the root view
View v = getView().findViewByID(R.id.x);
You can override onViewCreated() which is called right after all views had been inflated. It's the right place to fill in your Fragment's member View variables. Here's an example:
class GalleryFragment extends Fragment {
private Gallery gallery;
(...)
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
gallery = (Gallery) view.findViewById(R.id.gallery);
gallery.setAdapter(adapter);
super.onViewCreated(view, savedInstanceState);
}
}
The method getView() wont work on fragments outside OnCreate and similar methods.
You have two ways, pass the view to the function on the oncreate (what means you can only run your functions when the view is being created) or set the view as a variable:
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_contatos, container, false);
}
public void doSomething () {
ImageView thumbnail = (ImageView) rootView.findViewById(R.id.someId);
}
1) first inflate layout of Fragment then you can use findviewbyId .
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.my_image);
return view;
EditText name = (EditText) getView().findViewById(R.id.editText1);
EditText add = (EditText) getView().findViewById(R.id.editText2);
agreed with calling findViewById() on the View.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) V.findViewById(R.id.my_image);
return V;
}
Note :
From API Level 26, you also don't need to specifically cast the result of findViewById as it uses inference for its return type.
So now you can simply do,
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = view.findViewById(R.id.my_image); //without casting the return type
return view;
}
Use
imagebutton = (ImageButton) getActivity().findViewById(R.id.imagebutton1);
imageview = (ImageView) getActivity().findViewById(R.id.imageview1);
it will work
According to the documentation on API level 11
Reference, in Back Stack
http://developer.android.com/reference/android/app/Fragment.html
short code
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
Using getView() returns the view of the fragment, then you can call findViewById() to access any view element in the fragment view.
Try this it works for me
public class TestClass extends Fragment {
private ImageView imageView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
findViews(view);
return view;
}
private void findViews(View view) {
imageView = (ImageView) view.findViewById(R.id.my_image);
}
}
1) Declare your layout file.
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState) {
return inflate(R.layout.myfragment, container, false);
}
2)Then, get the id of your view
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView nameView = (TextView) view.findViewById(R.id.textview1);
}
The best way to implement this is as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView) rootView.findViewById(R.id.my_image);
return rootView
}
In this way, the rootView can be used for each control defined in the xml layout and the code is much cleaner in this way.
Hope this helps :)
Try This:
View v = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView img = (ImageView) v.findViewById(R.id.my_image);
return v;
try
private View myFragmentView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
myFragmentView = inflater.inflate(R.layout.myLayoutId, container, false);
myView = myFragmentView.findViewById(R.id.myIdTag)
return myFragmentView;
}
Use gradle skeleton plugin, it will automatically generate the view holder classes with the reference to your layout.
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyLayout myLayout = new MyLayout(inflater, container, false);
myLayout.myImage.setImageResource(R.drawable.myImage);
return myLayout.view;
}
}
Now assuming you had an ImageView declared in your my_layout.xml file, it will automatically generate myLayout class for you.
I like everything to be structured. You can do in this way.
First initialize view
private ImageView imageView;
Then override OnViewCreated
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
findViews(view);
}
Then add a void method to find views
private void findViews(View v) {
imageView = v.findViewById(R.id.img);
}
//here you can do it by
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_apple, container,
false);
ist = view.findViewById(R.id.linearLink);
second = view.findViewById(R.id.linearPhone);
return view;
ImageView imageView;
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.testclassfragment, container, false);
imageView = view.findViewById(R.id.my_image);
return view;
}
You can call findViewById() with the Activity Object you get inside your public void onAttach(Activity activity) method inside your Fragment.
Save the Activity into a variable for example:
In the Fragment class:
private Activity mainActivity;
In the onAttach() method:
this.mainActivity=activity;
Finally execute every findViewById through the vairable:
mainActivity.findViewById(R.id.TextView);
Inside onCreateView method
1) first you have to inflate the layout/view you want to add
eg. LinearLayout
LinearLayout ll = inflater.inflate(R.layout.testclassfragment, container, false);
2) Then you can find your imageView id from layout
ImageView imageView = (ImageView)ll.findViewById(R.id.my_image);
3)return the inflated layout
return ll;
You have to inflate the view
public class TestClass extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testclassfragment, container, false);
ImageView imageView = (ImageView)v.findViewById(R.id.my_image);
return v
}}
There is one more method called onViewCreated.
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView imageView = (ImageView) view.findViewById(R.id.imageview1);
}

Cannot retrieve View in onResume() method inside Fragment

I have a method in my fragment that I would like to call from onResume(). Unfortunately, my method requires that I pass it a View, which is defined in onCreateView():
onResume():
#Override
public void onResume() {
super.onResume();
retrieveData(0, false, rootView);
}
onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Set view
View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
...
}
Is there a way to call my retrieveData method as is in onResume(), or should I just create a new method without the rootView parameter (not ideal)?
Can't move it to global scope because the inflater method parameter is part of the fragment interface.
You can only get the view from your fragment, not your activity. To do this, in your fragment onCreateView
save the root view as an Instance Variable like
private View root;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_details_buddy, container, false);
... }
And call it in your onResume
Edit : to get data from Activity, pass it in a Bundle. Doc

loading GestureLibrary inside Fragment Activity not working

how to load Gesturelibrary inside Fragment Activity ...Please help me
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_a, container, false);
//error is here..."this" is not working
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureOverlayView = (GestureOverlayView)view.findViewById(R.id.gestureOverlayView);
return view;
}
It may be asking for context and it's throwing error because fragment doesn't have context.
Try to use like this:
mLibrary = GestureLibraries.fromRawResource(getActivity(), R.raw.gestures);

Categories