0

I know this question has been asked too much here but i ahve tried every solution out there to open a fragment from another fragment and no one worked for me.

Fragment1.cs

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    View view = inflater.Inflate(Resource.Layout.my_layout, container, false);
    return view;
    add.Click += delegate
        {
            Fragment2 fragment2 = new Fragment2();
            FragmentTransaction ft = FragmentManager.BeginTransaction();
            ft.Replace(Resource.Id.content_frame, fragment2);
            ft.Commit();
         };
}

my_layout

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame">
<TextView
    android:text="Text"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:id="@+id/total"
    android:textAlignment="center"
    android:textSize="30dp" />
<TextView
    android:text="Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textchangevalue"
    android:textSize="33dip"
    android:textStyle="bold" />
<Button
    android:text="UPDATE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/updatebtn" />
 <Button
    android:text="ADD"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/addbtn" />
 <ListView
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/portfoliolist"
    android:clickable="true" />

Fragment2.cs

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        View view = inflater.Inflate(Resource.Layout.fragment_lay, container, false);

        TextView txt = view.FindViewById<TextView>(Resource.Id.textView1);
        Button btn = view.FindViewById<Button>(Resource.Id.button1);
        listView = view.FindViewById<ListView>(Resource.Id.listView1);
        Toast.MakeText(Activity, "Fragment2 started", ToastLength.Short).Show();
        return view;
       }

When I run the app, and click the add button nothing is happening. Why is that ? I have tried to change the layout of the Fragment1 by putting instead of <LinearLayout> a <FrameLayout> but also disn't worked. Please help me to find a solution.

EDIT: I have put a toast message in the Fragment2 and when I click the add button in the fragment1, the toast message (which is in the fragment2) is showing which means that the fragment2 is starting fine but it's layout is not showing on the screen.

Wassim01
  • 189
  • 3
  • 11
  • 1
    You are trying to attach a listener after method is returned, which is not going to happen ever. – Omkar Jun 02 '18 at 21:45
  • Hello, does my answer work for you? – Robbit Jun 08 '18 at 02:58
  • @JoeLv-MSFT yes it did but would you please tell me how to high-lite `NavuagtionDrawer` item from a Fragment on button click ? Because now when I click the button, it takes me to the secondFragment but the firstfragment is still the highlited one when I swipe the Navigationdrawer menu. – Wassim01 Jun 10 '18 at 13:33

3 Answers3

0

You have to attach your Click event to your button before return the view, and I think that you want to attach this event on your addbtn so you have to create the variable add before to attach this event

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    View view = inflater.Inflate(Resource.Layout.my_layout, container, false);
    Button add = view.FindViewById<Button>(Resource.Id.addbtn);
    add.Click += delegate
    {
        Fragment2 fragment2 = new Fragment2();
        FragmentTransaction ft = FragmentManager.BeginTransaction();
        ft.Replace(Resource.Id.content_frame, fragment2);
        ft.Commit();
    };
    return view;
}
na2axl
  • 1,778
  • 11
  • 15
  • No, it didn't worked. I have put the `return view` after the event, nothing changed, still when I click the `add` button nothing is happening. – Wassim01 Jun 03 '18 at 01:33
0

You can use the events instead:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    View view = inflater.Inflate(Resource.Layout.my_layout, container, false);
    Button add = view.FindViewById<Button>(Resource.Id.addbtn);
    add.Click += (object sender, EventArgs e) =>
    {
        Fragment2 fragment2 = new Fragment2();
        FragmentTransaction ft = FragmentManager.BeginTransaction();
        ft.Replace(Resource.Id.content_frame, fragment2);
        ft.Commit();
    };

    return view;
}
Hichame Yessou
  • 2,658
  • 2
  • 18
  • 30
  • I tried it, it is not working. But I have put a toast message in the `Fragment2` and when I click the `add` button in the `fragment1`, the toast message is showing which means that the `fragment2` is starting but not showing. – Wassim01 Jun 03 '18 at 10:03
  • @Wassim01 Post the code of your Fragment and XML. If you put the toast in the event, it only means that the event is fired – Hichame Yessou Jun 03 '18 at 10:11
  • I have edited my question, plus, the `toast` is in the fragment2 which means the `add` button event is firing and the fragment2 is launching well but it's layout is not showing on the screen – Wassim01 Jun 03 '18 at 10:45
  • @Wassim01 You need to return the view when you finish the onCreate in your fragment – Hichame Yessou Jun 03 '18 at 11:05
  • I did but i forgot to put it in my code above. Sorry. Actually, visualstudio throws an error if you don't put `return view`. – Wassim01 Jun 03 '18 at 11:12
  • There are some problems in your fragment_lay XML, or with what are you trying to replace. I suggest you have a look here on how to properly structure your fragments: https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments – Hichame Yessou Jun 03 '18 at 13:53
0

You need a FrameLayout to contain your LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_frame">
  <LinearLayout
android:id="@+id/ll"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">


    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:id="@+id/total"
        android:textAlignment="center"
        android:textSize="30dp" />
    <TextView
        android:text="Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textchangevalue"
        android:textSize="33dip"
        android:textStyle="bold" />
    <Button
        android:text="UPDATE"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/updatebtn" />
    <Button
        android:text="ADD"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/addbtn" />
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/portfoliolist"
        android:clickable="true" />
  </LinearLayout>
</FrameLayout>

And your code should be :

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        var view = inflater.Inflate(Resource.Layout.my_layout, container, false);
        var ll = view.FindViewById<LinearLayout>(Resource.Id.ll);
        var bt = view.FindViewById(Resource.Id.addbtn);
        bt.Click+= delegate
        {
            Fragment2 fragment2 = new Fragment2();
            FragmentTransaction ft = FragmentManager.BeginTransaction();
            ft.Replace(Resource.Id.content_frame, fragment2);
            ft.Commit();
            ll.Visibility = ViewStates.Gone;
        };
        return view;
    }

You can refer to this to see why your fragment2 just top on it. I use ll.Visibility = ViewStates.Gone; to avoid it.

Robbit
  • 4,300
  • 1
  • 13
  • 29
  • Would you please check this thread [here](https://stackoverflow.com/questions/50746357/android-check-listview-items-on-the-start-of-the-app) – Wassim01 Jun 08 '18 at 10:46
  • I have seen that, but today I am busy, i will do it next week. – Robbit Jun 08 '18 at 10:49
  • Would you mind to check this thread also [here](https://stackoverflow.com/questions/50763580/android-shared-preferences-in-broadcast) – Wassim01 Jun 10 '18 at 18:10