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

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

Align RadioButtons in RadioGroup (created by code) in android

In my app I need to create a RadioGroup with 2 RadioButtons by code. I need to align the RadioButtons inside the RadioGroup, one to the left, and the other to the right part of RadioGruop. I know that if I add the radio-buttons to a LinearLayout these will solve this problem but in this case the property of RadioGroup, that only one RadioButton is checked at a time is not anymore available.
Here is my code :
TableRow.LayoutParams lp_radio1 = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams lp_radio2 = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams lp_radiogr = new TableRow.LayoutParams(
width, height);
lp_radio1.setMargins(left, left, top2, 0);
lp_radio1.gravity=Gravity.LEFT;
lp_radio1.weight=1;
lp_radio2.setMargins(left, left, top2, 0);
lp_radio2.gravity=Gravity.RIGHT;
lp_radio2.weight=1;
product_radiogroup = new RadioGroup(viewToLoad.getContext());
product_radiogroup.setLayoutParams(lp_radiogr); product_radiogroup.setOrientation(RadioGroup.HORIZONTAL); product_radiogroup.setBackgroundResource(R.drawable.radio_group_background);
product_radiobuttonYES = new RadioButton(viewToLoad.getContext());
product_radiobuttonYES.setLayoutParams(lp_radio1);
product_radiobuttonYES.setTextColor(R.color.medium_gray);
product_radiobuttonNO = new RadioButton(viewToLoad.getContext());
product_radiobuttonNO.setLayoutParams(lp_radio2);
product_radiobuttonNO.setTextColor(R.color.medium_gray);
product_radiogroup.addView(product_radiobuttonYES);
product_radiogroup.addView(product_radiobuttonNO);
and here is my result :
Has anyone any idea how to solve this ? Thanks in advance.
i don't think this solution is a nice one, but since you know the exact width and height of your radiogroup, just set the width of your radiobutton to width/2 instead of wrap_content and other attributes, such as weight and gravity on lp_radio1 and lp_radio2 can be removed.

android position TextViews programmatically

how do I position two programmatically created TextViews in a LinearLayout BESIDE each other? I tried the code below, but that way the "number" TextView is placed one line deeper compared to the "value" TextView, so the height of nlap LinearLayout changes. I need both TextViews to be at the same height, the "number" TextView should be on the left side an centered vertically. Any help is appreciated.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 1, 10, 1);
LinearLayout nlap = new LinearLayout(this);
nlap.setOrientation(LinearLayout.VERTICAL);
nlap.setLayoutParams(layoutParams);
TextView value = new TextView(this);
value.setText("Test");
value.setTextColor(Color.parseColor("#A60101"));
value.setTextSize(23);
value.setGravity(Gravity.CENTER);
value.setTypeface(font);
TextView number = new TextView(this);
number.setTextColor(Color.parseColor("#FFFFFF"));
number.setText("01");
nlap.addView(value);
nlap.addView(number);
You should use nlap.setOrientation(LinearLayout.HORIZONTAL); to achieve having textViews beside each other, if you use VERTICAL as you do, second one will always be below the first.

Align components in TableRow

First of all, all of my components have to be created at runtime. Well, I've got a TableLayout and inside this table view a bunch of TableRow. Inside every TableRow, there should be:
a Button at the right side having a given fixed height and a given fixed width
a TextView that fills the "rest" of the TableRow being allowed to wrap its text
The height of the TableRow should be the max of the Button height and the TableRow height. I either fail to set the Button height or only one of the elements (TableRow or Button) is displayed.
Maybe somebody could help me.
My current code:
// fetching the table layout
final TableLayout tl = (TableLayout) findViewById(R.id.ShoppingListTableLayout);
tl.removeAllViews();
// creating a single layout for every component
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.MATCH_PARENT);
tableRowParams.setMargins(0, 1, 0, 1);
TableRow.LayoutParams bParams = new TableRow.LayoutParams(m_Resources
.getDimensionPixelSize(R.dimen.ButtonWidth), m_DefaultButtonHeight_px);
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
Iterator<xy> itr = ...
while (itr.hasNext()) {
final xy = itr.next();
final TableRow tr = new TableRow(this);
tr.setLayoutParams(tableRowParams);
// 1. Child: TextView
final TextView CurrTxtView = new TextView(this);
CurrTxtView.setPadding(TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX, TV_PADDING_PX);
CurrTxtView.setTextSize(TV_TEXT_SIZE);
CurrTxtView.setTypeface(m_Font);
CurrTxtView.setTextColor(TEXT_ACTIVE_COLOR);
CurrTxtView.setText(CurrItem.GetName());
CurrTxtView.setLayoutParams(tvParams);
tr.addView(CurrTxtView, CHILD_INDEX_TV);
// 2. Child Button ("Edit" für aktive, "Delete" für inaktive)
final Button MyButton = new Button(this);
MyButton.setTextColor(TEXT_ACTIVE_COLOR;
MyButton.setText(m_Resources.getString(CurrItem.GetIsActive() ? R.string.EditString
: R.string.DeleteString));
MyButton.setLayoutParams(bParams);
MyButton.setTypeface(m_Font);
MyButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{... }
});
tr.addView(MyButton, CHILD_INDEX_B);
tr.setPadding(m_TableRowPadding_px, 0, m_TableRowPadding_px, 0);
tr.setGravity(Gravity.CENTER_VERTICAL);
tl.addView(tr, 0);
}
In this case only the buttons are displayed and even worse on the left side!
Assuming that you add the TextView and Button at the right indexes(CHILD_INDEX_TV and CHILD_INDEX_B are 0 and 1, although you could just drop them and just add the views in the correct order) a bad thing in your layout is the LayoutParams that you set to the TextView, the view that doesn't show up:
TableLayout.LayoutParams tvParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
First of all, the TextView is the child of a TableRow so you should set the TableRow.LayoutParams and not TableLayout.LayoutParams, secondly I don't understand why you suddenly decided to leave the TableLayout.LayoutParams and go for the android.view.ViewGroup.LayoutParams for the view's height. In the end it should be:
TableRow.LayoutParams tvParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
To push the Button to the right you could simply stretch the column with the TextView by using:
tl.setColumnStretchable(0, true);

