Android : Linear Layout Android get height of a button

on Thursday, December 4, 2014


In my code I created a horizontal scroll view in which I append buttons. I've already a starting button. In java code I want to get the size of the button already there to shape a new one just like the other and append it.


Here is the .xml code:



<TableRow android:layout_width="fill_parent" android:layout_weight="1" android:background="#ff11ff12">

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="0dip"
android:id="@+id/horizontalScrollView"
android:layout_column="0"
android:focusableInTouchMode="false"
android:fillViewport="false">

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:id="@+id/scrollLayout">

<Button
android:layout_width="wrap_content"
android:layout_height="0dip"
android:text="New Button"
android:id="@+id/button"
android:width="1000px"
/>
</LinearLayout>
</HorizontalScrollView>

</TableRow>


And the java code:



LinearLayout scrollLayout = (LinearLayout)findViewById(R.id.scrollLayout);

Button btnCriar = (Button)findViewById(R.id.button);

int width, height;
width = btnCriar.getMaxWidth();
height = btnCriar.getMaxHeight();

Button btn = new Button(this);
btn.setText("Botão de perfil");

btn.setWidth(width);
btn.setHeight(height);

scrollLayout.addView(btn);


The height is returning -1 and I need to know the real height.


0 comments:

Post a Comment