I need to open context menu by one click on view. I have already register view for context menu.
When I used activity I simply called openContextMenu([view registred for context menu]); but fragment hasn't this method.
What must i do to open context menu by one click in fragment?
getActivity().openContextMenu should work.
Find your view or just inflate, and then save it
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
View myView = view.findViewById(R.id.view);
Then you can open context menu using
view.showContextMenu();
Related
I want to prevent dialog dismissing and response touch event behide the dialog when I touch outside of the BottomSheetDialogFragment, so I do like this in my BottomSheetDialogFragment class:
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.poi_result_bottom_dialog, container, false);
this.getDialog().setCanceledOnTouchOutside(false);
return dialogView;
}
However, I find if I set 'setCanceledOnTouchOutside(false)', my activity under the dialog can't respond touch event when I touch outside of the dialog.
You should not be using a BottomSheetDialog. What you want is referred as a Persistent Bottom Sheet. It is well described with instructions at androidhive.info by using an embedded View, and setting up a BottomSheetBehavior.
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;
}
This code is from https://developers.facebook.com/docs/android/login-with-facebook/v2.1.
Code:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
return view;
}
Basically this code is to create the ui layout for the fragment button that will be injected into the main activity layout. My question is for the parameter attachtoroot, why is it false in this case? I know the onCreateView method sets up and returns a view containing the fragment's user interface and gives this view to the hosting activity so the host activity can install the view in its view hierarchy(notes I have). Going off the answer I got here Clarification about layout inflater -attach to root?, attachtoroot being true will attach the fragment layout to its parent layout, in this case main activity layout. Can anyone clarify why its false?
I am new in Android programming.
I created the main Activity of my app style google shop ussing ActionBarSherlock and a NavigationTabs, with fragments, each referencing another activity (Fragment 1 Fragment 2, etc) and each fragment inflating a layout.
However, I'm used to create layouts in xml and then customize them in java. To put a different text depending on the time of day, or according to some data in a database, giving function to buttons, etc.. But in a Fragment Class, I can not even use setContentView to work with each text or button, and set the context for using my database is giving me problems.
How I can customize a xml layout in a fragment?
Or what would be the right thing to do?
Here my Fragment:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.menu, container, false);
}
This is more simple then you think. onCreateView instanciate au returns the view for your Fragment. As you said, in a simple Activity you set (and instanciate) the view with setContentView() and then you get your Views with findViewById().
findViewById() asks for the view to return the view item that you want, you can call it from your view before returning it. Like this:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.menu, container, false);
// For example, getting a TextView
TextView tv = (TextView) v.findViewById(R.id.myTextView);
// do your job
return v;
}
so far so good, you just need to use the view you are inflating to get everything.
here is an example
View v = inflater.inflate(R.layout.menu, container, false);
Button b = (Button)v.findViewById(r.id.button1);
return v;
inside onActivityCreated you could use:
View mView = getView();
TextView textView = (TextView) view.findViewById(R.id.theIdOfTextView);
where theIdOfTextView is declared inside R.layout.menu.
getView() returns the View you inflated inside onCreateView. You use it only after onCreateView has been executed
I have a custom Fragment that inflates it's content from test.xml
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.test, container, true);
return v;
}
Inside of test.xml, I have a toggle button defined like so:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="update"
/>
When I click on the toggle button, I get the following error:
Could not find a method update(View) in the activity class
android.view.ContextThemeWrapper for onClick handler on view class
android.widget.ToggleButton
I have the following method defined in both the calling activity and the fragment, but neither is getting called:
public void update(View view) {
Log.d(LOG_TAG, "starting update!");
}
I'm confused.
When you are creating the LayoutInflater you are creating, probably, with the ContextThemeWrapper and to work you need to create with the activity context.
I had similar problem. I was creating like this:
(LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
then I done like this:
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
and worked.
I hope I have helped.