Quantcast

programmatically changing a fragment

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

programmatically changing a fragment

Wally McClure
I'm writing an example on programmatically changing a fragment in an activity.  The resulting image that I get is:
http://twitpic.com/8w8ply.  I don't understand why the display of frag1 does not go away.  Any suggestions, thoughts, criticisms are appreciated.
 
My Activity has the code:


protected override void OnCreate(Bundle bundle)

{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
FragmentManager fm = this.FragmentManager;
FragmentTransaction ft = fm.BeginTransaction();
var f2 = new frag2();
ft.Replace(Resource.Id.list, f2);
ft.Commit();
}
 
My layout has the following code:
<?
xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"
 
android:layout_height="fill_parent">
<fragment android:name="com.wallym.example.fragmenttransaction.frag1"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="match_parent" />
</LinearLayout>
 
My fragment's OnCreateView for frag1 looks like:
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var vw = new LinearLayout(this.Activity.ApplicationContext);
            var tv = new TextView(this.Activity.ApplicationContext);
            tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
            tv.Text = "frag 1";
            vw.AddView(tv);
            return vw;
        }
 
My fragment's OnCreateView for frag2 looks like:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var vw = new LinearLayout(this.Activity.ApplicationContext);
var tv = new TextView(this.Activity.ApplicationContext);
tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
tv.Text = "frag 2";
vw.AddView(tv);
return vw;
}

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: programmatically changing a fragment

Wally McClure
I've tried
ft.Remove(f1);

ft.Add(Resource.Id.list, f2)
I get the same result.  Any ideas?
 
Wally
 

>
> ---------- Forwarded message ----------
> From: Tom Opgenorth <[hidden email]>
> Date: Wed, Mar 14, 2012 at 11:16
> Subject: Re: [mono-android] programmatically changing a fragment
> To: Discussions related to Mono for Android <[hidden email]>
>
>
> Just curious, what if you explicitly delete frag 1 from the +@id/list,
> and then add frag 2 (instead of doing a replace)?
>


_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: programmatically changing a fragment

Mike Bluestein
In reply to this post by Wally McClure
Hi Wally,

I don't think you can replace a fragment created with the "fragment" xml element with a fragment created in code.

On Wed, Mar 14, 2012 at 12:24 PM, Wally McClure <[hidden email]> wrote:
I'm writing an example on programmatically changing a fragment in an activity.  The resulting image that I get is:
http://twitpic.com/8w8ply.  I don't understand why the display of frag1 does not go away.  Any suggestions, thoughts, criticisms are appreciated.
 
My Activity has the code:


protected override void OnCreate(Bundle bundle)

{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
FragmentManager fm = this.FragmentManager;
FragmentTransaction ft = fm.BeginTransaction();
var f2 = new frag2();
ft.Replace(Resource.Id.list, f2);
ft.Commit();
}
 
My layout has the following code:
<?
xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"
 
android:layout_height="fill_parent">
<fragment android:name="com.wallym.example.fragmenttransaction.frag1"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="match_parent" />
</LinearLayout>
 
My fragment's OnCreateView for frag1 looks like:
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var vw = new LinearLayout(this.Activity.ApplicationContext);
            var tv = new TextView(this.Activity.ApplicationContext);
            tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
            tv.Text = "frag 1";
            vw.AddView(tv);
            return vw;
        }
 
My fragment's OnCreateView for frag2 looks like:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var vw = new LinearLayout(this.Activity.ApplicationContext);
var tv = new TextView(this.Activity.ApplicationContext);
tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
tv.Text = "frag 2";
vw.AddView(tv);
return vw;
}

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid



_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: programmatically changing a fragment

Wally McClure

Ok, so I thought I would change my code to give a little better example,  I decided to test on startup if the device was vertical or horizontal and then to display the appropiate fragment.  I also read about the Android.Resource.It.Content, which I think was a personal hangup in my mind.  I also expected the text on the screen from each fragment to change.  The code is below.  Everything works perfectly the first time this load.  However, once you change the orientation of the device, the two fragments overwrite each other on screen.  To me, this has always been contrary to everything I have ever seen on Android, so it is confusing as heck.  Any ideas on how to properly clear the screen and then have the fragment draw properly?
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            FragmentManager fm = this.FragmentManager;
            FragmentTransaction ft = fm.BeginTransaction();
            var f2 = new frag2();
            var f1 = new frag1();
            var wm = WindowManager;
            Display d = wm.DefaultDisplay;
            if (d.Width > d.Height)
            {
                ft.Add(Android.Resource.Id.Content, f1);
            }
            else
            {
                ft.Add(Android.Resource.Id.Content, f2);
            }
            ft.Commit();
        }


