Lompat ke konten Lompat ke sidebar Lompat ke footer

Mengirim Data Dari Button Activity Ke Activity Lain Dengan EXTRA Android Studio

Macam Macam Fungsi Button Dan Cara Berpindahnya

 

Mainactivity.java

package bsi.ac.id.appbelajarintent;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

//Macam Macam Pemanggilan Button Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button PindahActivity;
private Button PindahActivity_WithData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

PindahActivity = findViewById(R.id.pindah_activity);
PindahActivity.setOnClickListener(this);

PindahActivity_WithData = findViewById(R.id.pindah_activity_with_data);
PindahActivity_WithData.setOnClickListener(this);

// PindahActivity_WithData.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Intent MoveintentWithData = new Intent(MainActivity.this, MoveWithDataActivity.class);
// MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_NAME,"Bina Sarana Informatika");
// MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_AGE,32);
// startActivity(MoveintentWithData);
// }
// });
}

@Override
public void onClick(View view) {
switch (view.getId()){
//pindah seperti set onclick listener (biasa)
case R.id.pindah_activity:
Intent Moveintent = new Intent(MainActivity.this, MoveActivity.class);
startActivity(Moveintent);
break;
case R.id.pindah_activity_with_data:
Intent MoveintentWithData = new Intent(MainActivity.this, MoveWithDataActivity.class);
MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_NAME,"Bina Sarana Informatika");
MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_AGE,32);

MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_NAME1,"M Farhanul IR");
MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_NAME2,"Dwiki Ramadhan");
MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_NAME3,"Firman Habib");

MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_AGE1,10200095);
MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_AGE2,10200022);
MoveintentWithData.putExtra(MoveWithDataActivity.EXTRA_AGE3,10200071);

startActivity(MoveintentWithData);
break;
}
}
}

 MoveWithDataActivity.Java

package bsi.ac.id.appbelajarintent;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MoveWithDataActivity extends AppCompatActivity {
public static String EXTRA_AGE = "extra_age";
public static String EXTRA_NAME = "extra_name";

public static String EXTRA_NAME1 = "extra_name1";
public static String EXTRA_NAME2 = "extra_name2";
public static String EXTRA_NAME3 = "extra_name3";

public static String EXTRA_AGE1 = "extra_age1";
public static String EXTRA_AGE2 = "extra_age2";
public static String EXTRA_AGE3 = "extra_age3";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move_with_data2);

TextView tvDataReceived=(TextView)findViewById(R.id.tv_data_received);
TextView nama_anggota1=(TextView)findViewById(R.id.tv_data_received1);
TextView nama_anggota2=(TextView)findViewById(R.id.tv_data_received2);
TextView nama_anggota3=(TextView)findViewById(R.id.tv_data_received3);

String name = getIntent().getStringExtra(EXTRA_NAME);
int age = getIntent().getIntExtra(EXTRA_AGE, 0);
String text = "Name : "+name+", Your Age : "+age;

String nama1 = getIntent().getStringExtra(EXTRA_NAME1);
String nama2 = getIntent().getStringExtra(EXTRA_NAME2);
String nama3 = getIntent().getStringExtra(EXTRA_NAME3);

int nim1 = getIntent().getIntExtra(EXTRA_AGE1, 0);
int nim2 = getIntent().getIntExtra(EXTRA_AGE2, 0);
int nim3 = getIntent().getIntExtra(EXTRA_AGE3, 0);

String text1 = "Name : "+nama1+", NIM : "+nim1;
String text2 = "Name : "+nama2+", NIM : "+nim2;
String text3 = "Name : "+nama3+", NIM : "+nim3;

tvDataReceived.setText(text);
nama_anggota1.setText(text1);
nama_anggota2.setText(text2);
nama_anggota3.setText(text3);
}
}

 MoveDataActivity.java

package bsi.ac.id.appbelajarintent;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MoveActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move);
}
}

 

 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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/pindah_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pindah Activity"
tools:ignore="MissingConstraints" />

<Button
android:layout_width="match_parent"
android:text="Pindah Aktivitiy Dengan Data"
android:id="@+id/pindah_activity_with_data"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />

</LinearLayout>

 activity_move.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MoveActivity">

<TextView
android:layout_width="match_parent"
android:text="Pindah Activity"
android:textAlignment="center"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

 activity_move_with_data2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MoveWithDataActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:id="@+id/tv_data_received"
android:text="haha"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />

<TextView
android:layout_width="wrap_content"
android:id="@+id/tv_data_received1"
android:text="haha"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />

<TextView
android:layout_width="wrap_content"
android:id="@+id/tv_data_received2"
android:text="haha"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />

<TextView
android:layout_width="wrap_content"
android:id="@+id/tv_data_received3"
android:text="haha"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

close