BAIJU SHARMA
BAIJU SHARMA

Reputation: 296

How to set TextView width for n number of TextView in a row dynamically of ListView in android?

I want to create a Listview that will hold 1 to 'n' nos of TextView data horizontally, this Textview data and width size will come from DB? I don't know how to set the width of TextView dynamically with holder?

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    ViewHolder holder  = new ViewHolder();

    if (view == null) {
        view = View.inflate(cntxts, R.layout.list_header_col, null);
        String [] ColNmae= (String[]) objects.get(0);

        int Width= Integer.parseInt(ColNmae[2].toString());
        LinearLayout layout=new LinearLayout(cntxts);
       // layout.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        TextView Textview = new TextView(cntxts);
        HorizontalScrollView hr = new HorizontalScrollView(cntxts);
        for(int i=0;i<objects.size();i++) {
            holder.Textview=new TextView(cntxts);
            layout.setLayoutParams(new RelativeLayout.LayoutParams(Width,80));
            layout.addView(holder.Textview);
        }
        hr.addView(layout);
        view = hr;
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    notifyDataSetChanged();
    final String[] objMain = (String[]) getItem(position);
        holder.Textview.setText(objMain[0]);
    return view;
}

Upvotes: 0

Views: 297

Answers (3)

BAIJU SHARMA
BAIJU SHARMA

Reputation: 296

Thanks for your help I have got the solution.

 try {
                LayoutInflater mInflater = (LayoutInflater)
                        cntxts.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = mInflater.inflate(R.layout.list_header_col, null);

                holder.layoutmain = (LinearLayout) view.findViewById(R.id.lyt_header);

                view.setTag(holder);

                String[] ColNmae = (String[]) objects.get(0);

                int Width = Integer.parseInt(ColNmae[2].toString());
                if (holder.layoutmain != null && holder.layoutmain.getChildCount() > 0)
                    holder.layoutmain.removeAllViews();

                for (int i = 0; i < objects.size(); i++) {
                    final String[] objMain = (String[]) getItem(i);
                    TextView textView = new TextView(cntxts);
                    textView.setText(objMain[0]);
                    holder.layoutmain.addView(textView);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

Upvotes: 1

Deekshith R Moolya
Deekshith R Moolya

Reputation: 971

try this,

 yourholder.textView.getLayoutParams().width = dynamic width value;

Upvotes: 0

Victor Viola
Victor Viola

Reputation: 585

Your data needs to come from somewhere. To fulfill a Spinner you have the following options:

  • Put the values in the layout file
  • Put in the code
    • Building your own values
    • Retrieving it from the DB

Upvotes: 0

Related Questions