Friday, 28 July 2017

Use Intent to pass value from one to other Activity in Android

An intent is used as message carrier between two activities in Android.It is an object that provides runtime binding between separate component.There are one parent activity and other activity that is invoked by parent activity act as child Activity.







Step 1: - Open your project in Android Studio.In your activity.xml create the layout of your requirement.Here I am giving an example. 

activity.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="com.example.hp.simpleuserinterface.MainActivity">

    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/enter_message" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click"
        android:onClick="sendMessage"/>


</LinearLayout>


String.xml


<resources>
    <string name="app_name">SimpleUserInterface</string>
    <string name="enter_message">Enter a message</string>
    <string name="click">Send</string>
</resources>



Step 2: - Create a new blank activity in Android.Go to app→java→open first content in Mainactivity right click→new→Activity→Empty activity.Give the name to your new activity.I am giving it name display_message.when we click finish new xml and java class creates.

Step 3: - In Androidmanifest.xml we type some statement to put navigation bar in the new activity that we have created display_message.

<activity android:name=".DisplayMessageActivity"    
android:parentActivityName=".MainActivity">
    <meta-data       
 android:name="android_support.PARENT_ACTIVITY"       
 android:value=".MainActivity"/>
</activity>

Put this code in manifest.xml.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.simpleuserinterface">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DisplayMessageActivity"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android_support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>

</manifest>



Design activity_display_message.xml according to your requirement.I am designing this according to my example.

<?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="com.example.hp.simpleuserinterface.DisplayMessageActivity">

     <TextView
         android:id="@+id/display_text"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textSize="25sp"
         android:textColor="@android:color/holo_blue_dark"/>
</LinearLayout>


Step 4: - Open your MainActivity.java and paste the below code.

package com.example.hp.simpleuserinterface;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ViewPropertyAnimatorCompatSet;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="org.mz.activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendMessage(View view){
        Intent intent=new Intent(this,DisplayMessageActivity.class);
        EditText editText=(EditText)findViewById(R.id.text);
        String message=editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE,message);
        startActivity(intent);
    }
}


Step 5: - Open your DisplayMessageActivity.java and paste the below code.


package com.example.hp.simpleuserinterface;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        Intent intent=getIntent();
        String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView=(TextView)findViewById(R.id.display_text);
        textView.setText("Welcome " + message);
    }
}




1 comment:

  1. Power Supply Titanium Art | TITanium-Arts.com
    Home · Hobbies · Parts & Accessories titanium vs stainless steel apple watch · Gifts & titanium damascus knives Personal Care · Toys & black titanium fallout 76 Games · ford titanium Electronics · Gaming Accessories · titanium quartz Video Games

    ReplyDelete

Change Fragment on button click in Android

In example mention below on click of a button, we attach the desired fragment in our frame layout which was present in activity_main.xml. ...