0

I need to create a button in Java. Below is my code:

 Button b = new Button(MyClass.this);
 b.requestLayout();
 LayoutParams lp = b.getLayoutParams();
 lp.height = LayoutParams.WRAP_CONTENT;
 lp.width = LayoutParams.WRAP_CONTENT;
 b.setLayoutParams(lp);
 b.setText("bla");
 b.setTextSize(16);
 b.setOnClickListener(myListener);

I then add this button to the bottom of a ListView:

 getListView().addFooterView(b);

However this crashes, because getLayoutParams returns null.

Even if I create new LayoutParams instead of getLayoutParams, i.e.:

 Button b = new Button(MyClass.this);
 LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 b.setLayoutParams(lp); 
 b.setText("bla");
 b.setTextSize(16);
 b.setOnClickListener(myListener);

then the application crashes. Without setLayoutParams, it runs fine, but my button is not sized properly.

How can I size my button?

lost_bits1110
  • 157
  • 1
  • 5
  • 18

3 Answers3

0

You have to add this button to a view if you want to get LayoutParams from button. Or just create new LayoutParams and set it.

Michael
  • 53,859
  • 22
  • 133
  • 139
0

It's returning null because when you programmatically create a widget, it has no layout params! (Until you add it to a view, then it receives defaults from the LayoutManager)

edit: above is referring to line 3 of your code

Set them like this:

TextView moneyTV = new TextView(this);
LayoutParams lp1 = new LayoutParams(HeightParamHere, WidthParamHere, WeightParamHere);
moneyTV.setLayoutParams(lp1);

Edit2: here's some readybake replacement code.

Button b = new Button(MyClass.this);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b.setText("bla");
b.setTextSize(16);
b.setOnClickListener(myListener);

Assuming you have defined myListener, this should work.

Eric
  • 1,953
  • 4
  • 24
  • 33
  • hi Eric, thanks - I just updated my question, please see it for code - I tried setting my LayoutParams using new, but it causes my app to crash. The crash happens after a call to setListAdapter(adapter). – lost_bits1110 Jun 01 '11 at 19:55
  • your app is crashing on lines 2 and 3. you are requesting parameters that dont exist, hence null pointer exceptions. – Eric Jun 01 '11 at 19:57
  • sorry - I didn't update my question properly, I have updated the last code portion and this still causes my app to crash at a call to setListAdapter(adapter). Without the call to setLayoutParams, it runs fine. – lost_bits1110 Jun 01 '11 at 20:02
  • Thanks for your Edit2 code.. I copied and pasted this, and it still crashes at setListAdapter(adapter). Without the call to b.setLayoutParams, it runs fine :s – lost_bits1110 Jun 01 '11 at 20:05
0

Because I'm adding this button via ListView::addFooterView, it turns out I had to use the ListView type.

 b.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));

Using this instead of just LayoutParams resolves my crash. Hope this helps others.

lost_bits1110
  • 157
  • 1
  • 5
  • 18