Charles Widney
Charles Widney

Reputation: 21

TextView to show two string variables on two lines

I am trying to show two string variables, apple and banana inside a TextView. I have tried to add the newline character and doesn't work. The only thing I haven't tried is to save the variables to files and read the files into the TextView.

Here is my java code:`

public class MainActivity extends ActionBarActivity {
    private Button button;
    TextView tv;
    String apple = "bananas" + "\n";
    String banana = "apples" + "\n";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv1);
        activatebutton();
    }

    private void activatebutton() {
        // TODO Auto-generated method stub
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // code here

                tv.setText(apple);
                tv.setMovementMethod(new ScrollingMovementMethod());
                tv.setText(banana, null);
                tv.setMovementMethod(new ScrollingMovementMethod());
            }
        });
    }
}

Here is the Xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="73dp"
        android:layout_marginTop="44dp"
        android:text="@string/button" />

    <TextView
       android:id="@+id/tv1"
       android:layout_width="match_parent"
       android:layout_height="100dp"
       android:layout_alignParentLeft="true"
       android:layout_below="@+id/button1"
       android:layout_marginTop="90dp"
       android:background="#FFFF00"
       android:gravity="top"
       android:lines="10"
       android:maxLines="10"
       android:scrollbarStyle="insideOverlay"
       android:scrollbars="vertical"
       android:textSize="18sp" />

</RelativeLayout>

What am I doing wrong?

Upvotes: 0

Views: 1608

Answers (3)

Simon Chius
Simon Chius

Reputation: 476

In the XML file Edit the EditText property as follows

 <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/two"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textMultiLine" />

In your MainActivity.java file

EditText editText;
String apple = "bananas" ;
String banana = "apples" ;
editText=(EditText) findViewById(R.id.editText1);
editText.setText(apple+"\n"+banana);

It is working well for me. I hope, it will help you.

Upvotes: 0

Anderson K
Anderson K

Reputation: 5505

Try this:

private void activatebutton() {

    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //First option
            //By this
            tv.setText(apple + banana);
            tv.setMovementMethod(new ScrollingMovementMethod());
        }
    });
}

Or this, If you want to dynamically.

private void activatebutton() {

    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //Second option
            //Or dynamically

            String oldText = tv.getText().toString();

            if(oldText.length > 0){
                tv.setText(oldText + "\n" + "your string here");
            } else {
                tv.setText("your string here");
            }   

            tv.setMovementMethod(new ScrollingMovementMethod());            
        }
    });
}

Upvotes: 0

grattmandu03
grattmandu03

Reputation: 1286

Just replace:

tv.setText(apple); tv.setMovementMethod(new ScrollingMovementMethod()); tv.setText(banana, null); tv.setMovementMethod(new ScrollingMovementMethod());

by:

tv.setText(apple + banana); tv.setMovementMethod(new ScrollingMovementMethod());

Upvotes: 1

Related Questions