I'm tryin to add an ImageView into a TableRow in Java, but it always shows in blank. The image seems to be there, but it is completely white.
This is my code:
TableRow newRow = new TableRow(this);
newRow.setId(1);
LayoutParams lptr = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
newRow.setLayoutParams(lptr);
newRow.setBackgroundColor(this.getResources().getColor(R.color.white));
ImageView pImg = new ImageView(this);
pImg.setImageDrawable(this.getResources().getDrawable(R.drawable.error));
newRow.addView(pImg);
Why does it not work?
I think you have forgotten two thing:
1) you have forgotten setContentView(view);
2) you have forgotten to add your TableRow to TableLayout
Related
I got a little problem with adding tablerows to a table layout in android.
Here's my code:
int z = 0;
for(String detail: details){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//odd / even
if(z%2 == 0)
tr.setBackgroundColor(Color.parseColor("#F5F5F5"));
//set detail text
TextView detailText = new TextView(this);
RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
detailText.setTextSize(16.0f);
detailText.setTextColor(Color.parseColor("#333333"));
detailText.setPadding(20, 10, 20, 10);
detailText.setLayoutParams(dtlp);
detailText.setText(detail);
tr.addView(detailText);
detailsTable.addView(tr);
++z;
}
I cannot figure out where the problem is. The detail textview is being set but the tablerows won't show up.
a. Try changing dtlp's "width parameter" to "RelativeLayout.MATCH_PARENT"
OR
b. Before For loop block create two layout params like this.
TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
In loop Replace
detailText.setLayoutParams(dtlp);
WITH
detailText.setLayoutParams(trps);
And replace
detailsTable.addView(tr);
with
detailsTable.addView(tr,tlps);
hope this helps.
I have a TableLayout in my XML, and I'd like to dynamically add a TableRow with an ImageView to that TableLayout. What I have so far is this:
TableRow tr = new TableRow(this);
ImageView imgView = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imgView.setLayoutParams(lp);
imgView.setImageDrawable(getResources().getDrawable(R.drawable.icon_test));
tr.addView(imgView);
tlCollection.addView(tr);
What am I doing wrong? If I want to add a Textview to the same TableRow, it works, but adding an ImageView doesn't work..
The code for adding a TextView:
TextView myTextView = new TextView(this);
myTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
myTextView.setText("Test");
tr.addView(myTextView);
Any idea?
I fixed it, using an LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
TableRow row = (TableRow)inflater.inflate(R.layout.collection_row, tlCollection, false);
//fill textview
TextView content = (TextView)row.findViewById(R.id.txtCollection);
content.setText("Test");
//fill imageview
ImageView myImgView = ImageView)row.findViewById(R.id.imgCollection);
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.my_icon));
//add row to tablelayout
tlCollection.addView(row);
I think it has to do with the layout params. You're setting both dimensions to WRAP_CONTENT but the image view doesn't have "content" per se, it has a source drawable. Try playing around with adjustViewBounds, setScaleType, and the layout params. Notice that in your example of being able to add a TextView, you're setting the width to FILL_PARENT?
The only example I can find quickly in my open projects of adding an ImageView dynamically in this way was a case in which I was using FILL_PARENT in both directions. That might not work for you since I'm not doing it exactly the same, but it's worth playing around with these settings as well as the two others mentioned above.
try this
ImageView imgView = new ImageView(this);
imgView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
tr.addView(imgView);
I Had the same problem now its fixed using the below code
CandFlag =new ImageView(this);
CandFlag.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
CandFlag.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.flag));
LayoutParams layoutParams = (LayoutParams) CandFlag.getLayoutParams();
layoutParams.width = 75;
layoutParams.height = 75;
CandFlag.setLayoutParams(layoutParams);
Tablerow.addView(CandFlag);
I already try many ways but it seems doesn't work with me.
my code :
TableLayout tTopic=(TableLayout) findViewById(R.id.__topic);
TableRow tr = new TableRow(this);
TableLayout.LayoutParams lpt2=null;
lpt2 = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT);
lpt2.setMargins(4, 2, 4, 2);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ImageView view = new ImageView(this);
view.setLayoutParams(new LayoutParams(20,30));
view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
view.setImageResource(R.drawable.kotak);
tr.addView(view);
tTopic.addView(tr,lpt2);
Why it doesn't appear at all? but when I omitted setLayoutParams it appear as default size. Thanks for your help
Try this:
view.setLayoutParams(new TableRow.LayoutParams(20,30));
To set ImageView width programmatically do this:
view.getLayoutParams().width = 20;
I'm running this code and the emulator is posting a force quit. Does anyone know what could be wrong?
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ImageView IV = new ImageView(this);
IV.setBackgroundResource(R.drawable.celtics);
IV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr.addView(IV);
tl.addView(tr);
I actually found a way to get around this by doing this dynamically. I deleted the xml file and created the table in Java
When a button is clicked, the following method is run:
public void createTableRow(View v) {
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
R.id.spreadsheet is an xml TableLayout. I can see from debugging that the method is being accessed, but nothing is drawn to the screen. What gives? Do I need to reset the Content View somehow?
As it turns out, Eclipse is not always right. Ctrl+Shift+M made the wrong import. It had import android.view.ViewGroup.LayoutParams when it should have import android.widget.TableRow.LayoutParams
Everything works fine now.