31

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Octoth0rpe
  • 2,267
  • 4
  • 19
  • 21

3 Answers3

116

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.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
Octoth0rpe
  • 2,267
  • 4
  • 19
  • 21
  • 11
    THANK YOU! I was have the same issue, it was because I was using LinearLayout.LayoutParams instead of TableRow.LayoutParams... dumb mistake but you saved me from a lot of frustration! – RyanG Nov 29 '12 at 16:25
  • thank you for clear explanation! you stopped several hours of my suffer! – Roman Mar 06 '15 at 08:48
  • Can anyone tell me what imports were used? – Tanner Summers Feb 04 '16 at 22:46
1
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");

   setContentView(txtviewExample);//when you don't use SETTER method for TextView you can't view the desireable text on the UI//
}

//use

killer
  • 11
  • 1
0

what is the 3rd variable in layoutparams supposed to do, is that alpha? what happens if you comment out paramsExample.setMargins

finally what happens if you to txtviewExample.setVisible(View.Visible) after you setLayoutParams?

those would be the things I would try if you haven't

CQM
  • 42,592
  • 75
  • 224
  • 366
  • the third reference in the layoutparams is for weight, equivalent to the `android:layout_weight="1.0"` part in the XML. and the `.setVisible(View.VISABLE);` didn't work. Thanks, though. – Octoth0rpe Aug 15 '12 at 02:51
  • FWIW, I've also tried not using the `txtviewExample.setLayoutParms(paramsExample);` and then during the actual addView in the layout, spec it as a secondary parameter. Still didn't work: `tblrow.addView(txtviewExample,paramsExample);` – Octoth0rpe Aug 15 '12 at 03:02
  • in `setText` I am assuming you have an actual string variable? does this variable have any data when it gets called? – CQM Aug 15 '12 at 03:09
  • yup. Everything is working as planned. The padding, the background, the text size/color/wording... the ONLY thing that I seem to need the layoutParams for is to set the `android:layout_margin="20dp"` portion that exists in the XML. When I remove the LayoutParams from the code, the TextView renders, but it's not in the right place, due to the missing margin settings. – Octoth0rpe Aug 15 '12 at 03:17
  • try removing the weight from the layout params, and try putting the textview in another layout http://stackoverflow.com/a/4814716/727429 – CQM Aug 15 '12 at 03:18
  • yeah, I have tried with and without the weight attribute, doesn't really make a difference. (it's mostly there because there's 4 "columns" to my table, and I want this view to do the stretching.) The full layout is: relativeLayout -> FrameLayout -> TableLayout -> TableRow -> **TextView**, so putting the TextView in _another_ layout isn't really the solution I'm hoping exists. – Octoth0rpe Aug 15 '12 at 03:26
  • For what it's worth, since the ONLY setting that I'm really using the LayoutParams for is the `android:layout_margin="20dp"` line from the XML version, I've also tried this: `MarginLayoutParams paramsExample = new MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);` ... also to no avail. – Octoth0rpe Aug 15 '12 at 12:57
  • This just in: I also just tried putting the TextView inside a new LinearLayout, and then only using setLayoutParams on the new LinearLayout. RelativeLayout -> FrameLayout -> TableLayout -> TableRow -> LinearLayout -> TextView... Still, nothing shows up in that view. – Octoth0rpe Aug 15 '12 at 14:35