Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:44:08+00:00 2026-05-27T18:44:08+00:00

My problem is that a multiline text works properly in my xml template. If

  • 0

My problem is that a multiline text works properly in my xml template. If the text is wider than the screen it wraps into the next line. But if I try the same in Java, the text won’t be wrapped:

I have the following xml template for testing my prospective layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    >
    <ScrollView 
            android:id="@+id/scrollView"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal">
    <TableLayout 
            android:id="@+id/messageTable"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:stretchColumns="*"
            android:clickable="false">
        <TableRow
            android:layout_width="fill_parent"
            android:background="#777777"
            android:textColor="#FFFFFF"
            android:clickable="false">
            <TextView 
                 android:id="@+id/topic"
                 android:text="This is the topic"            
                 android:textColor="#FFFFFF"
                 android:layout_width="fill_parent"
                 android:textSize="14sp"
                 android:paddingLeft="5sp"
                 android:paddingRight="5sp"
                 android:paddingTop="2sp"
                 android:paddingBottom="2sp"
                 android:textStyle="bold" 
                 android:clickable="false"  
                 />
        </TableRow>
         <TableRow>
            <TextView
                 android:text="Mo., 26.12.2011, 10:00 - This is some example text. It will be wrapped in view. This works."
                 android:textSize="14sp"
                 android:paddingLeft="5sp"
                 android:paddingRight="5sp"
                 android:paddingTop="2sp"
                 android:paddingBottom="2sp"
                 android:scrollHorizontally="false"
                 android:inputType="textMultiLine"
                 android:longClickable="false"
                 android:clickable="false"
                 android:layout_width="0sp"
                 />
        </TableRow>
.
.
.

Now I’d like to use parts of these xml in my Java code. The table rows should be generated dynamically, depending on the data I will receive in the real application. Now the xml of the real application looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    >
    <ScrollView 
            android:id="@+id/scrollView"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal">
    <TableLayout 
            android:id="@+id/messageTable"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:stretchColumns="*"
            android:clickable="false">
    </TableLayout>
.
.
.

In code I use own types for my text rows, text topics and text bodies:

TopicTableRow:

public class TopicTableRow extends TableRow {

    public TopicTableRow(Context context) {
        super(context);

        setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        setBackgroundColor(Color.parseColor("#777777"));
        setClickable(false);
    }
}

TopicTextView:

public class TopicTextView extends TextView {

    public final String TAG = "TopicTextView";

    public TopicTextView(Context context) {
        super(context);

        PixelCalc pixelCalc = new PixelCalc(context, TAG);

        setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        setPadding(pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2), pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2));

        setTextColor(Color.parseColor("#FFFFFF"));
        setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
        setTypeface(Typeface.DEFAULT_BOLD);

        setClickable(false);
    }
}

and BodyTextView (which doesn’t wrap the text) looks like this:

public class BodyTextView extends TextView {

    public final String TAG = "BodyTextView";

    public BodyTextView(Context context) {
        super(context);

        PixelCalc pixelCalc = new PixelCalc(context, TAG);

        setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT));
        setPadding(pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2), pixelCalc.scaledPixelToPixel(5), pixelCalc.scaledPixelToPixel(2));

        setClickable(false);
        setLongClickable(false);

        setHorizontallyScrolling(false);

        setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f);
            setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
    }
}

(As you can see, the width settings are exactly the same as in the template XML).
In Java code I add a topic followed by a “body”. These are the methods. (Everything works fine but the text is not wrapped.):

private void addTopic(String text)  {
    TopicTableRow topicTableRow = new TopicTableRow(this);
    TopicTextView topicTextView = new TopicTextView(this);

    topicTextView.setText(text);

    topicTableRow.addView(topicTextView);
    messageTable.addView(topicTableRow);        
}

private void addStandardText()  {
    TableRow tableRow = new TableRow(this);
    BodyTextView bodyTextView = new BodyTextView(this);

    bodyTextView.setText(getString(R.string.standardText));

    tableRow.addView(bodyTextView);
    messageTable.addView(tableRow);
}

I have the suspicion that the line setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT)); of BodyTextView doesn’t work properly because when I set layoutWidth to 100 or 200, the width won’t be minimized and protrudes out of the screen. Why is this so? If I try shortening the topic to 200 (TopicTextView), I get a smaller bar, so the Java method should work properly, but in BodyTextView it somehow ignores the width. Does anybody have any idea why this is so? I already thought about this bug but I’m not sure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:44:09+00:00Added an answer on May 27, 2026 at 6:44 pm

    Solved:
    I don’t know why but…

    setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_MULTI_LINE);

    instead of

    setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);

    does the job.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having the problem that I cannot select a specific XML node which
I have a problem, with Zend_PDF multiline, my problem is that I can't write
I have a regular expression in Ruby that isn't working properly in multiline mode.
One problem that I come across regularly and yet don't have a solution to
A problem that we need to solve regularly at my workplace is how to
The problem that I am having has to do with the need to keep
A problem that has frequently come up in my career is I have some
Simple problem that I can't figure out... How can I print a '%' character
I have a problem that confuses my users, being that although an item is
This has been a problem that I haven't been able to figure out for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.