android studio new line in 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);
tv1.setText("Line number 1 \nLine number 2 \nLine number 3");
TextView tv2 = (TextView) findViewById(R.id.text_view2);
tv2.setText("Line number 1");
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";
tv3.setText(str);
TextView tv4 = (TextView) findViewById(R.id.text_view4);
String str2 = "Line number 1 <br /> Line number 2";
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) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}