I’m pulling some data from DB, and need to display it in form of bullet-ed list.
With the code:
for (int i=0; i<ingredcount; i++){
String ingredient = recipe.Ingredients.get(i);
View linearlayout = findViewById(R.id.llIngredients);
ImageView img = new ImageView(RecipeIngredientsDisplay.this);
TextView txt1 = new TextView(RecipeIngredientsDisplay.this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(30, 2, 15, 2);
LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp2.setMargins(10, 2, 15, 2);
img.setLayoutParams(llp2);
img.setImageResource(R.drawable.dot_purple);
txt1.setLayoutParams(llp);
txt1.setText(ingredient);
((LinearLayout)linearlayout).addView(img);
((LinearLayout) linearlayout).addView(txt1);
}
I have all the data correctly displayed, it’s just that my image is in new line, and text is bellow it.
So I’d like to programmatically set weight to my image and my text, so that they are both in one line.
Usually I do this using adapter and inflating listView, but combination of scrollView and ListView within ScrollView does not behave good, and focus is lost when I have lot of ingredients.
You could enclose your ImageView and TextView in another LinearLayout that is set to horizontal.
I probably screwed a little bit up with that code, but you get the idea? LinearLayout is horizontal by default, so you don’t need to change it in the new one that you create.