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,
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
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
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.