Android - access fragment view from fragment container programatically - android

In MainActivity's onCreate, I replaced the FrameLayout fragment container in its xml layout with a DragSelectRecyclerView in MainFragment's xml layout using FragmentManager. The following code shows this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, new MainFragment())
.commit();
}
MainActivity's xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gra.app.activities.MainActivity">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="?attr/actionBarSize" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior=".utilities.AppBarLayoutBackgroundAlphaBehavior">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
MainFragment's xml:
<com.afollestad.dragselectrecyclerview.DragSelectRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:dsrv_autoScrollEnabled="true"
app:dsrv_autoScrollHotspotHeight="56dp"
tools:context="com.gra.app.fragments.MainFragment" />
Is there any way to programmatically access an instance of DragSelectRecyclerView through FrameLayout?
The reason for this is I need to make the AppBarLayout inside MainActivity's xml dependent on MainFragment's DragSelectRecyclerView so I can fade the AppBarLayout in/out based on
DragSelectRecyclerView.getVerticalScrollbarPosition()
through
CoordinatorLayout.Behavior<AppBarLayout>
Is this possible or should I just move the DragSelectRecyclerView inside MainActivity's xml?

I would suggest you to have a public method in MainFragment -
public float getVerticalScrollbarPosition(){
return DragSelectRecyclerView.getVerticalScrollbarPosition();
}
Update MainActivity's onCreate method to this -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainFragment mainFragment = new MainFragment();
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, mainFragment)
.commit();
}
Now within the MainActivity, you can access the method anywhere to get the value and fade the AppBarLayout in/out accordingly -
mainFragment.getVerticalScrollbarPosition();
This is how the communication between fragments and activity should be handled - https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

You could set the fragment directly in the xml layout of the main activity like
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:id="#+id/fragment"
android:name="some.package.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
And then get the instance of DragSelectRecyclerView in the onCreate method of your activity by
DragSelectRecyclerView recyclerView = (DragSelectRecyclerView) findViewById(R.id.recyclerView)
Or you just set the DragSelectRecyclerView from your Fragment into your Activity:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DragSelectRecyclerView dragSelectRecyclerView = (DragSelectRecyclerView) getView().findViewById(R.id.recyclerView);
Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) {
((MainActivity) activity).setDragSelectRecyclerView(dragSelectRecyclerView);
}
}
In both cases make sure you reset the variable after you fragment is gone.
#Override
public void onDestroyView() {
super.onDestroyView();
Activity activity = getActivity();
if (activity != null && activity instanceof MainActivity) {
((MainActivity) activity).setDragSelectRecyclerView(null);
}
}

you can try setting a tag in the fragment transaction and then retrieve the fragment and its view by the tag you set during the transaction.
you can do it like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, new MainFragment(),"tag") //Adding tag here
.commit();
}
and then later you can retreive the recyclerview inside that fragment by:
View fragmentView = getFragmentManager().findFragmentByTag("tag").getView();
DragSelectRecyclerView recyclerView = (DragSelectRecyclerView)fragmentView.findViewById(R.id.recyclerView) //use the view
but View fragmentView = getFragmentManager().findFragmentByTag("tag").getView(); may return null based on the state of the fragment's state. so you need to look out for that.

Related

How to make a transition from one fragment to another? [duplicate]

