Create an application that will get the Text Entered in Edit Text and display that Text using toast (Message).

Activity_Main.xml:
<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"
    tools:context=".MainActivity">

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="83dp"
        android:text="@string/t1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="48dp"
        android:text="@string/t2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:inputType="text"/>

<EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:ems="10"
        android:inputType="text"/>

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp"
        android:text="@string/b1" />

</RelativeLayout>

MainActivity.java

package com.example.prac13;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

publicclass MainActivity extends Activity implements OnClickListener {
            protectedvoid onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        Button b=(Button) findViewById(R.id.button1);
                        b.setOnClickListener(this);
            }
            publicboolean onCreateOptionsMenu(Menu menu) {
                        getMenuInflater().inflate(R.menu.activity_main, menu);
                        returntrue;
            }
            publicvoid onClick(View arg0) {
                        EditText tx1=(EditText) findViewById(R.id.editText1);
                        EditText tx2=(EditText) findViewById(R.id.editText2);
                        String s1=tx1.getText().toString();
                        String s2=tx2.getText().toString();
                        Toast.makeText(this,s1+" "+s2, Toast.LENGTH_LONG).show();
            }

}

Post a Comment

0 Comments