java - Android programically added button has match parent width and height -


i'm trying add button

        button dalsi_akce = new button(this);         dalsi_akce.setgravity(gravity.center);         relativelayout.layoutparams p = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content,         relativelayout.layoutparams.wrap_content);         dalsi_akce.setlayoutparams(p);         setcontentview(dalsi_akce);         dalsi_akce.settext("test"); 

button appears full match parent. have button on whole display. can advice me how set width , height of button ?

you setting activity's content button. that's why spans on whole activity , wrong.

instead create activity's layout (an xml file) , set setcontentview. can programatically add button content.

example:

your activity:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     viewgroup viewgroup = (viewgroup) findviewbyid(r.id.mylayout);     button dalsi_akce = new button(this);     dalsi_akce.setgravity(gravity.center);     relativelayout.layoutparams p = new relativelayout.layoutparams(             relativelayout.layoutparams.wrap_content,             relativelayout.layoutparams.wrap_content);     dalsi_akce.setlayoutparams(p);     dalsi_akce.settext("test");       viewgroup.addview(dalsi_akce); } 

main.xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin"     android:id="@+id/mylayout"     tools:context=".myactivity">   </relativelayout> 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -