Wednesday 21 January 2015

Intent Example

MainActivity

In MainActivity we pass the intent to MainActivity1


package com.intent_navigation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private EditText name;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        name =(EditText)findViewById(R.id.editText1);
         btn=(Button)findViewById(R.id.button1);
       
        abc();
       
       

    }
    public void abc()
    {
       
        btn.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
               
               
                String myname=name.getText().toString();
   
               
                Intent obj=new Intent(MainActivity.this,MainActivity1.class);
                obj.putExtra("i", myname);
                startActivity(obj);
               
               
            }
        });
       
    }
 }

Xml File for MainActivity:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.intent_navigation.MainActivity"
    tools:ignore="MergeRootFrame" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
       
</EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:layout_below="@+id/editText1"
        android:text="Next activity"
        android:onClick="abc" />

</RelativeLayout>

MainActivity1

package com.intent_navigation;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity1 extends ActionBarActivity {
    private TextView t1;
    String t2;
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity1);
        t1=(TextView)findViewById(R.id.textView1);
       
         get();
}
    private void get()
    {
   
        String obj=getIntent().getStringExtra("i");
       

        t1.setText(""+obj);
       
    }
}



Xml File for MainActivity1:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.intent_navigation.MainActivity1"
    tools:ignore="MergeRootFrame" >

    <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_marginLeft="74dp"
        android:layout_marginTop="44dp"
        />

</RelativeLayout>





                                                                MainActivity

                                                                   MainActivity1








No comments:

Post a Comment