Create an application for demonstration of Explicitly Starting New Activity using Intent

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="111dp"
        android:text="@string/b1" />

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

</RelativeLayout>

Activity_Main2.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=".MainActivity2" >

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="71dp"
        android:text="@string/b2" />

</RelativeLayout>

MainActivity.java

package com.example.prac10;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
       Button  b1 = (Button )findViewById(R.id.button1);
       b1.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
 
        return true;
    }

            @Override
            public void onClick(View arg0) {
                        Intent i = new Intent(getApplicationContext(),MainActivity2.class);
                        i.putExtra("value1","Cick Here To Call 2nd Activity");
                        i.putExtra("value2","Chaitu");
                        startActivity(i);
                       
            }
   
}

MainActivity2.java

package com.example.prac10;

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

public class MainActivity2 extends Activity implements OnClickListener{

            @Override
            protected void onCreate(Bundle bundel) {
                        super.onCreate(bundel);
                        setContentView(R.layout.activity_main_activity2);              
                        Bundle extra = getIntent().getExtras();
                        String val1 =extra.getString("value1");
                        String val2 =extra.getString("value2");
                       
                       
            Toast.makeText(getApplicationContext(), val1+" "+val2,Toast.LENGTH_LONG).show();
                        Button b2 = (Button)findViewById(R.id.button1);
                        b2.setOnClickListener(this);
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                        getMenuInflater().inflate(R.menu.activity_main_activity2, menu);
                        return true;
            }

            @Override
            public void onClick(View arg0) {
                        Intent i = new Intent(getApplicationContext(),MainActivity.class);
                        startActivity(i);
            }


}

Post a Comment

0 Comments