Monday, August 10, 2015

How To Switch Activity and Send Data


.
How To Switch Activity and Send Data
Using Android Studio 1.4

1) Create New Blank Activitiy

Follow the wizard to create New Blank Activity.

2) Edit res/layout/content_main.xml

Add Linear Layout containing Form elements.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"
   android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:showIn="@layout/activity_main" tools:context=".MainActivity">
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       >
       <TextView android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="Name: "/>
       <EditText android:id="@+id/name"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_marginBottom="10dip"/>
       <TextView
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="Email: "
           />
       <EditText android:id="@+id/email"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_marginBottom="10dip"/>
       <Button android:id="@+id/btnNextScreen"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="Send to Next Screen"
           android:layout_marginTop="15dip"/>
   </LinearLayout>
</RelativeLayout>
OUTCOME.
(content_main.xml in DESIGN mode)

3) Add Second Activity

Add New/Activity/BlankActivity
Accept “Activity Name=Main2Activity” and “Layout Name=activity_main2”
Edit res/layout/content_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"
   android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:showIn="@layout/activity_main2"
   tools:context="com.notarazi.switchactivity1.Main2Activity">
   <LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="You Entered..."
           android:textSize="25dip"
           android:gravity="center"
           android:layout_margin="15dip"/>
       <TextView android:id="@+id/txtName"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_margin="15dip"
           android:textSize="18dip"/>
       <TextView android:id="@+id/txtEmail"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_margin="15dip"
           android:textSize="18dip"/>
       <Button android:id="@+id/btnClose"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_marginTop="15dip"
           android:text="Close"/>
   </LinearLayout>
</RelativeLayout>
OUTCOME.
(content_main2.xml in DESIGN mode).

4) Edit MainActivity.java

package com.notarazi.switchactivity1;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
       final EditText inputName = (EditText) findViewById(R.id.name);
       final EditText inputEmail = (EditText) findViewById(R.id.email);
       Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
       //Listening to button event
       btnNextScreen.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
               //Starting a new Intent
               Intent nextScreen = new Intent(getApplicationContext(), Main2Activity.class);
               //Sending data to another Activity
               nextScreen.putExtra("name"inputName.getText().toString());
               nextScreen.putExtra("email"inputEmail.getText().toString());
               Log.e("n"inputName.getText() + "." inputEmail.getText());
               startActivity(nextScreen);
           }
       });
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action"null).show();
           }
       });
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
   }
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();
       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }
}

5) Edit Main2Activity.java

package com.notarazi.switchactivity1;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main2);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
       TextView txtName = (TextView) findViewById(R.id.txtName);
       TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
       Button btnClose = (Button) findViewById(R.id.btnClose);
       Intent i = getIntent();
       // Receiving the Data
       String name = i.getStringExtra("name");
       String email = i.getStringExtra("email");
       Log.e("Second Screen", name + "." + email);
       // Displaying Received data
       txtName.setText(name);
       txtEmail.setText(email);
       // Binding Click event to Button
       btnClose.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
               //Closing SecondScreen Activity
               finish();
           }
       });
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action"null).show();
           }
       });
   }
}

DOWNLOAD

REFERENCES



.

No comments:

Post a Comment