I hope my question is not too vague.
I have problem that I have been stuck for more than a couple of days. I have searched alot and despite the effort I have not found the solution.
My problem is the following:
I have a TableLayout and I populate my data dynamically with the total amount of items I have in my ArrayList. Momentarily my screen looks like this:
But what I would like to have is the following:
I have tried alot of different options for instance:
- Custom list adapter
- TableLayout
- GridLayout
- Alot of setLayoutParams options
Due to my effort and code that I have I would like to stick with TableLayout. GridLayout or any other ..Layout is also not a problem.
public void getTableLayoutDinnerProducts() {
tableLayout = (TableLayout)findViewById(R.id.tableLayoutDinner);
//gridLayout = (GridLayout)findViewById(R.id.gridLayoutDinner);
//gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
//gridLayout.setColumnCount(2);
//gridLayout.setRowCount(3);
//TableLayout.LayoutParams newTableLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
//newTableLayout.setMargins(400, 0, 0, 0);
// Creating a table row for each product in productController.
for (final Product product : productController.getStaticProducts()) {
if(product.getCategoryType() == CategoryType.Dinner){
// Create tablerows
tableRow = new TableRow(this);
tableRow.setId(product.getID() + 1);
tableRow.setBackgroundColor(Color.WHITE);
tableRow.setLayoutParams(new Toolbar.LayoutParams(300, 300));
// Create content for tablerows
textArray = new TextView(this);
textCaloriesArray = new TextView(this);
textProductArray = new TextView(this);
// Add image for each row
image = new ImageView(this);
bitmap = BitmapFactory.decodeResource(getResources(), product.getImage());
int width=400;
int height=400;
resizedbitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
image.setImageBitmap(resizedbitmap);
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
//image.setBackgroundResource(R.drawable.imagestyle);
image.setAdjustViewBounds(true);
tableRow.addView(image, 0, new TableRow.LayoutParams(400, 400));
textArray.setText(product.getName());
textCaloriesArray.setText(" Cal: " + String.valueOf(product.getCalories()));
textProductArray.setText(" Prod: " + String.valueOf(product.getProductType()));
textArray.setTextSize(18);
textArray.setTypeface(null, Typeface.BOLD);
textArray.setTextColor(Color.BLACK);
textArray.setPadding(0, 150, 0, 100);
//TableRow.LayoutParams params = (TableRow.LayoutParams)textArray.getLayoutParams();
//params.span = 4;
//textArray.setLayoutParams(params); // causes layout update
tableRow.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
tableRow.addView(textArray, 1, new TableRow.LayoutParams(200, 500));
tableRow.addView(textCaloriesArray, 2, new TableRow.LayoutParams(200, 400));
tableRow.addView(textProductArray, 3, new TableRow.LayoutParams(200, 400));
tableRow.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
final View background = v;
background.setBackgroundColor(Color.RED);
final Dialog dialog = new Dialog(DinnerScreen.this);
dialog.setContentView(R.layout.popup);
dialog.show();
dismisspopup = (Button)dialog.findViewById(R.id.dismissPopup);
dismisspopup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
background.setBackgroundDrawable(getResources().getDrawable(R.drawable.productimages));
}
});
removeproduct = (Button)dialog.findViewById(R.id.removeProduct);
removeproduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), DinnerScreen.class);
dialog.dismiss();
products = productController.getStaticProducts();
products.remove(product);
productController.setStaticProducts(products);
productController.getStaticProducts();
intent.putExtra("totalProducts", products);
intent.putExtra("totalCalories", String.valueOf(calorieCounter.getCountcalories()));
overridePendingTransition( 0, 0);
startActivity(intent);
overridePendingTransition( 0, 0);
showToastMessage("Product has been removed");
}
});
return false;
}
});
tableRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calorieCounter.addCalories(product.getCalories());
changeStatusBar(calorieCounter.getCountcalories());
}
});
//gridLayout.addView(tableRow);
tableLayout.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
}
Because I add these dynamically I don't have an .XML file to add the width or position.
There must be a solution for this problem. Only I cannot find it anymore.
Thanks in advance