A) Create a Simple Application Which Send ―Hello‖ message from one activity to another with help of Button (Use Intent).
Soln :
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="utf-8"?> |
activity_main2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="utf-8"?> |
MainActivity.java
package com.example.slipap1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText textBox;
Button passButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = (EditText)findViewById(R.id.textBox);
passButton = (Button)findViewById(R.id.passButton);
passButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = textBox.getText().toString();
Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
intent.putExtra("message", str);
startActivity(intent);
}
});
}
}
MainActivity2.java
package com.example.slipap1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
text = (TextView)findViewById(R.id.text);
Intent intent = getIntent();
String str = intent.getStringExtra("message");
text.setText(str);
}
}
ScreenShot
Screen 1 |
a message is passed from first activity to second when a button is pressed.
Screen 2
0 Comments