I am trying to set a margin in a text view and padding in a linear layout in a list view adapter. Basically, if the text view has 2 or less lines of text I am creating a margin/padding for that list item.
Here is the code:
public class StockCountListAdapter extends ArrayAdapter<StockCountItem> {
private TextView txtProduct;
private LinearLayout llStockCountItem;
public StockCountListAdapter(Context context, int textViewResourceId, List<StockCountItem> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.stock_count_list_item, parent, false);
txtProduct = (TextView) rowView.findViewById(R.id.stock_count_item_product);
llStockCountItem = (LinearLayout) rowView.findViewById(R.id.ll_stock_count_item);
StockCountItem item = getItem(position);
txtProduct.setText(item.Product);
llStockCountItem.post(new Runnable() {
@Override
public void run() {
if (txtProduct.getLineCount() <= 2) {
llStockCountItem.setPadding(0, 0, 0, 10);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 7);
params.setMargins(1, 1, 1, 10);
txtProduct.setLayoutParams(params);
}
}
});
return rowView;
}
The margin/padding is applied when the list is scrolled, but not when the list is first displayed or on orientation change.
How can I get it to apply the margin/padding on activity load or orientation change?