how to make a 10X6 grid with each cell filled with a text view in android?

I am new in android.Please tell me how i can make a grid with 10 rows and 6 columns,and each cell filled with text views.I want to fill the text of each cell with different values,by click on a button.
i make a grid using table layout,with 10 rows and 60 text views,but i think it is not the right way to do this.
Please provide me source code for this.
Thanks in advance...my java file..and xml files are here...
TableLayout t1 =(TableLayout)findViewById(R.id.myTableLayout)
// TableRow [] tr = new TableRow[10];
TableRow tr = new TableRow(this);
TextView [] tv = new TextView[20];
int j=0;
int k=0;
//for inserting 10 rows..
for(int i=0;i<=10;i++)
{
String cnt;
tv[j] = new TextView(this);
TextView tv1 = new TextView(this);
TextView ttv = new TextView(this);
// TextView tv = new TextView(this);
//to put a space between cells
//tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv1.setPadding(20, 0, 0, 0);
tv1.setWidth(30);
tv1.setHeight(45);
tv1.setText("");
//to put digits in text views...
//ttv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ttv.setPadding(20, 0, 0, 0);
ttv.setWidth(60);
ttv.setHeight(45);
ttv.setTextColor(Color.DKGRAY);
ttv.setBackgroundColor(Color.CYAN);
ttv.setTextSize(25);
cnt=""+k+"";
ttv.setText(cnt);
k++;
// to put symbols store in an array using a button click...the code of button and array in dnt show here...
// tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tv[j].setPadding(20, 0, 0, 0);
tv[j].setWidth(60);
tv[j].setHeight(45);
tv[j].setTextColor(Color.YELLOW);
tv[j].setBackgroundColor(Color.GREEN);
tv[j].setTextSize(25);
//tv[j].setText("ab");
tr.setPadding(0, 1, 0, 1);
tr.addView(tv1);
tr.addView(ttv);
tr.addView(tv[j]);
j++;
tr.addView(tv1);
tr.addView(ttv);
tr.addView(tv[j]);
j++;
tr.addView(tv1);
tr.addView(ttv);
tr.addView(tv[j]);
t1.addView(tr);
}
You wont get the codez. So dont ask for it. I learnt it very early so suggesting you to try and do the coding yourself. We will only help if we see that you actually did do something yourself.
Anyways, using GridLayout would be better. Include TextView in your layout and in the onClick() of your button just change the text of the respective TextViews like this -
tv1.setText("1st changed");
tv2.setText("2nd changed");
and so on.
Ofcourse you need to find tv1 and tv2 ... also.
TextView tv1=(TextView) findViewById(R.id.tv1);
in your XML for gridview set one more attribute
android:numColumns="6"
for setting 6 columns in a row.
Then set adapter with length of 60 element and you will get 10X6 Grid

How to set the row in a table layout?

So far, I have code that dynamically creates my table within a horizontal scrollview. I want each item to appear in the same y location and so that you can only see one at a time. Right now, each column gets set, but the visual is like a staircase, where the next row is underneath and only moved over about 50px. Is there a way to force each column to take up the entire screen width? Also, can you set the row that the content appears in?
TableLayout tl = (TableLayout)findViewById(R.id.contentTable);
for(int i=0;i<itemIndexes.size();i++)
{
TableRow tr = new TableRow(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
if(globals.myHitetItems.get(itemIndexes.get(i)).image!=-1)
{
ImageView iv = new ImageView(this);
iv.setImageResource(globals.myHitetItems.get(itemIndexes.get(i)).image);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
else
{
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.noimage);
iv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(iv);
}
TextView tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).name);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tv = new TextView(this);
tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).content);
tv.setTextSize(20);
tv.setPadding(3, 3, 3, 3);
tv.setLayoutParams(new TableRow.LayoutParams(i));
tr.addView(tv);
tl.addView(tr);
}
As you have use the TableLayout and now you want each cell to consume appropriate same size throughout the table respectfully by comparing itself via row and column.
So, you have to set WEIGHT to each item and specify weight value = "1.0" or below it.

Categories

Resources