android: LayoutParams for TextView makes the view disappear, programmatically

Octoth0rpe picture Octoth0rpe · Aug 15, 2012 · Viewed 51.7k times · Source

I've hit this from various different angles. Basically the gist is this:

I've layed out a template in XML for an interface which NEEDS to be run programmatically, so it's going to be populated dynamically during the run.

The problem here, is that the XML TextView has quite a few layout tweaks (which work) and are necessary. But if I actually set them in code, the TextView doesn't even show up.

(The TextView, by the way, is nested inside a TableRow, thus the call to weight.) The XML template I designed first, to use as reference for the code is this, and it works just fine:

<TextView
  android:id="@+id/txtviewWhiteBox"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
  android:background="@drawable/textview_9patch_white"
  android:gravity="center"
  android:text="75"
  android:textColor="@android:color/black"
  android:layout_margin="20dp"
  android:padding="20dp"
  android:textSize="40dp" />

Like I said, that lays out perfectly. When I run the same layout in code though, and apply LayoutParams, the TextView disappears.

Here's the relevant snippet:

int textViewExampleID = 9001;

private TextView txtviewExample = new TextView(this);

private void buildTextView(){   
    LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    txtviewExample.setId(textViewExampleID);
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
    txtviewExample.setGravity(Gravity.CENTER);
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
    paramsExample.setMargins(20, 20, 20, 20);
    txtviewExample.setPadding(20, 20, 20, 20);
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample");

    //if I comment out the following line, this TextView displays.
    //if I leave it in, it doesn't display.
    txtviewExample.setLayoutParams(paramsExample);
}

I realize there's all sorts of available classes for LayoutParams, and I've been playing with them all LinearLayout.LayoutParams, TableView.LayoutParams, RelativeLayout.LayoutParams, LayoutParams just by itself... No matter which one I try, any attempt at calling "setLayoutParams" renders the entire TextView gone. I've scoured the forums here, and haven't quite found the answer. This can't be THAT uncommon.

Answer

Octoth0rpe picture Octoth0rpe · Aug 15, 2012

well, that was painful but I finally got it figured out. The most important thing to remember (that I just realized) is that of all the myriads of LayoutParams, you need to use the one that relates to the PARENT of the view you're working on, not the actual view.

So in my case, I was trying to get the TextView margins working, but it was being put inside a TableRow. The one simple change was ensuring that the type of LayoutParams being used were the ones for TableRow:

private void buildTextView(){   
    // the following change is what fixed it
    TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    txtviewExample.setId(textViewExampleID);
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
    txtviewExample.setGravity(Gravity.CENTER);
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
    paramsExample.setMargins(20, 20, 20, 20);
    txtviewExample.setPadding(20, 20, 20, 20);
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample");
    txtviewExample.setLayoutParams(paramsExample);
}

Thanks guys, hopefully this will come in handy for somebody else down the line, as I saw a lot of semi-related questions in the forums here, but not one that really defines the problem.