0

i am using following code. The code should show values of values in a row e.g if the values[] contain {A,B,C,D,E,F} It should show A B C D E F In relative layout. If i don't use LayoutParams pramas then all textViews are added over each other. How can i make them add to the left of each other

for (int i = 0 ; i < values.length ; i++)
      {
        TextView textView = new TextView(this);
        textView.setText(values[i].toString());
        textView.setTextColor(Color.BLACK);
        textView.setClickable(true);
        textView.setId(i);
        textView.setBackgroundDrawable(d);
        textView.setTextSize(24);
        LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        LayoutParams params  = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 50, 0, 0); 
        if (i>0)
        params.addRule(RelativeLayout.RIGHT_OF, i);
        textView.setLayoutParams(param);
        wordsLayout.addView(textView,params);
      }
    }

The problem is, when i use wordsLayout.addView(textView,params); Nothing is displayed

4
  • I'm sorry, but don't you want RIGHT_OF? or do you want {A,B,C,D,E,F} to display as F E D C B A ? Commented Apr 29, 2012 at 15:42
  • sorry, i have changed it to Right of but when i use params of relative layout, nothing is displayed. That is the problem. Problem edited Commented Apr 29, 2012 at 15:50
  • Are you declaring two sets of LayoutParams in your actual code? you should only be using one, so don't declare param and don't apply it to the textView. It could be that param is overwriting params when android adds the view to the viewgroup Commented Apr 29, 2012 at 15:55
  • Tried removing pram of textView, It didn't work Commented Apr 29, 2012 at 16:00

2 Answers 2

1

U R WORKING CODE...

LinearLayout wordsLayout = new LinearLayout(this);
        for (int i = 0 ; i < 5 ; i++)
        {
            TextView textView = new TextView(this);
            textView.setText(String.valueOf(i));
            textView.setTextColor(Color.RED);
            textView.setClickable(true);
            textView.setId(i);
            textView.setBackgroundDrawable(d);
            textView.setTextSize(24);
            LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            LayoutParams params  = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            params.setMargins(5, 50, 0, 0); 
            if (i>0)
                params.addRule(RelativeLayout.RIGHT_OF, i);
            textView.setLayoutParams(param);
            wordsLayout.addView(textView,params);
        }

        setContentView(wordsLayout);
Sign up to request clarification or add additional context in comments.

Comments

0

I would recommend putting each Textview inside its own Viewgroup which wraps around the Textviews content size. Have all the Viewgroups lined up next to each other the way you want all the visible textviews do. And make the inner textview-elements VISIBLE and GONE however you need to have them displayed. It's the least messy way of doing it in my opinion, and it secures the order of your textviews.

Comments

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.