Stillie
Stillie

Reputation: 2676

Adding TextViews programmatically

I have found a couple ways to do this, but my issue is that my code has to be a LinearLayout with a horizontal orientation. So what happens is the dynamically created TextViews go off the screen.

The code I have is below:

   mProductAttrLayout.setOrientation(LinearLayout.HORIZONTAL);
   mProductAttrLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    for (ProductAttribute productAttribute : aProductAttributes) {
      String name = productAttribute.getName();
      TextView attr = new TextView(getContext());
      attr.setText(name);
      attr.setPadding(8, 8, 8, 8);
      attr.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
      attr.setTextColor(getResources().getColor(R.color.black));
      mProductAttrLayout.addView(attr);


      for (int i = 0; i < productAttribute.getValues().size(); i++) {
        TextView value = new TextView(getContext());
        value.setText(productAttribute.getValues().get(i));
        value.setTextColor(getResources().getColor(R.color.black));
        value.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        value.setPadding(8, 8, 8, 8);
        mProductAttrLayout.addView(value);
      }
    }

What I currently have is:

| name: value, value, value, val--|

what I need is something like:

name: value, value(unknown number of values)

but I need it to go the the next line in the screen if it's too wide like below:

| name: value, value, value,---|

| value, value, value. ------------|

Hope you can understand what I need?

Upvotes: 0

Views: 120

Answers (5)

Stillie
Stillie

Reputation: 2676

I know this is an old question I asked long ago but they (Google) have given us a solution to this problem using FlexboxLayout with the flexWrap="wrap" attribute.

This is for anyone else that finds this question with the same problem.

Example in XML:

<com.google.android.flexbox.FlexboxLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:flexWrap="wrap" />

For more info you can visit the github repo here FlexboxLayout Repo

It works like this:

enter image description here

Can read more over here:

Build flexible layouts with FlexboxLayout

Upvotes: 0

Oleksandr
Oleksandr

Reputation: 912

you can use SpannableStringBuilder and TextAppearanceSpan. Example code:

        List<String> manyStrings = Arrays.asList(new String[]{"name", "value", "value", "value"});
        TextView textView = (TextView) view.findViewById(R.id.textView);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        for (int i = 0; i < manyStrings.size(); i++) {
            String text;
            if (i == 0) {
                text = manyStrings.get(i) + ": ";
            } else if (i == manyStrings.size() - 1) {
                text = manyStrings.get(i);
            } else {
                text = manyStrings.get(i) + ", ";
            }

            if (i == 0) {
                spannableStringBuilder.append(text);
                spannableStringBuilder.setSpan(new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_DeviceDefault_Large), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                int oldLength = spannableStringBuilder.length();
                spannableStringBuilder.append(text);
                spannableStringBuilder.setSpan(new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_DeviceDefault_Small), oldLength, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        textView.setText(spannableStringBuilder);

Read more about SpannableStringBuilder and TextAppearanceSpan: http://developer.android.com/reference/android/text/SpannableStringBuilder.html http://developer.android.com/reference/android/text/style/TextAppearanceSpan.html

Upvotes: 0

Sackurise
Sackurise

Reputation: 2814

You have to go with Creating a Customized LinearLayout class. Refer the link for some sample ideas.

How can I add a TextView to a LinearLayout dynamically in Android?

Upvotes: 0

Swati Singh
Swati Singh

Reputation: 420

Create a flow layout in OnCreate:-

   muscle_gp_tag_flow_layout = new FlowLayout(mContext);
   //add the flow layout to your main layout

Call the method first time and whenever you want to refresh the view:-

public void createShowSpannableStringMuscleGroup(List<MuscleGroup> muscleGroups) {

        // clears the flowlayout views
        muscle_gp_tag_flow_layout.removeAllViews();
        for (int countGroup = 0; countGroup < muscleGroups.size(); countGroup++) {
            MuscleGroup group = muscleGroups.get(countGroup);
            if (group.getName() != null && group.getName().length() > 0) {

//                inflating the textview views in flow layout
               TextView textView=new textView(context);
//set color, background for textview
              textView.setText(group.getName().toString());

//                adding views to flowlayout muscle group
                muscle_gp_tag_flow_layout.addView(mView);
            }
        }

Upvotes: 0

F43nd1r
F43nd1r

Reputation: 7759

LinearLayout is the wrong choice here.

You could either write your own layout or use existing work.

What you probably want is usually called a FlowLayout, like this.

Custom layouts can be used like any other Layout. E.g. you can add views with layout.addView. Noramlly the only difference in use is reflected in the custom layoutParams.

Upvotes: 1

Related Questions