7. Pass parameters from one Activity to Another Activity in Android
We can go from one activity to another activity and also pass parameters from one to other activity. Let's create another activity named AnotherActivity and pass name as parameter from MainActivity to AnotherActivity and display it .
7. Pass parameters from one Activity to Another Activity in Android
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:id="@+id/mainActivityLinearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="tutorial.android.sachin4droid.androidtutorials.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"
android:textColor="@color/colorPrimaryDark"
android:textSize="30dp"
android:textStyle="bold"
android:id="@+id/textView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me !!!" />
</LinearLayout>
activity_another.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="tutorial.android.sachin4droid.androidtutorials.AnotherActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="103dp"
android:layout_marginTop="100dp"
android:text="in Another Activity" />
<TextView
android:id="@+id/paramtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/textView2"
android:layout_centerVertical="true"
android:layout_marginEnd="25dp"
android:text="TextView" />
</RelativeLayout>
MainActivity
package tutorial.android.sachin4droid.androidtutorials;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static String TAG = "MainActivity";
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"onCreate called now!!!");
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
intent.putExtra("myname","sachin123");
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG,"onStart called now!!!");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG,"onResume called now!!!");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG,"onPause called now!!!");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG,"onStop called now!!!");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG,"onDestroy called now!!!");
}
}
AnotherActivity
package tutorial.android.sachin4droid.androidtutorials;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class AnotherActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
textView = (TextView) findViewById(R.id.paramtextView);
//to get the param value
String paramValue = getIntent().getExtras().getString("myname");
textView.setText("Welcome "+paramValue+"!!!");
}
}
When We click on button, we will see another activity:
Happy Learning!!!
No comments:
Post a Comment