Adding text views dynamically to relativelayout.

While I add textviews to relative layout, at the end of the first line, the text does not look right.

as shown below:

picture...

here is my code for diplay text views.

public void showkeyword()
{
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout fl =  (RelativeLayout)findViewById(R.id.key_layout); 
    fl.removeAllViews();
    RelativeLayout.LayoutParams params ;

        //TextView key = (TextView) inflater.inflate(R.layout.tag_keyword,null);

i = 0;

   for(String s : alist)
   {   
        TextView textview = (TextView) inflater.inflate(R.layout.tag_keyword,null);
        textview.setText(s);
        textview.setId(2000 + i);      

        if (i == 0) {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            textview.setLayoutParams(rlp2);
            fl.addView(textview);

        } else {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
           // rlp2.addRule(RelativeLayout.ALIGN_BASELINE);
            rlp2.setMargins(10,0, 10,0);
            rlp2.addRule(RelativeLayout.RIGHT_OF, textview.getId() - 1);
            textview.setLayoutParams(rlp2);
            fl.addView(textview);  
        }          
       i++;
   }

}

      

I want to have something like this, kind of a tab implementation:

enter image description here

0


source to share


5 answers


Hope the following code will help you:

Functioning:

contactWrapper is a linear layout, we keep adding text views to these linear layouts one at a time and before adding find if contactWrapper has enough room to fit in the next TextView, unless a new linear layout is created and text elements are added to it.



Analyze the following code.

public void drawLayout() {
    int counter = 0;
    contactWrapperWidth = getResources().getDisplayMetrics().widthPixels;
    contactWrapper.setOrientation(LinearLayout.VERTICAL); 
    // contact wrapper is a linear Layout 
    // use LinearLayout contactWrapper = (LinearLayout) mView
    //          .findViewById(R.id.yourLinearLayout);
    currCounter = 0;
    currWidth = 0;
    isNewLine = false;

    row[currCounter] = new LinearLayout(getActivity());

    @SuppressWarnings("rawtypes")
    Iterator it = button.iterator();

    for (int i = 0; i < button.size(); i++) {
        it.next();
        row[currCounter].setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        currWidth += Integer
                .parseInt(button.get(i).get("width").toString());
        Log.i("Item width ", "i == "
                + button.get(i).get("width").toString());

        // contactWrapper.getw

        if (isNewLine) {
            if (currWidth < contactWrapperWidth) {
                row[currCounter]
                        .addView((View) button.get(i).get("button"));
                if (!it.hasNext()) {
                    contactWrapper.addView(row[currCounter]);
                } else {
                    if (contactWrapperWidth < (currWidth + Integer
                            .parseInt(button.get(i + 1).get("width")
                                    .toString()))) {
                        isNewLine = true;
                        contactWrapper.addView(row[currCounter]);
                        currCounter += 1;
                        row[currCounter] = new LinearLayout(getActivity());
                        currWidth = 0;
                    } else {
                        isNewLine = false;
                    }
                }
            } else {
                isNewLine = true;
                contactWrapper.addView(row[currCounter]);
                currCounter += 1;
                row[currCounter] = new LinearLayout(getActivity());
                currWidth = 0;
            }
        } else {
            if (currWidth < contactWrapperWidth) {
                if (!it.hasNext()) {

                    View view = (View) button.get(i).get("button");
                    row[currCounter].addView((View) button.get(i).get(
                            "button"));
                    contactWrapper.addView(row[currCounter]);
                } else {
                    View view = (View) button.get(i).get("button");

                    row[currCounter].addView((View) button.get(i).get(
                            "button"));
                    if (contactWrapperWidth < (currWidth + Integer
                            .parseInt(button.get(i + 1).get("width")
                                    .toString()))) {
                        isNewLine = true;
                        Logger.show(Log.INFO, "it.hasNext()",
                                "it.hasNext() contactWrapper");
                        contactWrapper.addView(row[currCounter]);
                        currCounter += 1;
                        row[currCounter] = new LinearLayout(getActivity());
                        currWidth = 0;
                    } else {
                        isNewLine = false;
                    }
                }
            } else {
                isNewLine = true;
                contactWrapper.addView(row[currCounter]);
                currCounter += 1;
                row[currCounter] = new LinearLayout(getActivity());
                currWidth = 0;
            }
        }
        counter++;
    }
}

      

