0

I set my view(button) height at runtime in baseAdapter getView().

But My layoutParams set only for last element of getview.

My getView code:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.listrow_round_table, null);
        btnRoundTables = (Button) vi.findViewById(R.id.btn_table);

     //set the btn height at runtime
        ViewTreeObserver viewTreeObserver = btnRoundTables.getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                btnRoundTables.getViewTreeObserver().removeOnPreDrawListener(this);

                int btnWidth=btnRoundTables.getMeasuredWidth();
                btnRoundTables.setLayoutParams(new LinearLayout.LayoutParams(btnWidth, btnWidth));
                return true;
            }
        });
       HashMap<String, String> mainMap = data.get(position);

        // Setting all values in listview
        btnRoundTables.setText(mainMap.get(Keys.KEY_TITLE));
        btnRoundTables.setTag(mainMap.get(Keys.KEY_TABLE_ID));
        btnRoundTables.setOnClickListener(this);
        Log.d("table name" + mainMap.get(Keys.KEY_TITLE), "listadapterGrid");

    }
        return vi;
}

My output:

enter image description here

Thanks for help..

1
  • Please provide your full getView code. Your code seems to be wrong.
    – 2hamed
    Sep 20, 2015 at 6:09

1 Answer 1

1

You're doing it wrong in getView. You must use the ViewHolder pattern in your adapters.

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolder holder;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.listrow_round_table, null);
        holder = new ViewHolder();
        holder.btnRoundTables = (Button) vi.findViewById(R.id.btn_table);
        vi.setTag(holder);
    }else{
       holder = (ViewHolder)vi.getTag();
    }

     //set the btn height at runtime
        ViewTreeObserver viewTreeObserver = holder.btnRoundTables.getViewTreeObserver();
        viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                holder.btnRoundTables.getViewTreeObserver().removeOnPreDrawListener(this);

                int btnWidth=holder.btnRoundTables.getMeasuredWidth();
                holder.btnRoundTables.setLayoutParams(new LinearLayout.LayoutParams(btnWidth, btnWidth));
                return true;
            }
        });
       HashMap<String, String> mainMap = data.get(position);

        // Setting all values in listview
        holder.btnRoundTables.setText(mainMap.get(Keys.KEY_TITLE));
        holder.btnRoundTables.setTag(mainMap.get(Keys.KEY_TABLE_ID));
        holder.btnRoundTables.setOnClickListener(this);
        Log.d("table name" + mainMap.get(Keys.KEY_TITLE), "listadapterGrid");

    }
        return vi;
}

public class ViewHolder{
    Button btnRoundTables;
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.