Search code examples
androidtextviewdynamic-programmingandroid-relativelayout

Adding Textviews to Relative Layout based on screen size


I'm trying to dynamically create textviews in a relative layout.

Question: How to add them to RIGHT_OF if there is enough space on the screen or add them BELOW if there isn't enough space.

    void addTravelActivities()
    {
        RelativeLayout travel_act_layout=(RelativeLayout) findViewById(R.id.rl_for_travelact);
        
        for (int i=0;i<=15;i++)
        {
            TextView tv=new TextView(this);
            tv.setId(i);
            tv.setText(traveliconName.get(i));
            tv.setBackgroundResource(R.drawable.round_rect_shape);
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.plus_profile,0, 0, 0);
            tv.setCompoundDrawablePadding(10);
            tv.setTextSize(16f);
            tv.setTextColor(this.getResources().getColor(R.color.tripoto_orange));
            tv.measure(0, 0);
            
    //              tv.setBackground(this.getResources().getDrawable(R.drawable.round_rect_shape,null));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            if(i==0)
            {
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.setMargins(8,0, 8, 8);
                travel_act_layout.addView(tv,params);
                /*tv.addOnLayoutChangeListener(new OnLayoutChangeListener() {
                    
                    @Override
                    public void onLayoutChange(View v, int left, int top, int right,
                            int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                        // TODO Auto-generated method stub
                    int width=right-left;
                    Toast.makeText(NewProfileSettings.this,width+"", Toast.LENGTH_SHORT).show();
                    }
                });*/
            }
            else if(getWindowSize(tv.getMeasuredWidth())==true) {
            params.addRule(RelativeLayout.RIGHT_OF,i-1);
            params.setMargins(8,0, 8, 8);
            sum += tv.getMeasuredWidth();
            travel_act_layout.addView(tv,params);
    //              Toast.makeText(this, tv.getMeasuredWidth()+"",        Toast.LENGTH_SHORT).show();
            }
            else if(getWindowSize(tv.getMeasuredWidth())==false)
            {
                params.addRule(RelativeLayout.BELOW,i-1);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.setMargins(8,0,8,8);
                travel_act_layout.addView(tv,params);
            sum=0;
            }
                
        }
        
    }
    
    Boolean getWindowSize(int textViewWidth)
    {
        
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        int layout_width=width-40-textViewWidth;
        if(layout_width>sum)
        return true;
        else
            return false;
        
    }

I'm probably close to doing it because I've gotten to this

Output


Solution

  • Answer:

    Yes Flow Layout is the answer as suggested by @Frank N. Stein. But you'll find a lot of libraries that would help you perform this. If you want to refrain from using libraries here is the simplest way I found while researching :

    https://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/

    Just a class and a few changes in your layout and it works just as you want.