Sorry if the question isn’t informative enough.
I am coding an app for ICS. I decided to use a TableLayout to create a grid for users to click stuff in.
For each row, I put in several TextViews and an ImageView of a 1dp vertical border between each two of them, all done programmatically in a loop.
Now I made each TextView clickable. When it’s clicked, its background changes to a blue drawable. However, I observe that the drawable does not fill the whole “grid” horizontally.
I think that the ImageView is perhaps occupying more space than it should (1dp). I have come up with or found many ways to stretch the TextViews but neither has worked for me.
Any ideas? Thanks in advance!
Edit: Here is the loop I am using – I know it’s a bit complicated so I didn’t post it 😛 The variables are already declared properly, of course.
for(int i=0; i<5; i++){
tr = new TableRow(ctxt);
tr.setGravity(Gravity.CENTER);
for(int j=0; j<6; j++){ //add the text from an array
tv = new TextView(ctxt);
tv.setText(a[6*i+j]:null);
tv.setTextSize(16);
tv.setPadding(0, 4, 0, 4);
tv.setGravity(Gravity.CENTER);
tv.setClickable(true);
tv.setBackgroundResource(R.drawable.list_selector_background);
tv.setTag(39+6*i+j);
tv.setOnClickListener(this);
tr.addView(tv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 2.0f));
if(j<5){ //set the border
border = new ImageView(ctxt);
border.setImageResource(R.drawable.vert_border);
tr.addView(border, new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.0f));
}
}
tl2.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, 48));
if(i<4){ //horizontal border
tr = new TableRow(ctxt);
tr.setMinimumHeight(1);
tr.setBackgroundColor(color.bg_gray);
tl2.addView(tr);
}
}
Well…this solution was inspired by Alfi, but mostly discovered by myself.
I used a LayoutInflater to create the TableRow instead of constructing it.
where tbrow.xml is
since weight sum cannot be set in the program.
the 293 here equals the total width of my TextViews and my ImageView borders. (48*6+1*5)
Then in the program I can set exactly the width I want.
As Alfi instructed, I set the width to zero then set the weight as the width I want.
as for the border:
Hope this helps somebody else who stumbles upon this page.