Allan Macmillan
Allan Macmillan

Reputation: 1491

TextView too big for Screen

I have a TextView that contains 32 lines of text. On rotation to landscape the TextView becomes too large for the screen and therefore I would like it to split into 2, 16 line TextViews but do not know if this is possible. This is what I have so far.

I know I could do a test to see if getHeight() > screen height but even if it is, I wouldn't know what to do.

        TextView displayMethod = new TextView(getActivity());
        displayMethod.setTextColor(Color.BLACK);
        displayMethod.setClickable(false);
        displayMethod.setBackgroundColor(Color.WHITE);
        displayMethod.setTextSize(14);
        displayMethod.setTypeface(Typeface.MONOSPACE);  

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10,20,10, 0);
        displayMethod.setLayoutParams(params);

        int i = 0;      
        while (i < 32){

            String x = method.getNextLine();

            displayMethod.append(x + "\n");             
            i++;
        }
        linLayout.addView(displayMethod);

Upvotes: 0

Views: 486

Answers (1)

Yjay
Yjay

Reputation: 2737

Having a behavior of splitting the TextView in two pieces when viewed in landscape would be a great way to use all of your screen real-estate, and is actually very simple.

Next to your res/layout folder, create a new layout folder named layout-land, and in here put your new layout containing two different TextView objects. Note that the name of the new layout file in layout-land needs to be exactly the same as your original layout from the layout folder. (Copy-paste actually suggested here).

From here, you have two options:

-- Update your original layout in your layout folder to have two TextView objects stacked vertically. Make sure that the ids of your TextViews are the same in this layout compared to your landscape layout in layout-land. No code changes are required in this option.

-- Leave your original layout as is, but check for the existance of the landscape-specific TextView in your Activity and fill either one or two TextViews appropriately. For example...

TextView textView = findViewById(R.id.textView);
TextView textViewLandscapeOnly = findViewById(R.id.textViewLandscapeOnly);

if(textViewLandscapeOnly == null) {
    //We're in portrait mode, so only fill the first text view with all 32 lines.
} else {
    //We're in landscape mode, so fill both text views, each with 16 lines.
}

Upvotes: 2

Related Questions