-1

I have to add multiple TextViews dynamically.

Each TextView should fill the screen and the next TextView should be added beneath the first view and even it should occupy the entire screen .

I've used the following code to determine screen height and with at run time.

Code snippet

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;

        for(int i=0;i<name.size();i++)
        {
            text = new TextView(getApplicationContext());
            text.setText(class.get(i));
            text.setTextColor(Color.parseColor("#000000"));
            text.setLayoutParams(new LinearLayout.LayoutParams(width, height));
            text.setGravity(Gravity.CENTER);
            text.setTextSize(40);
            text.setClickable(true);
            LL.addView(text);
        }

Result:

1st view occupies entire screen and it is extended to next screen.

When 2nd view added to LinearLayout little gap is left over at the top.

This scenario can be understood only with screenshots.

Screenshots:

1st

enter image description here

enter image description here

slowly these screens are moving down.their height is increased by 30dp approx.

How could I get the exact height and width of the screen using DisplayMetrics.

EDIT:

Even by using this Answer screen occupies 30dp more than its height

Answer:

After calculating the screen width and height substract the height of Action bar from it.

This point is not mentioned. neither here nor in that duplicate question

Community
  • 1
  • 1
Prabs
  • 4,923
  • 6
  • 38
  • 59

1 Answers1

0

use LinearLayout.LayoutParams(LinearLayout.match_parent, LinearLayout.match_parent) instead of new LinearLayout.LayoutParams(width, height)

and also make your TextView object local instead of global

TextView text = new TextView(getApplicationContext());
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95