Programmatically Added Buttons not showing Text

668 views Asked by At

I have an app in which I'm adding buttons to my layout programmatically to insure the spacing works out properly. In that method I'm using the setText method to change their text. however, that text isn't displaying. Here's what's being created

Here's the code I'm using:

public void addButtons() {
    Button update = new Button(this);
    RelativeLayout.LayoutParams updateParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    updateParams.addRule(RelativeLayout.CENTER_VERTICAL);
    update.setLayoutParams(updateParams);
    update.setPadding(padding, 0, 0, 0);
    update.setText("Update Me!");
    update.setTextColor(Color.BLUE);
    update.setOnClickListener(new ClickHandler("install_install", this));
    updateId = update.generateViewId();
    update.setId(updateId);
    layout.addView(update);

    Button install = new Button(this);
    RelativeLayout.LayoutParams installParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    installParams.addRule(RelativeLayout.RIGHT_OF, update.getId());
    installParams.addRule(RelativeLayout.CENTER_VERTICAL);
    install.setLayoutParams(installParams);
    install.setPadding(padding, 0, 0, 0);
    install.setText("Install LastBox");
    install.setTextColor(Color.BLUE);
    install.setOnClickListener(new ClickHandler("lastbox_install", this));
    installId = install.generateViewId();
    install.setId(installId);
    layout.addView(install);

    Button dlFamily = new Button(this);
    RelativeLayout.LayoutParams dlFamilyParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    dlFamilyParams.addRule(RelativeLayout.RIGHT_OF, install.getId());
    dlFamilyParams.addRule(RelativeLayout.ABOVE, install.getId());
    dlFamily.setLayoutParams(dlFamilyParams);
    dlFamily.setPadding(padding, 0, 0, padding);
    dlFamily.setText("Download Family Build");
    dlFamily.setTextColor(Color.BLUE);
    dlFamily.setOnClickListener(new ClickHandler("family_build", this));
    dlFamilyId = dlFamily.generateViewId();
    dlFamily.setId(dlFamilyId);
    layout.addView(dlFamily);

    Button upFamily = new Button(this);
    RelativeLayout.LayoutParams upFamilyParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    upFamilyParams.addRule(RelativeLayout.RIGHT_OF, install.getId());
    upFamilyParams.addRule(RelativeLayout.BELOW, install.getId());
    upFamily.setLayoutParams(upFamilyParams);
    upFamily.setPadding(padding, padding, 0, 0);
    upFamily.setText("Update Family Build");
    upFamily.setTextColor(Color.BLUE);
    upFamily.setOnClickListener(new ClickHandler("family_update", this));
    upFamilyId = upFamily.generateViewId();
    upFamily.setId(upFamilyId);
    layout.addView(upFamily);

    Button dlAdult = new Button(this);
    RelativeLayout.LayoutParams dlAdultParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    dlAdultParams.addRule(RelativeLayout.RIGHT_OF, dlFamily.getId());
    dlAdultParams.addRule(RelativeLayout.ABOVE, install.getId());
    dlAdult.setLayoutParams(dlAdultParams);
    dlAdult.setPadding(padding, 0, 0, padding);
    dlAdult.setText("Download Adult Build");
    dlAdult.setTextColor(Color.BLUE);
    dlAdult.setOnClickListener(new ClickHandler("adult_build", this));
    dlAdultId = dlAdult.generateViewId();
    dlAdult.setId(dlAdultId);
    layout.addView(dlAdult);

    Button upAdult = new Button(this);
    RelativeLayout.LayoutParams upAdultParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    upAdultParams.addRule(RelativeLayout.RIGHT_OF, upFamily.getId());
    upAdultParams.addRule(RelativeLayout.BELOW, install.getId());
    upAdult.setLayoutParams(upAdultParams);
    upAdult.setPadding(padding, padding, 0, 0);
    upAdult.setText("Update Adult Build");
    upAdult.setTextColor(Color.BLUE);
    upAdult.setOnClickListener(new ClickHandler("adult_update", this));
    upAdultId = upAdult.generateViewId();
    upAdult.setId(upAdultId);
    layout.addView(upAdult);
}
2

There are 2 answers

0
Nico Bentley On BEST ANSWER

I found that setting the text size on the button made the text show up. I guess it defaulted to be size 0?

4
AudioBubble On

Your code is fine as far as you declare layout correctly here's a sample code base on your project

LinearLayout layout;
     layout = (LinearLayout) findViewById(R.id.mainLinear);
    layout.setOrientation(LinearLayout.VERTICAL);

Now the addButtons method

public void addButtons() {
        Button update = new Button(this);
        RelativeLayout.LayoutParams updateParams = new RelativeLayout.LayoutParams(100, 200);
        updateParams.addRule(RelativeLayout.CENTER_VERTICAL);
        update.setLayoutParams(updateParams);
        update.setPadding(0, 0, 0, 0);
        update.setText("Update Me!");
        update.setTextColor(Color.BLUE);
        update.setOnClickListener(new ClickHandler("install_install", this));
        updateId = update.generateViewId();
        update.setId(updateId);
        layout.addView(update);
    }