1

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?

Ben Kleywegt
  • 46
  • 1
  • 5
  • You need to save the padding numbers in the bundle inside onSaveInstanceState and read the numbers inside onCreate. look here: :http://developer.android.com/training/basics/activity-lifecycle/recreating.htm – yshahak Jul 16 '15 at 03:49
  • Sorry for not being clear enough - The margin/padding is not being applied when the list is first displayed either - I have edited the question – Ben Kleywegt Jul 16 '15 at 04:24

2 Answers2

0

Handling Runtime Changes

When Screen orientation such a change occurs, Android restarts the running Activity (onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

To properly handle a restart, it is important that your activity restores its previous state through the normal Activity lifecycle, in which Android calls onSaveInstanceState() before it destroys your activity so that you can save data about the application state. You can then restore the state during onCreate() or onRestoreInstanceState().

http://developer.android.com/guide/topics/resources/runtime-changes.html

Community
  • 1
  • 1
Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
0

in code

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,
                         LinearLayout.LayoutParams.WRAP_CONTENT, 7);
params.setMargins(1, 1, 1, 10);
params.gravity = Gravity.FILL; // this is where i come in
txtProduct.setLayoutParams(params);
txtProduct.invalidate(); // edits
txtProduct.requestLayout();//edits

the type of Gravity is up to you now with rotation you need to override onOnrientationChanged() (something like that) and call that same code there

Edit

base on your post runnable code i am thinking of a workaround, i do not think it will work but worth it.. since txtProduct.getLineCount() returns 0 after layout pass, i am improvising

if(txtProduct.getLineCount()==0){ //if false you run your previous code without post
     int textSize = txtProduct.getTextSize()/2,mw,lines;
     // for the textsize i am thinking of a rectangle
     if(txtProduct.getMeasuredWidth() > 0) //if this line checks out 0 the code is trash

  //assume none where zero
  int fillW = textSize * txtProduct.getText().toString().length();
  if(fillW > mw){ //my logic here is we have more than one line
     if(fillW / mw > -1){ //
      lines = fillW/mv;
       if(fillW%mv !=0){// it is not a factor which means there is another line trending
           lines++;
           // now add your normal code here without post to check if lines is what you want
       }
     }
   }
 }else{ //add your code without post

see if this works,

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • I tried `Gravity.LEFT` and `Gravity.FILL` but unfortunately it still doesn't apply the margin/padding until I begin scrolling down the list – Ben Kleywegt Jul 16 '15 at 04:39
  • i am certain it should work, try gravities with center or center horinzontal etc and see @BenKleywegt and quick question why are you putting that line of code in a runnable? post? any reason? – Elltz Jul 16 '15 at 04:44
  • I put it in a post because of `txtProduct.getLineCount()` ([link](http://stackoverflow.com/questions/3528790/textview-getlinecount-always-0-in-android)). I tried some different gravities but it still isn't working. – Ben Kleywegt Jul 16 '15 at 04:57
  • can you please check some edit i have made and see if it actually works @BenKleywegt – Elltz Jul 16 '15 at 05:43
  • unfortunately `txtProduct.getMeasuredWidth()` will return zero because the TextView has 0 width and layout_weight set i.e. `android:layout_width="0dp" android:layout_weight="7"` so the width isn't known until after the layout is drawn - requiring a post runnable to get the measured width. I can't really set a fixed width because I want the layout to scale out to both large and small screens – Ben Kleywegt Jul 16 '15 at 06:00
  • hmm your question seems interesting, try this last one then i am out of tricks, use my first codes as you did and this time add invalidate and maybe requestLayout() and see if it works, i will edit it an put it in@BenKleywegt – Elltz Jul 16 '15 at 07:14
  • @EIItz I think that's close - I tried those methods previously but didn't have any success but I'll keep experimenting tomorrow – Ben Kleywegt Jul 16 '15 at 07:50