Addition of Three Strings Using Single Activity In Android

How to Perform  Addition of three strings In Android

When we add two or more strings, we can get output in one of two ways, first the result shows in the current screen, and the second is to show the result in another screen.

Here I am going to tell you how to add three strings on the current screen.

Step 1

Create a new project "AddStrings" as shown in the following diagram and create the Java file as "MainActivity.java".

Image1

 newprjct.jpg

After selection of "Android Application Project" as shown above we have to provide the Project name, Package name and Application name as shown below.

myprjct.jpg

Step 2

Open the XML file as "activity_main.xml" and update the code as shown below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context=".MainActivity" >

    <EditText

        android:id="@+id/id1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:ems="10"

        android:inputType="textEmailAddress" >

        <requestFocus />

    </EditText>

    <EditText

        android:id="@+id/id2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:ems="10"

        android:inputType="textPersonName" />

    <EditText

        android:id="@+id/id3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:ems="10"

        android:inputType="textPersonName" />

    <Button

        android:id="@+id/btid"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/button" />

      <TextView

        android:id="@+id/textView1"

        android:layout_width="match_parent"

        android:layout_height="82dp"

        android:text="@string/tv" />

</LinearLayout>

Step 3

Go to res/values and open the "string.xml" file and update it according to your UI requirements.

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">MyProject</string>

    <string name="button" >Add All</string>

    <string name="menu_settings">Settings</string>

    <string name="tv"></string>

</resources>

 Step 4

Open the Java file as "MainActivity.java" and update the code as shown below.

package com.project.myproject;

import com.project.myproject.R;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.EditText;

public class AddString extends Activity {

EditText txtbox1;

EditText txtbox2;

EditText txtbox3;

TextView tv;

Button btn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtbox1=(EditText) findViewById(R.id.id1);

txtbox2=(EditText) findViewById(R.id.id2);

txtbox3=(EditText) findViewById(R.id.id3);

tv=(TextView) findViewById(R.id.textView1);

btn=(Button) findViewById(R.id.btid);

 

btn.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

String a,b,c;

a=txtbox1.getText().toString();

b=txtbox2.getText().toString();

c=txtbox3.getText().toString();

tv.setText(a+b+c);

}

});

}

}

Step 5

The Manifest file also must be updated if we require any update in the manifest.

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.project.myproject"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.project.myproject.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>                          

Input:

 outpt1.jpg

Output:

 outpt2.jpg


Similar Articles