0

While I am adding textviews to relative layout, at the end of first line, the textview is going wrong.

as shown in below:

picture.

here is my code to diplay textviews.

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 wish to have something like this, sort of a tab implementation:

enter image description here

16
  • 2
    How do you want them to be? They should be aligned vertically? Commented Jun 5, 2014 at 8:33
  • 1
    @Aniruddha it's like adding tags in stakoverflow. so i want show them in horizontally Commented Jun 5, 2014 at 8:35
  • Use LinerarLayout instead of RelativeLayout, it will solve your problem. Commented Jun 5, 2014 at 8:39
  • Yes @DipakKeshariya is right use LinerarLayout. Commented Jun 5, 2014 at 8:39
  • Then why are you not using LinearLayout with horizontal orientation? Try decreasing the textsize, it will help you out. Commented Jun 5, 2014 at 8:39

5 Answers 5

1

Hope the following code will help you out:

Functioning:

contactWrapper is a linear layout, we go on adding the textviews into these linear layouts one by one and before adding find whether the contactWrapper has space enough to put in the next TextView, if not a new linear layout is created and the textViews are added into it.

Take time analyzing 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++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Finally I am able to remove that bug using idea by @kailas

Here I am posting 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) ;

}

Comments

0

Try to remove

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

because the behavior you want is the default behavior.

Put Orientation horinzontal in the relative layout.

1 Comment

textview overriding previous textview
0

Try using a LinearLayout and a fixed height (using the dip unit) instead of WRAP_CONTENT

Comments

0

Try to put all the text you want in one String and then put all this text in only one TextView.

The best way is using StringBuilder to concat the text:

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

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

1 Comment

actually i want implement onclick listener to each textview.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.