Date: Wed, 14 Mar 2012 13:48:50 -0400
From: [hidden email]
To: [hidden email]
Subject: Re: [mono-android] programmatically changing a fragment

Hi Wally,

I don't think you can replace a fragment created with the "fragment" xml element with a fragment created in code.

On Wed, Mar 14, 2012 at 12:24 PM, Wally McClure <[hidden email]> wrote:
I'm writing an example on programmatically changing a fragment in an activity.  The resulting image that I get is:
http://twitpic.com/8w8ply.  I don't understand why the display of frag1 does not go away.  Any suggestions, thoughts, criticisms are appreciated.
 
My Activity has the code:


protected override void OnCreate(Bundle bundle)

{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
FragmentManager fm = this.FragmentManager;
FragmentTransaction ft = fm.BeginTransaction();
var f2 = new frag2();
ft.Replace(Resource.Id.list, f2);
ft.Commit();
}
 
My layout has the following code:
<?
xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"
 
android:layout_height="fill_parent">
<fragment android:name="com.wallym.example.fragmenttransaction.frag1"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="match_parent" />
</LinearLayout>
 
My fragment's OnCreateView for frag1 looks like:
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var vw = new LinearLayout(this.Activity.ApplicationContext);
            var tv = new TextView(this.Activity.ApplicationContext);
            tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
            tv.Text = "frag 1";
            vw.AddView(tv);
            return vw;
        }
 
My fragment's OnCreateView for frag2 looks like:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var vw = new LinearLayout(this.Activity.ApplicationContext);
var tv = new TextView(this.Activity.ApplicationContext);
tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
tv.Text = "frag 2";
vw.AddView(tv);
return vw;
}

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid



_______________________________________________ Monodroid mailing list [hidden email] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: programmatically changing a fragment

Wally McClure
Here's what I am getting now.  I know we're almost there, but danged these stumbling blocks are amazingly frustrating.
http://twitpic.com/8wb7h4
 

From: [hidden email]
To: [hidden email]
Date: Wed, 14 Mar 2012 15:24:14 -0400
Subject: Re: [mono-android] programmatically changing a fragment


Ok, so I thought I would change my code to give a little better example,  I decided to test on startup if the device was vertical or horizontal and then to display the appropiate fragment.  I also read about the Android.Resource.It.Content, which I think was a personal hangup in my mind.  I also expected the text on the screen from each fragment to change.  The code is below.  Everything works perfectly the first time this load.  However, once you change the orientation of the device, the two fragments overwrite each other on screen.  To me, this has always been contrary to everything I have ever seen on Android, so it is confusing as heck.  Any ideas on how to properly clear the screen and then have the fragment draw properly?
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            FragmentManager fm = this.FragmentManager;
            FragmentTransaction ft = fm.BeginTransaction();
            var f2 = new frag2();
            var f1 = new frag1();
            var wm = WindowManager;
            Display d = wm.DefaultDisplay;
            if (d.Width > d.Height)
            {
                ft.Add(Android.Resource.Id.Content, f1);
            }
            else
            {
                ft.Add(Android.Resource.Id.Content, f2);
            }
            ft.Commit();
        }


Date: Wed, 14 Mar 2012 13:48:50 -0400
From: [hidden email]
To: [hidden email]
Subject: Re: [mono-android] programmatically changing a fragment

Hi Wally,

I don't think you can replace a fragment created with the "fragment" xml element with a fragment created in code.

On Wed, Mar 14, 2012 at 12:24 PM, Wally McClure <[hidden email]> wrote:
I'm writing an example on programmatically changing a fragment in an activity.  The resulting image that I get is:
http://twitpic.com/8w8ply.  I don't understand why the display of frag1 does not go away.  Any suggestions, thoughts, criticisms are appreciated.
 
My Activity has the code:


protected override void OnCreate(Bundle bundle)

{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
FragmentManager fm = this.FragmentManager;
FragmentTransaction ft = fm.BeginTransaction();
var f2 = new frag2();
ft.Replace(Resource.Id.list, f2);
ft.Commit();
}
 
My layout has the following code:
<?
xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"
 
