5. Create an Android App which receives name form the user and displays welcome name in Second Activity.

 

you tube link

Activity_main.xml:

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

<LinearLayout 

xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 

android:layout_width="match_parent" 

android:layout_height="match_parent" 

android:orientation="vertical"

 tools:context=".MainActivity">

<EditText 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:textSize="30sp" 

android:hint="Enter Name" 

android:id="@+id/edit"/>

<Button

android:layout_width="wrap_content" 

android:layout_height="wrap_content" 

android:textSize="30sp" 

android:text="CLICK" 

android:layout_gravity="center" 

android:onClick="Next"/>

</LinearLayout>


Layout Preview:





Activity_2.xml:

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

<LinearLayout 

xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 

android:layout_width="match_parent" 

android:layout_height="match_parent" 

tools:context=".Activity2">

<TextView 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:id="@+id/text" 

android:textSize="50sp"/>

 

</LinearLayout>

Layout Preview:



MainActivity.java:

package com.uday.android.a5a4_lc4; 

import android.content.Intent; 

import android.os.Bundle;

import android.view.View; 

import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity; 

public class MainActivity extends AppCompatActivity

 {

       EditText e;

     @Override

     protected void onCreate(Bundle savedInstanceState)

 {        super.onCreate(savedInstanceState); 

         setContentView(R.layout.activity_main); e=findViewById(R.id.edit);

   public void Next(View view)  

 { 

         String s =e.getText().toString();

        Intent i=new Intent(this, Activity2.class); 

        putExtra("key",s); 

        startActivity(i);

 }

}

 

Activity2.java:

package com.uday.android.a5a4_lc4; 

 

import android.os.Bundle;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class Activity2 extends AppCompatActivity 

    TextView tv;

    @Override

    protected void onCreate(Bundle savedInstanceState)

    { 

            super.onCreate(savedInstanceState); 

            setContentView(R.layout.activity_2);

            tv = findViewById(R.id.text);

            String s= getIntent().getStringExtra("key"); 

            tv.setText("Welcome "+s);

     }

}

OUTPUT:







Post a Comment

0 Comments