Bug 35345 - Gravity Flags not working in TextView in Frame Layout
Summary: Gravity Flags not working in TextView in Frame Layout
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 5.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-10-29 02:07 UTC by Dharmendar
Modified: 2015-10-30 17:10 UTC (History)
2 users (show)

Tags: BZCU
Is this bug a regression?: ---
Last known good build:


Attachments
I have replicated above mentioned case in this sample (12.76 KB, application/zip)
2015-10-29 02:07 UTC, Dharmendar
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Dharmendar 2015-10-29 02:07:26 UTC
Created attachment 13570 [details]
I have replicated above mentioned case in this sample

While layout the textview inside the frame layout the gravity flags values Center and CenterVertically doesn’t work.But same case is working fine in Linear Layout

Replication Procedure:
1.Set the content View as Frame layout and 
2.Layout a text view inside the frame layout in the size of a single row using layout override method
3.Set the text view gravity flag to Center or CenterVertical
4.The text will not be display
5.While layout text view inside linear layout and set the width & height of text view instead of layout the text view using layout override method
6.Please refer the below code snippet.
7.Please find attached sample if you not able replicate. 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

FrameLayout samplelayout = new FrameLayout(base.ApplicationContext);
samplelayout.SetBackgroundColor(Color.Yellow);
ParentLayout gridlayout = new ParentLayout(this.BaseContext);
gridlayout.SetBackgroundColor(Color.Purple);
LoadMoreView loadmoreview = new LoadMoreView(this.BaseContext);
loadmoreview.SetBackgroundColor(Color.Pink);
samplelayout.AddView(gridlayout);
gridlayout.AddView(loadmoreview);
SetContentView(samplelayout);
}
}

public class ParentLayout : FrameLayout
{
public ParentLayout(Context context):base(context)
{

}
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
GetChildAt(0).Layout(left, (bottom/2 -25), right, (bottom / 2 + 25));
}
}

public class LoadMoreView : FrameLayout
{
TextView tv;
public LoadMoreView(Context context):base(context)
{
tv = new TextView(context);
tv.Text = "Hello World!";
tv.SetBackgroundColor(Color.Yellow);
tv.SetTextColor(Color.Red);
tv.Gravity = GravityFlags.Center;
this.AddView(tv);
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
tv.Layout(l, 0, r, b-t);
}
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Comment 1 Dharmendar 2015-10-29 02:12:47 UTC
I should also confirm that when loading the TextViews inside the LinearLayout, the text content inside the TextViews are center aligned only if the textViews are given values on SetWidth() and SetHeight() methods. 

But I manually layout the TextViews inside the LinearLayout or the FrameLayout using the OnLayout override, the TextViews does not seem to align its text based on the GravityFlags or TextAlignment set to it.
Comment 2 Jonathan Pryor 2015-10-30 17:10:59 UTC
> FrameLayout samplelayout = new FrameLayout(base.ApplicationContext);

Why is your layout around an *Application* context? This should be an Activity context, i.e. `this`:

http://stackoverflow.com/a/4128799/83444

> ParentLayout gridlayout = new ParentLayout(this.BaseContext);

If it's UI code, it should be using the current Activity, which would be `this`.

Aside from those (to me) obvious questions, this doesn't sound like a Xamarin.Android and/or binding-related bug or question. This sounds like a "Android doesn't work reasonably" question.

Which is basically what you say in Comment #1:

> when loading the TextViews inside the LinearLayout,
> the text content inside the TextViews are center aligned only if the textViews
> are given values on SetWidth() and SetHeight() methods. 

Which sounds like "the API isn't being used right; when the API is used (more?) properly, it works."

~~~

For this to be a Xamarin.Android *bug*, you would need to port that C# code to Java and demonstrate that the app's behavior is *different* between the ~identical C# and Java code. I have not done that, but I suspect that a Java app would behave the same as your C# app, and the TextView contents wouldn't be visible.

To back up that suspicion, see this Java sample which includes an API call that yours doesn't: TextView.setLayoutParams(), providing thew View width and height:

http://www.codota.com/android/scenarios/52fcbdc8da0a1c68922ca17a/android.widget.TextView?tag=dragonfly

> TextView tv = new TextView(getActivity());
> tv.setLayoutParams(new ViewGroup.LayoutParams(MATCH_PARENT,MATCH_PARENT));