-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondActivity.java
More file actions
67 lines (56 loc) · 2.23 KB
/
Copy pathSecondActivity.java
File metadata and controls
67 lines (56 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.example.luckynumber;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.util.Random;
public class SecondActivity extends AppCompatActivity {
TextView welcomeTxt, luckyNumberTxt;
Button sharedBtn;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_second);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
welcomeTxt = findViewById(R.id.textview2);
luckyNumberTxt = findViewById(R.id.luckynumber);
sharedBtn = findViewById(R.id.sharedbtn);
Intent i = getIntent();
String userName = i.getStringExtra("name");
// Generating random number
int randomNumber = generateRandomNumber();
luckyNumberTxt.setText(""+randomNumber);
sharedBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareData(userName, randomNumber);
}
});
}
private void shareData(String userName, int randomNumber) {
// Implicit intent
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, userName + "got lucky number today!");
i.putExtra(Intent.EXTRA_TEXT, "Your lucky number is: "+randomNumber);
startActivity(Intent.createChooser(i, "Choose a Platform"));
}
public int generateRandomNumber() {
Random random = new Random();
int upper_limit = 1000;
return random.nextInt(upper_limit);
}
}