android add new line to string code example

Example 1: android studio string new line

String str = "my string \n my other string";

Example 2: string display new line with n in android app

package com.cfsuman.me.myapplication5;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.text.Html;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

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

    public void perform_action(View v)
    {
        TextView tv1 = (TextView) findViewById(R.id.text_view1);
        //define multiline by \n line separator
        tv1.setText("Line number 1 \nLine number 2 \nLine number 3");

        TextView tv2 = (TextView) findViewById(R.id.text_view2);
        tv2.setText("Line number 1");
        //define new line by append android system line separator
        tv2.append(System.getProperty("line.separator"));
        tv2.append("Line number 2");

        TextView tv3 = (TextView) findViewById(R.id.text_view3);
        String str = "Line number 1"
                + System.getProperty("line.separator")
                + "Line number 2";
        //define new line by android system line separator
        tv3.setText(str);

        TextView tv4 = (TextView) findViewById(R.id.text_view4);
        //define new line by html <br />tag
        String str2 = "Line number 1 <br /> Line number 2";
        //need to import android.text.Html class
        tv4.setText(Html.fromHtml(str2));

        TextView tv5 = (TextView) findViewById(R.id.text_view5);
        tv5.setText(R.string.Multiline_Text_By_N);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Tags:

Java Example