0


source


Finally I can remove this error using @ kailas's idea

Here's where I post my method:



public void showkeyword() {
    int counter = 0;
    int screenWidth = getResources().getDisplayMetrics().widthPixels;
    final RelativeLayout contactWrapper =  (RelativeLayout)findViewById(R.id.key_layout);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     RelativeLayout.LayoutParams buttonparams = new RelativeLayout.LayoutParams(
          150,
          80);

     int i = 0;



    contactWrapper.removeAllViews();
    // contact wrapper is a linear Layout 
    // use LinearLayout contactWrapper = (LinearLayout) mView
    //          .findViewById(R.id.yourLinearLayout);
    int currCounter = 0;
    int currWidth = 0;
    boolean isNewLine = false;
    boolean firstLine = true;

    for(final String s : alist)
    {
            TextView textview = new TextView(this);

           RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                      RelativeLayout.LayoutParams.WRAP_CONTENT);

            rlp1.setMargins(7, 5, 7, 0);
            textview.setText(s);
            textview.setId(2000 + i);
            textview.setBackgroundColor(Color.DKGRAY);
            textview.setTextColor(Color.CYAN);
            textview.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
             contactWrapper.removeView(v);
             alist.remove(s);
                }

              });

           int width = s.length()*15;
            if((currWidth+width+150)<=screenWidth)
            {
                currWidth += width+10;
                isNewLine = false;
                currCounter++;
            }
            else{
                currWidth = width+14;
                firstLine = false ;
                isNewLine = true;
                currCounter=1;
            }



            if(i==0)
            {     rlp1.addRule(RelativeLayout.ALIGN_START);
                  textview.setLayoutParams(rlp1);
                  contactWrapper.addView(textview);
            }
            else if(isNewLine){
                 rlp1.addRule(RelativeLayout.ALIGN_LEFT);
                 rlp1.addRule(RelativeLayout.BELOW,2000-1+i );
                 textview.setLayoutParams(rlp1);
                 contactWrapper.addView(textview);
            }
            else if(firstLine)
            {
                 rlp1.addRule(RelativeLayout.RIGHT_OF,2000-1+i );
                 textview.setLayoutParams(rlp1);
                 contactWrapper.addView(textview);

            }
            else{

                 rlp1.addRule(RelativeLayout.RIGHT_OF,2000-1+i );
                 rlp1.addRule(RelativeLayout.BELOW,2000-currCounter+i );
                 textview.setLayoutParams(rlp1);
                 contactWrapper.addView(textview);

            }



            i++;
    }

     buttonparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
     buttonparams.addRule(RelativeLayout.ALIGN_BASELINE,2000-1+i);
     Button clearbtn = new Button(this);


        clearbtn.setText("clear");
       // clearbtn.setBackgroundColor(Color.RED);
       // clearbtn.setTextColor(Color.CYAN);
        clearbtn.setLayoutParams(buttonparams);
        clearbtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
         contactWrapper.removeAllViews();
         alist.clear();
            }

          });


   contactWrapper.addView(clearbtn) ;

}

      

+1


source


Try using LinearLayout and fixed height (using dip module) instead of WRAP_CONTENT

0


source


Try to remove

rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

      

because the behavior you want is the default behavior.

Place the orientation horizontally in a relative layout.

0


source


Try to put all the text you want on one line and then put all that text in just one text file.

The best way is to use StringBuilder to concat the text:

StringBuilder sb = new StringBuilder();
sb.append("str1");
sb.append("str2");

      

and then put the string in the textview.setText (sb.toString ());

0


source







All Articles