0

i am really new to android studio and struggle with a very basic problem. I have seen a few threads regarding that issue but none really tacles my problem exactly.

Problem: How am i able to change a fragment in the NavigationDrawer template? The template provides a environment in which the so-called content_main.xml should be changed to another fragment. I do not know how to accomblish that

What I tried: Since I was not able to change the content_main.xml I changed the whole xml, which includes the content_main.xml. It is easier understandble if you just have a short look at my code.

Template app_bar_main.xml that sets up how the fragments are suppose to look and includes content_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    android:id="@+id/content_main_frame"
    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=".MainActivity"
    tools:showIn="@layout/app_bar_main"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>

In the Navigation Drawer main_activity I change my fragment like this:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment newFragment;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (id == ..) {
        } else if (id == R.id.nav_emergencies) {
            transaction.replace(R.id.content_main_frame, new Drawer_Emergencies());
        transaction.addToBackStack(null);
        transaction.commit();

}}

As you might see the transaction replaces the whole app_bar_main but i think i should just change the content_main.xml that is included in app_bar_main.xml. How do I accomblish that?

As I implemented the suggestion from Jude I got a updated result. The Fragment is at the right place, but somehow the content_main_frame.xml (hello world) is still shown.

result for now

mrmeaaan
  • 670
  • 1
  • 6
  • 18

3 Answers3

1

maybe you should try to make a framelayout inside your content_main xml file. then load the new fragment into the framelayout.this is like creating a frame for the new fragment. then load it like:

transaction.add(R.id.yourframelayoutid, new Drawer_Emergencies()).commit();
Jude Bobinihi
  • 178
  • 1
  • 13
  • Thanks! That was a good shot. Sadly it does not replace the content_main framelayout, it puts it on top. That means the content_main is still there instead of being replaced.. – mrmeaaan Dec 11 '18 at 07:53
  • I edited my post btw with a screenshot including, therefore you can see what I actally mean. :) – mrmeaaan Dec 11 '18 at 14:12
  • @Christian.gruener maybe you should include your full XML layout ( that's including app_bar_main.xml) I need to understand what the fragment is supposed to replace. then try resizing your framelayout so it doesn't contain the whole screen and see – Jude Bobinihi Dec 11 '18 at 23:39
  • Hey I did that intially. But that raised the initial issue of the fragment Drawer_emergency being under the Toolbar.. :) – mrmeaaan Dec 12 '18 at 07:31
1

From a Discussion with Mike.M I was able to solve my problem, whereas he provided the way. To make this thread closable I want to post the solution in here, citing Mike.M :

If you want to replace some Views with a FragmentTransaction, those Views should be in their own Fragment, one that you could load at startup, in the Activity's onCreate(). Your subsequent replace() transactions will then work as expected, provided that you're passing the same R.id for all of them; i.e., provided that you're swapping them out of the same ViewGroup. (Btw, please put @ in front of my username when you'd like to ping me. When there's more than one other user in comments, we don't get notified unless you address it directly to a user.)

Which yields the following step-by-step solution:

  1. I have to make empty Framelayout, e.g. make my content_main.xml empty.
  2. Make a seperate Fragment for the inital content and load that in content_main.xml at the start..
  3. After opening a new drawer I the fragment container, which is my Framelayout with a new fragment..
mrmeaaan
  • 670
  • 1
  • 6
  • 18
0

You can do something like this:

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/content_frame">

</FrameLayout>

and then

        transaction.replace(R.id.content_frame, new Drawer_Emergencies());
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • Hey F43nd1r. This is what Jude also suggested. This is a good shot and i implemented that. The only issue i still have is that the concent_frame is not replaced by my new fragment. The fragment sits on top of the content_frame.. How to i resolve that issue? – mrmeaaan Dec 11 '18 at 08:55
  • I edited my post btw with a screenshot including, therefore you can see what I actally mean. – mrmeaaan Dec 11 '18 at 14:12
  • 1
    https://stackoverflow.com/questions/14810348/android-fragment-replace-doesnt-replace-content-puts-it-on-top suggests that the framelayout has to be empty. – F43nd1r Dec 11 '18 at 18:31