android


Android: Height of table cell should get height of previous cell


I'm creating a TableLayout programmatically. A table row can consist of an amount + unit (cell 1), and ingredient (cell 2) and a delete button (cell 3).
The ingredients can be longer than the available width, so I used the weight attribute and set it to 1 to enable a line break:
setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
This works.
The problem is that the delete button prevents the table row to increase the height, so it is partly hidden which looks like this:
This is the important part of the code that produces one table row:
final TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setTag(INGREDIENT_ENTRY);
tableRow.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
// Amount and unit
int dp6InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 6);
TextView tvAmountAndUnitText = new TextView(getApplicationContext());
tvAmountAndUnitText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tvAmountAndUnitText.setText(strAmount + " " + strUnit);
tvAmountAndUnitText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvAmountAndUnitText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tvAmountAndUnitText.setGravity(Gravity.RIGHT);
tvAmountAndUnitText.setTypeface(tvAmountAndUnitText.getTypeface(), Typeface.BOLD);
tvAmountAndUnitText.setPadding(dp6InPixel, 0, dp6InPixel, 0);
tableRow.addView(tvAmountAndUnitText);
// Ingredient
TextView tvIngredientText = new TextView(getApplicationContext());
tvIngredientText.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1f));
tvIngredientText.setText(strIngredient);
tvIngredientText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f);
tableRow.addView(tvIngredientText);
// Button
int dp10InPixel = PixelCalculator.convertDpToPixel(getApplicationContext(), 10);
TextView tvIngredientDeleteButton = new TextView(getApplicationContext());
LayoutParams buttonParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(dp10InPixel, 0, 0, 0);
tvIngredientDeleteButton.setLayoutParams(buttonParams);
tvIngredientDeleteButton.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
tvIngredientDeleteButton.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.lightred));
tvIngredientDeleteButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);
tvIngredientDeleteButton.setPadding(dp6InPixel, dp6InPixel, dp6InPixel, dp6InPixel);
tvIngredientDeleteButton.setText("x");
//more code
tableRow.addView(tvIngredientDeleteButton);
ingredientTable.addView(tableRow);
When I set tvIngredientDeleteButton.setMinLines(2);, then I can see the full ingredient cell. Unfortunately all rows have a min height of 2 then which looks ugly. I need some way to recognize if the ingredient cell has a line break and set minLines for that case or any other good solution (but I will not count ingredient characters or something. I guess this can be solved with some table attributes or similar). Any ideas how to solve this problem?
TableRow is direct subclass of LinearLayout. And inorder to set how child views are positioned in it, you need to define it's gravity. Default value is TOP, and I've tried with FILL, CENTER_VERTICAL etc. So any value of setGravity() other than TOP will render TextView with full content. Please refer official document, for more detail.
So you just need to add one statement at a time of declaration of TableRow to achieve your requirement.
tableRow.setGravity(Gravity.FILL);

Related Links

how to apply random function into button array in android
Not able to Login after connect Android client with local XMPP server
Mo Pub add doesnot load
Finding the point on the progressbar where the surface was clicked
Android Autocomplete textview onitemclicklistener not working
Android Source: Repo not displaying any initialization message
adb does not see devices connected to Apple Thunderbolt Cinema Display
How to create TextView with reflection on android?
MediaRecorder issue - throws FileNotFoundException when I try to write to sdcard
how to sort arrays/list and return the result in android?
android imagebutton answers and imageview questions
use ActionBarSherlock as library in my own project
About Android Query library
Are These For Memory Leak Warnings
Only display tabs in action bar
TextView not displaying Characters like “à è ì ò ù” in android

Categories

HOME
uml
cygwin
turing-machines
deep-learning
ng-idle
fpga
ecmascript-6
mstest
socket.io
sublimetext
yandex
scripting
square
jetty
iis-6
google-cloud-datalab
odoo-8
okhttp
openstreetmap
frequency-distribution
aws-sdk
imessage
bourbon
drive
exploit
jquery-callback
jboss6.x
dapper-extensions
itunes
amazon-quicksight
firebase-analytics
intersystems-cache
openmdao
aws-codecommit
jbutton
jett
github3.py
sql-delete
browser-extension
imx6
hivemq
openproject
xcode7.3
imagenet
git-rewrite-history
projects-and-solutions
cin
mongoose-schema
spring-mvc-test
branching-and-merging
glm-math
facebook-social-plugins
libgphoto2
posixct
remedy
colorbar
template-toolkit
shinobi
bootstrap-tags-input
smartfoxserver
angular2-testing
webjars
google-cloud-console
dotnetnuke-7
appscale
mediametadataretriever
tracker-enabled-dbcontext
nio2
fiware-monitoring
coda
sapscript
ember-cli-rails
iced-coffeescript
keyup
telepat
shopizer
dot.js
jama
insight.database
parallel-testing
light
code-duplication
scmmanager
fb.ui
monotouch.dialog
zope.interface
adobe-media-server
thttpd
cross-database
apache-commons-dbutils
workflow-services
promotion-code
jqote
appledoc
diagrams
django-piston
echo3
filesystemobject
dsl-tools
tui
application-planning
firefox-3
image-capture

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App