I am currently trying to create an app having a splash screen, a login screen and a register screen.
The only way to achieve exactly what I need is through fragments. Though when I try to transition from the splash screen to the login fragment the text view which was part of the splash screen remains. The same happens after transitioning from the login fragment to the register fragment as well as from the register fragment back to the login fragment. It is important to note that there is no issue during transitioning between login and register or between register and login - except the fact that the edit text stays there.
My MainActivity.java
public class StartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
#Override
public void onBackPressed(){
Toast.makeText(getApplicationContext(), "Back button is disabled", Toast.LENGTH_SHORT).show();
}
}
My layout for MainActivity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity"
android:background="#mipmap/background">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewpagerstart"
android:layout_centerInParent="true"
android:name="com.example.distributedsystemsui.SplashFragment">
</fragment>
</RelativeLayout>
The SplashScreen.java
public class SplashFragment extends Fragment {
LinearLayout linearLayout;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_splash, container, false);
linearLayout = (LinearLayout) view.findViewById(R.id.f_linearsplash);
Animation animation_fadein = AnimationUtils.loadAnimation((StartActivity)getActivity(), R.anim.fade_in);
linearLayout.startAnimation(animation_fadein);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_enter_left, R.anim.slide_exit_left,
R.anim.slide_enter_right, R.anim.slide_exit_right);
LoginFragment loginFragment = LoginFragment.newInstance();
ft.replace(R.id.viewpagerstart, loginFragment);
ft.commit();
}
}, 3000);
return view;
}
}
And the layout for the SplashScreen fragment.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginFragment"
android:background="#color/Tranparent"
android:id="#+id/frame_splash">
<LinearLayout
android:id="#+id/f_linearsplash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/f_splashlogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="70dp"
android:background="#FFFFFF"/>
<TextView
android:id="#+id/f_splashtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:background="#FFFFFF"/>
</LinearLayout>
</FrameLayout>
All my search led back to nothing.
I can only assume that either the below contained in the MainActivity.xml creates a confusion (which I tried to change but no luck):
android:name="com.example.distributedsystemsui.SplashFragment"
or that I am doing a mistake in the SplashScreen.java that I cannot locate, even though I tried for more than 5 hours to make it work.
The problem is that you cannot replace fragments added via the <fragment> tag.
Instead, you should either:
1) Switch to FragmentContainerView, which was added in Fragment 1.2.0. It does not suffer from the same issue as the <fragment> tag and lets you do replace operations without issues:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity"
android:background="#mipmap/background">
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewpagerstart"
android:layout_centerInParent="true"
android:name="com.example.distributedsystemsui.SplashFragment">
</androidx.fragment.app.FragmentContainerView>
</RelativeLayout>
2) Use a FrameLayout in your layout and manually add the SplashFragment in your activity (this is essentially what FragmentContainerView does for you):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity"
android:background="#mipmap/background">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewpagerstart"
android:layout_centerInParent="true">
</FrameLayout>
</RelativeLayout>
which means you need to add the SplashFragment manually:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.viewpagerstart, new SplashFragment())
.commit()
}
}
I think it is better to do the fragment transactions from your activity instead of doing it from your fragment. You might consider having functions in your MainActivity as follows.
public class StartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
public void goToLoginFragment() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_enter_left, R.anim.slide_exit_left,
R.anim.slide_enter_right, R.anim.slide_exit_right);
LoginFragment loginFragment = LoginFragment.newInstance();
ft.replace(R.id.viewpagerstart, loginFragment);
ft.commit();
}
public void goToRegisterFragment() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_enter_left, R.anim.slide_exit_left,
R.anim.slide_enter_right, R.anim.slide_exit_right);
LoginFragment registerFragment = RegisterFragment.newInstance();
ft.replace(R.id.viewpagerstart, registerFragment);
ft.commit();
}
#Override
public void onBackPressed(){
Toast.makeText(getApplicationContext(), "Back button is disabled", Toast.LENGTH_SHORT).show();
}
}
And then from your SplashFragment call the function that is defined in your activity.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
((MainActivity) getActivity()).goToLoginFragment();
}
}, 3000);
I hope that solves the problem.

How to find a custom fragment from an inflated layout?

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>
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");
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());
}

FragmentTransaction.remove() not removing fragment only while SwipeRefreshLayout is active

The gist of my problem:
I have multiple fragments that are added sequentially and not re-ordered. Almost all of them have SwipeRefreshLayouts as their root views. When the user triggers onBackPressed, the last fragment should be removed. If a new fragment is opened, the last fragment is hidden.
In the event that the SwipeRefreshLayout is active (ie refresh state set to true and onRefresh is called) and the user pops off the current fragment, the view remains stuck until an app restart. I've confirmed that onPause() and onDestroy() do get called. I have tested this across each type of my fragments, so I'm nearly sure the problem lies in how I remove fragments.
My code:
//I know this is a misnomer. I just changed the inside from
//addToBackStack which got me the same effect
protected void addToBackStack(BaseFragment fragment){
FragmentTransaction ft = fragMan.beginTransaction();
if(fragCount > 0)
ft.hide(peekBackStack());
ft.add(R.id.fragment_container, fragment, Integer.toString(fragCount++)).commit();
invalidateOptionsMenu();
}
protected BaseFragment peekBackStack(){
return (BaseFragment)(fragMan.findFragmentByTag(Integer.toString(fragCount - 1)));
}
#Override
public void onBackPressed(){
if (fragCount <= 1) {
//...do stuff & finish
} else {
BaseFragment fragment = peekBackStack();
if(fragment != null) {
if (fragment.isWeb() && ((WebFragment) fragment).canGoBack())
((WebFragment) fragment).goBack();
else
popActive();
}
else
finish();
}
}
public void popActive(){
BaseFragment remove = peekBackStack();
fragCount--;
BaseFragment show = peekBackStack();
fragMan.beginTransaction().remove(remove).commit();
Log.d(TAG, ""+remove.isVisible());
fragMan.beginTransaction().show(show).commitNow();
show.initAppBar();
}
Inside BaseFragment.java
#Override
#NonNull
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle state){
ViewGroup root = (ViewGroup) inflater.inflate(getLayoutResource(), container, false);
if(root instanceof SwipeRefreshLayout) {
swipeRefreshLayout = (SwipeRefreshLayout) root;
swipeRefreshLayout.setOnRefreshListener(this);
}
return root;
}
#Override
public void onDestroy(){
super.onDestroy();
if(swipeRefreshLayout != null)
swipeRefreshLayout.destroyDrawingCache();
}
...
WebFragment XML:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_submission_link"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.ksoichiro.android.observablescrollview.ObservableWebView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/observable_web"
android:layout_centerHorizontal="true"
android:layout_below="#+id/toolbar"
android:scrollbarStyle="outsideOverlay"
android:fadeScrollbars="true">
</com.github.ksoichiro.android.observablescrollview.ObservableWebView>
Main activity XML:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/appbarLayout">
</RelativeLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
So I found a related thread after hours of searching. Both solutions worked for me, although I think I prefer the FrameLayout wrapper for now. This seems to be a bug in android itself

