xml - How to get the left margin of a centered view in android? -


i have view centered in xml android:layout_centerhorizontal="true". how leftmargin of it, can align other views it?

i tried using getlayoutparams on centered view didn't work. seems report leftmargin of 0.

i tried compute leftmargin (screenwidth-view width)/2, gives higher value expected. more specifically, code:

    display display = getwindowmanager().getdefaultdisplay();     point size = new point();     display.getsize(size);     int screenwidth = size.x;     int screenheight  = size.y;      textview button1 = (textview) findviewbyid(r.id.button1);     relativelayout.layoutparams params=(relativelayout.layoutparams)     button1.getlayoutparams();     params.height=102;     params.width=114;     params.topmargin =0;     button1.setlayoutparams(params);     params=(relativelayout.layoutparams) button1.getlayoutparams();     int leftmargin = params.leftmargin;      textview button2 = (textview) findviewbyid(r.id.button2);     params=(relativelayout.layoutparams) button2.getlayoutparams();     params.height=102;     params.width=114;     params.leftmargin =leftmargin;     params.topmargin =102;     button2.setlayoutparams(params);      textview button3 = (textview) findviewbyid(r.id.button3);     params=(relativelayout.layoutparams) button3.getlayoutparams();     params.height=102;     params.width=114;     params.leftmargin =(screenwidth-114)/2;     params.topmargin =204;     button3.setlayoutparams(params); 

button1 centered in xml. button2 left-aligned, not centered. button3 more or less centered, right.

so,

1) how left margin of view centered in xml?

2) how center (exactly) view in code? getdefaultdisplay reports higher width real screen width?

you can directly ask layout have views aligned. example,

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"      android:layout_height="match_parent">     <textview         android:id="@+id/centered_view"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerinparent="true"         android:text="random text"/>      <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignleft="@id/centered_view"         android:layout_alignparenttop="true"         android:text="top button"/>      <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignleft="@id/centered_view"         android:layout_alignparentbottom="true"         android:text="bottom button"/> </relativelayout> 

edit: relative offset of centered view, might suffice:

textview.getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() {         @override         public void ongloballayout() {             log.d(getclass().getsimplename(), "top: " + textview.gettop() + " left: " + textview.getleft());             textview.getviewtreeobserver().removeglobalonlayoutlistener(this);         }     }); 

see:

view.gettop() , view.getleft()


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -