How to add parameters to a button from 'Java Code' in Android studio

1

This is something I can not get for more than I try, what I want is this:

These properties add them but in the Java code ...

Can you explain how I would do this ?, I want to add the android property: layout_weight to a Button that is inside a TableRow of a TableLayout ...

    
asked by Alexis Rodriguez 27.04.2017 в 20:00
source

2 answers

2

You can define a LayoutParams to your button, where you can programmatically define width and height, by means of the properties MATCH_PARENT or WRAP_CONTENT , in addition to the weight (last parameter):

LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);

You can even define width and height in pixels, such as 500 px wide and 500 px high, in addition to weight (last parameter):

   LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(500, 500, 1.0f);

In this way you can progamatically define weight properties, as well as width and something to your views:

    
answered by 27.04.2017 / 21:06
source
1

You must do it with the LayoutParams property.

Like this:

button.setLayoutParams(new TableLayout.LayoutParams(x,y,z));

According to the Android Documentation the signature of the constructor is as follows.

LayoutParams(int width, int height, float weight)

Sources: StackOverflow Android Doc's

    
answered by 27.04.2017 в 21:04