IllegalArgumentException:No view found for id

I get the following exception:
java.lang.IllegalArgumentException:
No view found for id 0x7f04001a (com.whatever:layout/fragment_main)
Looking at similar questions on SO:
One answer suggests using getChildFragmentManager() instead of getSupportFragmentManager().
This is not an option since MainActivity is not a Fragment.
Another answer suggests that fragment_main is not a child of activity_main, but I think I have that setup in activity_main.xml with tools:layout="#layout/fragment_main
Another suggests that setContent() has not been called, but I have that in onCreate()
What am I missing?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
super.onStart();
// create fragment immediately
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.layout.fragment_main, new MainActivityFragment());
fragmentTransaction.commit();
boolean wereThereWereAnyPendingTransactions = fragmentManager.executePendingTransactions();
}
}
MainActivityFragment.java
public class MainActivityFragment extends Fragment implements Observer<String> {
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="com.whatever.MainActivityFragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_main_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<TextView
android:text="#string/fragment_main_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
The answer with respect to code:
actvity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_frame_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
</FrameLayout>
MainActivity.java:
...
fragmentTransaction.add(R.id.activity_frame_layout, new MainActivityFragment());
...
If you embed a Fragment inside of an Activity XML layout, you do not create it dynamically. The system does it for you. So there is no need to create the transaction and commit it, the Fragment will be created and added when you called setContentView().
Note that the tools:layout= line does not do anything at runtime. It is strictly for the tools to better link layouts and components together.

FragmentManager gives me NPE

I'm checking fragments for the first time and I created a LinearLayout with a FrameLayout within and for my layout-sw600dp a LinearLayout with 2 FrameLayouts.
The one has an id of "main" and the other is "details".
The error I get is :
Caused by: java.lang.NullPointerException
at android.app.BackStackRecord.doAddOp(BackStackRecord.java:395)
at android.app.BackStackRecord.add(BackStackRecord.java:385)
at mes.fallstudio.tvfall.MainActivity.onCreate(MainActivity.java:28)
where line 28 is the last line here:
public class MainActivity extends AppCompatActivity {
private android.support.v4.app.Fragment mainFragment;
private android.app.Fragment detailsFragment;
private Boolean isDualPane = false;
private android.support.v4.app.FragmentManager fm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
mainFragment = fragmentManager.findFragmentById(R.id.main);
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.main, mainFragment).commit();
}
}
XML layout files :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
this is for layout-sw600dp and the same without the 2nd FrameLayout ( details) for the xml file for the layout folder.
I'm extending AppCompatActivity if this helps.
Thanks you
AppCompatActivity uses getSupportFragmentManager() instead of getFragmentManager().
Reference: https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html
EDIT:
In your code you try to get mainFragment from the XML layout. However, you do not define it there. You can either instantiate mainFragment in your code:
mainFragment = new MainFragment();
Or define it in your XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="mes.fallstudio.tvfall.MainFragment"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/details"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
In the latter case you don't need to use the FragmentManager at all in your code unless you need to manipulate the fragments at run time.
In both cases you also need the actual fragment:
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.main_fragment, container, false);
}
}

Categories

Resources