Search code examples
androidtextviewandroid-arrayadapter

When does a textview gets a width inside an array adapter in android?


I have a ListView which is filled by a custom array adapter. I use the holder design pattern to assign values to a textview inside each row.

I want to set the margin of the TextView programmatically within a custom ArrayAdapter. This margin(left) is calculated by using the width of the TextView.

But somehow the getWidth() method on the textview returns 0

Here's my code:

public class BudgetAdapter extends ArrayAdapter<Budget> {

    private Context context;
    private int layoutResourceId;
    private List<Budget> data = null;

    public BudgetAdapter(Context context, int layoutResourceId, List<Budget> data) {
        super(context, layoutResourceId, data);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.data = data;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        BudgetHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new BudgetHolder();
            holder.titleText = (TextView)row.findViewById(R.id.budget_list_item_title_label);
            holder.remainingText = (TextView)row.findViewById(R.id.budget_list_item_remaining_label);
            holder.remainingBar = (RemainingBar)row.findViewById(R.id.budget_list_item_remaining_bar);
            row.setTag(holder);
        }
        else
        {
            holder = (BudgetHolder)row.getTag();
        }

        Budget budget = data.get(position);
        holder.titleText.setText(budget.getShortDescription());
        holder.remainingText.setText(budget.getRemainingAsString());
        holder.remainingBar.setRemaining(budget.getRemaining());
        holder.remainingBar.setTotal(budget.getTotal());

        Log.i("Main", "The width of the TextView is: " + holder.remainingText.getWidth());
        return row;
    }


    static class BudgetHolder {
        TextView titleText;
        TextView remainingText;
        RemainingBar remainingBar;
    }
}

So my question is when does the TextView get a width?


Solution

  • when does the TextView get a width?

    If the View class is generated dynamically (via code) they will get their width after they are displayed on the screen.

    How about your case?

    Sorry, i dont know. Hope someone will help us :)

    But i might help you with code. Try to change :

    holder.remainingText.getWidth());
    

    To something like this :

    holder.remainingText.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    holder.remainingText.getLayoutParams().width;
    holder.remainingText.getLayoutParams().height;
    

    You should get the text's width and height.