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

What is the difference between defining method in class and implementing interface and overriding its method in android?
Daily alarm issue, Calendar.DATE and Calendar.DAYS_OF_MONTH difference
You cannot start a load for a destroyed activity in relativelayout image using glide
ListView OnItemClickListener not working within a fragment
Volley second request return null
Trouble upgrading to new com.android.support:support-v4:24.2.0 library
Backup Calls on Android 6
JSONArray not a string exception only when proguard is enabled
how to convert longitude and latitude into text format to show street address?
activityManager.getRunningAppProcesses() not working on Andorid M [duplicate]
IndexOutOfBoundsException on getRef().removeValue in FirebaseRecyclerAdapter
Getting error when installing app from Google Play
Android: How to distinguish CLEAR all events from notification bar from user action
Stock Android : NPException : at android.content.res.StringBlock.get(int)
Google Play Services on Android Wear
RxJava make retryWhen() fire to onError() method

Categories

HOME
deep-learning
css3
redux-form
object
powerpoint
flex
interface
mstest
owl-carousel
newrelic
spring-data-mongodb
vxml
pyqt4
polling
opengl-3
32bit-64bit
text-mining
mxgraph
x-cart
datastage
angular-dart
tput
stellar.js
endpoint
pybossa
bitnami
linear-algebra
azure-iot-hub
scsi
null-pointer
google-data-studio
mapstruct
poedit
dpdk
mount
itunes
android-maps
entity-relationship-model
wcag
jaws
winpe
send
contacts
hunspell
html-encode
python-import
baidu-map
mnist
lubridate
numpy-broadcasting
ideascript
flatmap
news
azure-arm
doc
subclipse
print-css
claims
congestion-control
snapchat
cnc
lightning
wpas
appdomain
context-sensitive-grammar
opengl-4
css-transforms
monkey
rxtx
filenet
javafx-css
vcloud-director-rest-api
actor-platform
greatest-n-per-group
mta
clipboard.js
hadoop-partitioning
piracy-prevention
highland.js
fuelcms
viola-jones
nsurlsessiondatatask
diff3
rrule
xmltype
mmwormhole
contrast
create.js
angular-carousel
ss7
personalization
reflector
lnk
matrix-inverse
xml-dsig
functional-java
renderer
vbe
gtm-oauth2
http-patch
jeromq
dtmf
workitem
time.h
server-administration
idn
android-loadermanager
fragmenttransaction
iboutlet
subproject
database-create
hibernateexception
cgbitmapcontextcreate
appledoc
ruby-1.8
folding
openfaces
sifr
purepdf
cuda.net
fault-tolerance
java1.4
information-management
online-storage
commercial-application

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