Bug 27937 - Center Aligment is not working when TextView is programatically inserted inside a ViewGroup or FrameLayout.
Summary: Center Aligment is not working when TextView is programatically inserted insi...
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.2
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2015-03-12 00:23 UTC by Harikrishnan
Modified: 2016-12-12 04:59 UTC (History)
4 users (show)

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

Sample to replicate CenterAlignment not working when inserting view programatically. (28.18 KB, application/x-zip-compressed)
2015-03-12 00:32 UTC, Harikrishnan

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 for Bug 27937 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Harikrishnan 2015-03-12 00:23:40 UTC
My application is like a table view which displays data in a table format. All I have made is kept a class extending from FrameLayout as my base class. Inside that I have loaded a custom scrollviewer which is extended from FrameLayout as well. The scrollviewer has a contentView to display the contents which is also derived from FrameLayout. Now I have inserted few FrameLayouts inside my ContentView which acts as rows. Each row has few cells which is a custom class derived from ViewGroup. I have loaded a text view inside each cell and when setting the center alignment for textView(GravityFlags.Center) the alignment is not working.

To replicate it in a simple sample I have tried loading three FrameLayouts one inside the other in the layout file. Then programatically created a Row which contains only cell(extended from ViewGroup) in it. Now if I set Gravity as GravityFlags.Center for the TextView loaded inside my cell, it does nothing. I expect the text to be centerAligned but it does not happens. I have attached the simple sample which replicated me the issue. I hope it will be fixed soon.
Comment 1 Harikrishnan 2015-03-12 00:32:00 UTC
Created attachment 10286 [details]
Sample to replicate CenterAlignment not working when inserting view programatically.

The attached sample replicates the reported issue.
Comment 2 Jonathan Pryor 2015-03-12 09:15:46 UTC
I believe that GravityFlags.Center is working as documented, e.g.:


The above stackoverflow result suggests that you should instead use android:layout_gravity:

Comment 3 Harikrishnan 2015-03-12 10:43:29 UTC
Hi Jonathan,

I think am pretty clear about this.

1) gravity is the way the text will align itself in the TextView.

2) layout_gravity is the way the TextView will align itself in its parent.

And my requirement is the text in the textView should be center aliged. 
I have manually arranged the textview inside the parent specifying its width and height. so I have acheived the second statement. My problem is the first statement. I need the text inside the textview to be center aligned. 
If I change the sample doing every thing in xml, the text is center aligned. But it is not when doing it programatically. So this is surely an issue. 

And moreover am unaware about whether android:layout_gravity programatically. It will be helpful if you share how it can be done if it is possible.