layout - Centering multiple textViews programmatically on Android -


i have open gl es 2.0 app , displaying android textview on top of glsurfaceview.

i have actual textviews displaying ok need try centre them.

this have far:

    text1 = new textview(this);     text1.settext("this sample text");     text1.settextcolor(color.white);     text1.settextsize(textsize);      text2= new textview(this);     text2.settext("and more sample text");     text2.settextcolor(color.white);     text2.settextsize(textsize);      relativelayout.layoutparams textparams = new relativelayout.layoutparams(layoutparams.wrap_content,layoutparams.match_parent);     lp.addrule(relativelayout.center_horizontal);      textlayout = new relativelayout(this);     textlayout.setlayoutparams(textparams);     textlayout.addview(text1);     textlayout.addview(text2);      mainlayout.addview(textlayout); 

however, when run this, text1 centered. text 2 isn't, , starts @ left side of first (correctly centered) textview. try might, can't seem both of them centered. following graphic describes mean:

(please note re vertical placement - in actual results, both textviews @ same vertical/'y' position , therefore overlapping - expected haven't changed vertical position of 2nd textview, make things clearer illustrate, i've moved 2nd 1 down manually.......)

enter image description here

the idea going creating 'textlayout' add of textviews, add textlayout main layout. facilitating addition , removal of of textviews single line of code.

please note not using, , not wish use, xml - know how programmatically.

what missing?

edit here how creating main layout adding textview.....

layout = new relativelayout(this);         layout.setlayoutparams(new layoutparams(layoutparams.match_parent,    layoutparams.match_parent)); 

when adding "relativelayout.center_horizontal" rule layout params, doing telling textlayout should centered horizontally in mainlayout (see http://developer.android.com/reference/android/widget/relativelayout.layoutparams.html#attr_android:layout_centerhorizontal). in code should text1.setgravity(gravity.center) , text2.setgravity(gravity.center).


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -