简体   繁体   中英

Adding TextViews inside horizontal LinearLayout dynamically

Click here to see the image

In the profile page of my application, I want to have an interest section as shown in the image. The user has a list of interest under his profile. I want to show his/her interests inside a horizontal LinearLayout. I have created an array of TextViews and add them dynamically inside the parent LinearLayout, but I do not want to add the TextViews when there is no more space. Instead, I want to add a TextView showing the number of remaining interests.

As shown in the picture (use the image link), the user had 24 interests, 4 of them fit horizontally on the same line and last TextView(+20) shows the number of remaining interests on the same line.

String interestList[]={"Travel","Music","Photography","Sports","Dance","Animals","SciFi Movies"};
    int interestWidth =0, parentWidth=interestLinearLayout.getWidth();
    for(String interest: interestList) {
        TextView textView = new TextView(MainActivity.this);
        textView.setBackground(getResources().getDrawable(R.drawable.interests_bg));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(2,0,10,2);
        textView.setLayoutParams(params);
        textView.setPadding(2, 2, 2, 2);
        textView.setText(" "+interest+" ");
        textView.setTextColor(getResources().getColor(R.color.white));
        textView.setIncludeFontPadding(true);
        interestWidth += textView.getWidth();
        if(interestWidth<parentWidth) //both are 0 on first iteration of loop???
            interestLinearLayout.addView(textView);
        else
            break;
    }

You can add views dynamically but first you need a reference to the parent view to which you want to add a view.

You can do this by just using findViewById. Assuming it's a linear layout,

LinearLayout parent = findViewById(R.id.parent);
// Then create a textview
TextView textView = new TextView(this);
// Add the view to the parent
parent.addView(textView);

And that's it! To change properties about the TextView, you can use TextView getters and setters. If you want to change the margin, padding or height of width of the TextView, use LayoutParams

// Remember that I'm using LinearLayout.LayoutParams because  the parent of the ttextview is a  LinearLayout
LinearLayout.LayourParams params = textView.getLayoutParams();
// Remember these values are in pixels
params.height = 100;
params.width = 200;

There are tons of problems using this method, such as setting height and width in pixels instead of dps. And writing a lot of code when you could have done it in xml. You can however make this much easier by creating an xml file in your res/layout and then inflating it and finally adding it to the parent.

You can do this by -

// First get the layout inflater
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView textView = inflater.inflate(R.layout.myTextView, null);
linearLayout.addView(textView);

Finally addressing your problem about adding only enough views that the linearLayout doesn't go beyond the screen width.

The easiest solution is, to loop through the interest list and in every iteration of the loop, measure the combined width of the TextViews created and then checking whether it exceeds the width of the linearLayout.

It would look similar to this -

int combinedWidth = 0;
int linearLayoutWidth = linearLayout.getMeasuredWidth();
for(String interest : interests){
    TextView view = inflater.inflate(R.layout.textview, null);
    combinedWidth += textView.getMeasuredWidth();
    view.setText(interest);
    if(combinedWidth > linearLayoutWidth){
        // No need to add more views
        break;
    }else{
        linearLayout.addView(textView);
    }
}

However, the above solution may or may not work depending on when it is executed. So post the activity code along with the xml file so that I can better answer your question.

The interestWidth and parentWidth are initially 0 because they have not been laid out when getWidth is called.

get width for dynamically created textViews

The above link helped me getting width of dynamically created textViews from interestList.

And by using ViewTreeObserver on interestLinearLayout I was able to get the width of LinearLayout after it was laid out.

Finally, the above code should be modified as below to add textViews from JAVA inside a LinearLayout.

       final LinearLayout interestLinearLayout = findViewById(R.id.interests);
       interestLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                interestLinearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                String interestList[]={"Travel","Music","Photography","Sports","Dance","Animals","SciFi Movies"};
                int interestWidth =0;
                int parentWidth = interestLinearLayout.getWidth(); // got width inside view tree observer for linearlayout
                for(String interest: interestList) {
                    TextView textView = new TextView(MainActivity.this);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.setMargins(2,0,10,2);
                    textView.setLayoutParams(params);
                    textView.setPadding(2, 2, 2, 2);
                    textView.setText(interest);
                    textView.setIncludeFontPadding(true);
                    textView.measure(0,0); //using approach mentioned in link to get width of text views
                    interestWidth += textView.getMeasuredWidth(); 
                    if(interestWidth<parentWidth)
                        interestLinearLayout.addView(textView);
                    else
                        break;
                }
            }
        });

To create a LinearLayout,

LinearLayout layout = new LinearLayout(MainActivity.this);

To set background color of a layout,

layout.setBackgroundColor(Color.parseColor("#135517"));

To set width and height of the layout,

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(15, 5, 5, 5);
layout.setLayoutParams(params);

The orientation,

layout.setOrientation(LinearLayout.HORIZONTAL);          
layout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
layout.setPadding(10, 10, 5, 5);

Then create a textview,

    TextView textView = new TextView(this);
textView.setLayoutParams(params);
        textView.setPadding(2, 2, 2, 2);
        textView.setText(" "your" ");
        textView.setTextColor(getResources().getColor(R.color.white));
        textView.setIncludeFontPadding(true);

Add the view to the parent,

layout.addView(textView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM