String.format()

Simple example using String.format().


package com.example.androidstringformat;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

EditText edit1, edit2;
TextView textOut;
Button buttonPrint;

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

edit1 = (EditText)findViewById(R.id.edit1);
edit2 = (EditText)findViewById(R.id.edit2);
textOut = (TextView)findViewById(R.id.printout);
buttonPrint = (Button)findViewById(R.id.buttonprint);

buttonPrint.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
String str1 = edit1.getText().toString();
String str2 = edit2.getText().toString();
String strOut = String.format("%s\n%S", str1, str2);

textOut.setText(strOut);
}});
}

}


<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidstringformat.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonprint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" - Print - " />
<TextView
android:id="@+id/printout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>