android:layout_height="fill_parent">
<fragment android:name="com.wallym.example.fragmenttransaction.frag1"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="match_parent" />
</LinearLayout>
 
My fragment's OnCreateView for frag1 looks like:
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var vw = new LinearLayout(this.Activity.ApplicationContext);
            var tv = new TextView(this.Activity.ApplicationContext);
            tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
            tv.Text = "frag 1";
            vw.AddView(tv);
            return vw;
        }
 
My fragment's OnCreateView for frag2 looks like:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var vw = new LinearLayout(this.Activity.ApplicationContext);
var tv = new TextView(this.Activity.ApplicationContext);
tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
tv.Text = "frag 2";
vw.AddView(tv);
return vw;
}

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid



_______________________________________________ Monodroid mailing list [hidden email] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________ Monodroid mailing list [hidden email] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: programmatically changing a fragment

Wally McClure
I got this working, but omg, it is really complicated.  I'm going to search for some better ways to do this, so don't ask for code.  To me, its a little bit embarassing to look at.
 
Wally 

From: [hidden email]
To: [hidden email]
Date: Wed, 14 Mar 2012 15:44:07 -0400
Subject: Re: [mono-android] programmatically changing a fragment

Here's what I am getting now.  I know we're almost there, but danged these stumbling blocks are amazingly frustrating.
http://twitpic.com/8wb7h4
 

From: [hidden email]
To: [hidden email]
Date: Wed, 14 Mar 2012 15:24:14 -0400
Subject: Re: [mono-android] programmatically changing a fragment


Ok, so I thought I would change my code to give a little better example,  I decided to test on startup if the device was vertical or horizontal and then to display the appropiate fragment.  I also read about the Android.Resource.It.Content, which I think was a personal hangup in my mind.  I also expected the text on the screen from each fragment to change.  The code is below.  Everything works perfectly the first time this load.  However, once you change the orientation of the device, the two fragments overwrite each other on screen.  To me, this has always been contrary to everything I have ever seen on Android, so it is confusing as heck.  Any ideas on how to properly clear the screen and then have the fragment draw properly?
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            FragmentManager fm = this.FragmentManager;
            FragmentTransaction ft = fm.BeginTransaction();
            var f2 = new frag2();
            var f1 = new frag1();
            var wm = WindowManager;
            Display d = wm.DefaultDisplay;
            if (d.Width > d.Height)
            {
                ft.Add(Android.Resource.Id.Content, f1);
            }
            else
            {
                ft.Add(Android.Resource.Id.Content, f2);
            }
            ft.Commit();
        }


Date: Wed, 14 Mar 2012 13:48:50 -0400
From: [hidden email]
To: [hidden email]
Subject: Re: [mono-android] programmatically changing a fragment

Hi Wally,

I don't think you can replace a fragment created with the "fragment" xml element with a fragment created in code.

On Wed, Mar 14, 2012 at 12:24 PM, Wally McClure <[hidden email]> wrote:
I'm writing an example on programmatically changing a fragment in an activity.  The resulting image that I get is:
http://twitpic.com/8w8ply.  I don't understand why the display of frag1 does not go away.  Any suggestions, thoughts, criticisms are appreciated.
 
My Activity has the code:


protected override void OnCreate(Bundle bundle)

{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
FragmentManager fm = this.FragmentManager;
FragmentTransaction ft = fm.BeginTransaction();
var f2 = new frag2();
ft.Replace(Resource.Id.list, f2);
ft.Commit();
}
 
My layout has the following code:
<?
xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"
 
android:layout_height="fill_parent">
<fragment android:name="com.wallym.example.fragmenttransaction.frag1"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="match_parent" />
</LinearLayout>
 
My fragment's OnCreateView for frag1 looks like:
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);
            var vw = new LinearLayout(this.Activity.ApplicationContext);
            var tv = new TextView(this.Activity.ApplicationContext);
            tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
            tv.Text = "frag 1";
            vw.AddView(tv);
            return vw;
        }
 
My fragment's OnCreateView for frag2 looks like:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var vw = new LinearLayout(this.Activity.ApplicationContext);
var tv = new TextView(this.Activity.ApplicationContext);
tv.LayoutParameters = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.MatchParent);
tv.Text = "frag 2";
vw.AddView(tv);
return vw;
}

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid



_______________________________________________ Monodroid mailing list [hidden email] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________ Monodroid mailing list [hidden email] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________ Monodroid mailing list [hidden email] UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Loading...