TokTok123
TokTok123

Reputation: 771

Setting the text of a programatically created button

Basic question regarding setting the text of a programatically created button. As seen in my code below I've done the basics in terms of creating the button but my button appears as seen in my attached image. Basically the text in the button doesn't appear as expected. Any ideas why?

Note: I've declared button as a public instance variable right above my onCreate() and has been added correctly to my relative layout using addView();

// Create User button
btnUserAdmin = new Button(this);

// Customise the UserAdmin button
btnUserAdmin.setBackgroundColor(Color.BLUE);
btnUserAdmin.setTextSize(13.7f);
btnUserAdmin.setTextColor(Color.parseColor("#FFCC00"));
btnUserAdmin.setText("USER ADMINISTRATION");
btnUserAdmin.setGravity(Gravity.LEFT);

enter image description here

Thanks.

Upvotes: 1

Views: 63

Answers (1)

Carlos Robles
Carlos Robles

Reputation: 10947

You should specify the dimensions of the button, otherwise the size could be unexpected. For instance

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(              
                                          RelativeLayout.LayoutParams.WRAP_CONTENT,   
                                          RelativeLayout.LayoutParams.MATCH_PARENT   );

btnUserAdmin.setLayoutParams(lp);

also, you can directly set them when you add the buttom

yourRelativeLatout.addView(btnUserAdmin, lp);

Also remember that numeric values for the dimensions (of the bottom or the layout) usually are evil. As you can, use only WRAP_CONTENT and MATCH_PARENT

Upvotes: 1

Related Questions