How to find a custom fragment from an inflated layout?
up vote
0
down vote
favorite
I have a custom fragment in a inflated layout, how to access it?
I have tried many ways after inflated, but none of them worked:
getTransitionManager().findFragmentById(id_xxxxx)
returns null;
getChildAt(x)
, get a View but not be able to cast to fragment;- I Tried to create a new fragment programmatically, and add to a container in the inflated view, but the app crashed says "No view found for id xxx for fragment fff"
code, I want to find ReportCalenderFragment
and hold it:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
LayoutInflater inflater = LayoutInflater.from(getActivity());
mCalendar = inflater.inflate(R.layout.dlg_choose_report, null);
ConstraintLayout ly = (ConstraintLayout)mCalendar;
RelativeLayout rly = (RelativeLayout)ly.getChildAt(0);
LinearLayout lly = (LinearLayout)rly.getChildAt(0);
View v = (View)lly.getChildAt(0);
//lly.setId(R.id.report_calendar_con);
//ReportCalenderFragment frag = new ReportCalenderFragment();
//FragmentTransaction fragTrans = getChildFragmentManager().beginTransaction();
//fragTrans.add(R.id.report_calendar_con, frag);
//fragTrans.commit();
//ReportCalenderFragment frag = (ReportCalenderFragment)lly.getChildAt(0);
...
}
XML, ConstraintLayout
was filtered by this text editor:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/report_calendar_con"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#000"
android:orientation="vertical">
<fragment
android:id="@+id/frag_calendar_rp"
android:name="package.fragments.ReportCalenderFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
android
|
show 2 more comments
up vote
0
down vote
favorite
I have a custom fragment in a inflated layout, how to access it?
I have tried many ways after inflated, but none of them worked:
getTransitionManager().findFragmentById(id_xxxxx)
returns null;
getChildAt(x)
, get a View but not be able to cast to fragment;- I Tried to create a new fragment programmatically, and add to a container in the inflated view, but the app crashed says "No view found for id xxx for fragment fff"
code, I want to find ReportCalenderFragment
and hold it:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
LayoutInflater inflater = LayoutInflater.from(getActivity());
mCalendar = inflater.inflate(R.layout.dlg_choose_report, null);
ConstraintLayout ly = (ConstraintLayout)mCalendar;
RelativeLayout rly = (RelativeLayout)ly.getChildAt(0);
LinearLayout lly = (LinearLayout)rly.getChildAt(0);
View v = (View)lly.getChildAt(0);
//lly.setId(R.id.report_calendar_con);
//ReportCalenderFragment frag = new ReportCalenderFragment();
//FragmentTransaction fragTrans = getChildFragmentManager().beginTransaction();
//fragTrans.add(R.id.report_calendar_con, frag);
//fragTrans.commit();
//ReportCalenderFragment frag = (ReportCalenderFragment)lly.getChildAt(0);
...
}
XML, ConstraintLayout
was filtered by this text editor:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/report_calendar_con"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#000"
android:orientation="vertical">
<fragment
android:id="@+id/frag_calendar_rp"
android:name="package.fragments.ReportCalenderFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
android
Paste your code
– Sarath Kn
Nov 19 at 6:49
@Sarath, code pasted
– voyagex6
Nov 19 at 6:58
Are you inflating the layout inside a fragment?
– Sarath Kn
Nov 19 at 7:14
@Sarath, Yes, I am. And this layout is used as a custom view in a Dialog
– voyagex6
Nov 19 at 7:15
You mean, you have a fragment, where you show a dialog fragment, and inside that dialog fragment's layout you have another fragment?
– Sarath Kn
Nov 19 at 7:23
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a custom fragment in a inflated layout, how to access it?
I have tried many ways after inflated, but none of them worked:
getTransitionManager().findFragmentById(id_xxxxx)
returns null;
getChildAt(x)
, get a View but not be able to cast to fragment;- I Tried to create a new fragment programmatically, and add to a container in the inflated view, but the app crashed says "No view found for id xxx for fragment fff"
code, I want to find ReportCalenderFragment
and hold it:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
LayoutInflater inflater = LayoutInflater.from(getActivity());
mCalendar = inflater.inflate(R.layout.dlg_choose_report, null);
ConstraintLayout ly = (ConstraintLayout)mCalendar;
RelativeLayout rly = (RelativeLayout)ly.getChildAt(0);
LinearLayout lly = (LinearLayout)rly.getChildAt(0);
View v = (View)lly.getChildAt(0);
//lly.setId(R.id.report_calendar_con);
//ReportCalenderFragment frag = new ReportCalenderFragment();
//FragmentTransaction fragTrans = getChildFragmentManager().beginTransaction();
//fragTrans.add(R.id.report_calendar_con, frag);
//fragTrans.commit();
//ReportCalenderFragment frag = (ReportCalenderFragment)lly.getChildAt(0);
...
}
XML, ConstraintLayout
was filtered by this text editor:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/report_calendar_con"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#000"
android:orientation="vertical">
<fragment
android:id="@+id/frag_calendar_rp"
android:name="package.fragments.ReportCalenderFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
android
I have a custom fragment in a inflated layout, how to access it?
I have tried many ways after inflated, but none of them worked:
getTransitionManager().findFragmentById(id_xxxxx)
returns null;
getChildAt(x)
, get a View but not be able to cast to fragment;- I Tried to create a new fragment programmatically, and add to a container in the inflated view, but the app crashed says "No view found for id xxx for fragment fff"
code, I want to find ReportCalenderFragment
and hold it:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
LayoutInflater inflater = LayoutInflater.from(getActivity());
mCalendar = inflater.inflate(R.layout.dlg_choose_report, null);
ConstraintLayout ly = (ConstraintLayout)mCalendar;
RelativeLayout rly = (RelativeLayout)ly.getChildAt(0);
LinearLayout lly = (LinearLayout)rly.getChildAt(0);
View v = (View)lly.getChildAt(0);
//lly.setId(R.id.report_calendar_con);
//ReportCalenderFragment frag = new ReportCalenderFragment();
//FragmentTransaction fragTrans = getChildFragmentManager().beginTransaction();
//fragTrans.add(R.id.report_calendar_con, frag);
//fragTrans.commit();
//ReportCalenderFragment frag = (ReportCalenderFragment)lly.getChildAt(0);
...
}
XML, ConstraintLayout
was filtered by this text editor:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/report_calendar_con"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#000"
android:orientation="vertical">
<fragment
android:id="@+id/frag_calendar_rp"
android:name="package.fragments.ReportCalenderFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
android
android
edited Nov 19 at 8:16
asked Nov 19 at 6:47
voyagex6
33
33
Paste your code
– Sarath Kn
Nov 19 at 6:49
@Sarath, code pasted
– voyagex6
Nov 19 at 6:58
Are you inflating the layout inside a fragment?
– Sarath Kn
Nov 19 at 7:14
@Sarath, Yes, I am. And this layout is used as a custom view in a Dialog
– voyagex6
Nov 19 at 7:15
You mean, you have a fragment, where you show a dialog fragment, and inside that dialog fragment's layout you have another fragment?
– Sarath Kn
Nov 19 at 7:23
|
show 2 more comments
Paste your code
– Sarath Kn
Nov 19 at 6:49
@Sarath, code pasted
– voyagex6
Nov 19 at 6:58
Are you inflating the layout inside a fragment?
– Sarath Kn
Nov 19 at 7:14
@Sarath, Yes, I am. And this layout is used as a custom view in a Dialog
– voyagex6
Nov 19 at 7:15
You mean, you have a fragment, where you show a dialog fragment, and inside that dialog fragment's layout you have another fragment?
– Sarath Kn
Nov 19 at 7:23
Paste your code
– Sarath Kn
Nov 19 at 6:49
Paste your code
– Sarath Kn
Nov 19 at 6:49
@Sarath, code pasted
– voyagex6
Nov 19 at 6:58
@Sarath, code pasted
– voyagex6
Nov 19 at 6:58
Are you inflating the layout inside a fragment?
– Sarath Kn
Nov 19 at 7:14
Are you inflating the layout inside a fragment?
– Sarath Kn
Nov 19 at 7:14
@Sarath, Yes, I am. And this layout is used as a custom view in a Dialog
– voyagex6
Nov 19 at 7:15
@Sarath, Yes, I am. And this layout is used as a custom view in a Dialog
– voyagex6
Nov 19 at 7:15
You mean, you have a fragment, where you show a dialog fragment, and inside that dialog fragment's layout you have another fragment?
– Sarath Kn
Nov 19 at 7:23
You mean, you have a fragment, where you show a dialog fragment, and inside that dialog fragment's layout you have another fragment?
– Sarath Kn
Nov 19 at 7:23
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Try
ReportCalenderFragment frag = (ReportCalenderFragment) getParentFragment().getFragmentManager().findFragmentById(R.id.frag_calendar_rp);
If you are using ReportCalenderFragment
inside a dialog, which is inside another fragment
You should use the ChildFragmentmanager
from parent fragment while showing the dialog
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getChildFragmentManager(), "dialog");
The ReportCalenderFragment is not inside another fragment, it's in a layout of the presented xml file. Actually I used getChildFragmentManager() before. I forgot to mention the code is in the onCreate() method of the mother fragment.
– voyagex6
Nov 19 at 8:13
It seems DialogFragment is a much more appropriate method to deal with a custom dialog than inflating a view, your answer inspired me to use a DialogFragment instead , thx
– voyagex6
Nov 19 at 14:49
add a comment |
up vote
0
down vote
Like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);//this method
Log.e(getClass().getSimpleName(),fragment.toString());
}
There was no error in both fragment and layout, the app can display the inflated layout, but I want to hold the fragment to do something
– voyagex6
Nov 19 at 7:07
Use getSupportFragmentManager().findFragmentById. Like this: ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);
– Justin
Nov 19 at 7:21
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try
ReportCalenderFragment frag = (ReportCalenderFragment) getParentFragment().getFragmentManager().findFragmentById(R.id.frag_calendar_rp);
If you are using ReportCalenderFragment
inside a dialog, which is inside another fragment
You should use the ChildFragmentmanager
from parent fragment while showing the dialog
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getChildFragmentManager(), "dialog");
The ReportCalenderFragment is not inside another fragment, it's in a layout of the presented xml file. Actually I used getChildFragmentManager() before. I forgot to mention the code is in the onCreate() method of the mother fragment.
– voyagex6
Nov 19 at 8:13
It seems DialogFragment is a much more appropriate method to deal with a custom dialog than inflating a view, your answer inspired me to use a DialogFragment instead , thx
– voyagex6
Nov 19 at 14:49
add a comment |
up vote
0
down vote
accepted
Try
ReportCalenderFragment frag = (ReportCalenderFragment) getParentFragment().getFragmentManager().findFragmentById(R.id.frag_calendar_rp);
If you are using ReportCalenderFragment
inside a dialog, which is inside another fragment
You should use the ChildFragmentmanager
from parent fragment while showing the dialog
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getChildFragmentManager(), "dialog");
The ReportCalenderFragment is not inside another fragment, it's in a layout of the presented xml file. Actually I used getChildFragmentManager() before. I forgot to mention the code is in the onCreate() method of the mother fragment.
– voyagex6
Nov 19 at 8:13
It seems DialogFragment is a much more appropriate method to deal with a custom dialog than inflating a view, your answer inspired me to use a DialogFragment instead , thx
– voyagex6
Nov 19 at 14:49
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try
ReportCalenderFragment frag = (ReportCalenderFragment) getParentFragment().getFragmentManager().findFragmentById(R.id.frag_calendar_rp);
If you are using ReportCalenderFragment
inside a dialog, which is inside another fragment
You should use the ChildFragmentmanager
from parent fragment while showing the dialog
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getChildFragmentManager(), "dialog");
Try
ReportCalenderFragment frag = (ReportCalenderFragment) getParentFragment().getFragmentManager().findFragmentById(R.id.frag_calendar_rp);
If you are using ReportCalenderFragment
inside a dialog, which is inside another fragment
You should use the ChildFragmentmanager
from parent fragment while showing the dialog
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getChildFragmentManager(), "dialog");
edited Nov 19 at 8:07
answered Nov 19 at 7:37
Sarath Kn
1,522918
1,522918
The ReportCalenderFragment is not inside another fragment, it's in a layout of the presented xml file. Actually I used getChildFragmentManager() before. I forgot to mention the code is in the onCreate() method of the mother fragment.
– voyagex6
Nov 19 at 8:13
It seems DialogFragment is a much more appropriate method to deal with a custom dialog than inflating a view, your answer inspired me to use a DialogFragment instead , thx
– voyagex6
Nov 19 at 14:49
add a comment |
The ReportCalenderFragment is not inside another fragment, it's in a layout of the presented xml file. Actually I used getChildFragmentManager() before. I forgot to mention the code is in the onCreate() method of the mother fragment.
– voyagex6
Nov 19 at 8:13
It seems DialogFragment is a much more appropriate method to deal with a custom dialog than inflating a view, your answer inspired me to use a DialogFragment instead , thx
– voyagex6
Nov 19 at 14:49
The ReportCalenderFragment is not inside another fragment, it's in a layout of the presented xml file. Actually I used getChildFragmentManager() before. I forgot to mention the code is in the onCreate() method of the mother fragment.
– voyagex6
Nov 19 at 8:13
The ReportCalenderFragment is not inside another fragment, it's in a layout of the presented xml file. Actually I used getChildFragmentManager() before. I forgot to mention the code is in the onCreate() method of the mother fragment.
– voyagex6
Nov 19 at 8:13
It seems DialogFragment is a much more appropriate method to deal with a custom dialog than inflating a view, your answer inspired me to use a DialogFragment instead , thx
– voyagex6
Nov 19 at 14:49
It seems DialogFragment is a much more appropriate method to deal with a custom dialog than inflating a view, your answer inspired me to use a DialogFragment instead , thx
– voyagex6
Nov 19 at 14:49
add a comment |
up vote
0
down vote
Like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);//this method
Log.e(getClass().getSimpleName(),fragment.toString());
}
There was no error in both fragment and layout, the app can display the inflated layout, but I want to hold the fragment to do something
– voyagex6
Nov 19 at 7:07
Use getSupportFragmentManager().findFragmentById. Like this: ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);
– Justin
Nov 19 at 7:21
add a comment |
up vote
0
down vote
Like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);//this method
Log.e(getClass().getSimpleName(),fragment.toString());
}
There was no error in both fragment and layout, the app can display the inflated layout, but I want to hold the fragment to do something
– voyagex6
Nov 19 at 7:07
Use getSupportFragmentManager().findFragmentById. Like this: ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);
– Justin
Nov 19 at 7:21
add a comment |
up vote
0
down vote
up vote
0
down vote
Like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);//this method
Log.e(getClass().getSimpleName(),fragment.toString());
}
Like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);//this method
Log.e(getClass().getSimpleName(),fragment.toString());
}
edited Nov 19 at 7:23
answered Nov 19 at 6:59
Justin
12
12
There was no error in both fragment and layout, the app can display the inflated layout, but I want to hold the fragment to do something
– voyagex6
Nov 19 at 7:07
Use getSupportFragmentManager().findFragmentById. Like this: ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);
– Justin
Nov 19 at 7:21
add a comment |
There was no error in both fragment and layout, the app can display the inflated layout, but I want to hold the fragment to do something
– voyagex6
Nov 19 at 7:07
Use getSupportFragmentManager().findFragmentById. Like this: ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);
– Justin
Nov 19 at 7:21
There was no error in both fragment and layout, the app can display the inflated layout, but I want to hold the fragment to do something
– voyagex6
Nov 19 at 7:07
There was no error in both fragment and layout, the app can display the inflated layout, but I want to hold the fragment to do something
– voyagex6
Nov 19 at 7:07
Use getSupportFragmentManager().findFragmentById. Like this: ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);
– Justin
Nov 19 at 7:21
Use getSupportFragmentManager().findFragmentById. Like this: ReportCalenderFragment fragment= (ReportCalenderFragment) getSupportFragmentManager().findFragmentById(R.id.frag_calendar_rp);
– Justin
Nov 19 at 7:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369585%2fhow-to-find-a-custom-fragment-from-an-inflated-layout%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Paste your code
– Sarath Kn
Nov 19 at 6:49
@Sarath, code pasted
– voyagex6
Nov 19 at 6:58
Are you inflating the layout inside a fragment?
– Sarath Kn
Nov 19 at 7:14
@Sarath, Yes, I am. And this layout is used as a custom view in a Dialog
– voyagex6
Nov 19 at 7:15
You mean, you have a fragment, where you show a dialog fragment, and inside that dialog fragment's layout you have another fragment?
– Sarath Kn
Nov 19 at 7:23