Lucky Number App
Output:
Main XML:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Lucky Number"
android:textColor="@color/white"
android:textSize="32sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.099" />
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="235dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="295dp"
android:hint="Please enter your name"
android:textColor="@color/white"
android:textColorHint="@color/white"
app:layout_constraintBottom_toTopOf="@+id/imageview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:layout_marginEnd="128dp"
android:text="Wish me a Luck!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_text" />
<ImageView
android:id="@+id/imageview"
android:layout_width="200dp"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
android:src="@drawable/dice"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_text" />
Second Activity XML:
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Your Lucky Number is:"
android:textColor="@color/white"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/lucky_number_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:text="888"
android:textColor="@color/white"
android:textSize="64sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/share_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:text="Share my Lucky Number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Main Java:
public class MainActivity extends AppCompatActivity {
EditText editText;
Button btn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.textView);
editText = findViewById(R.id.edit_text);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName = editText.getText().toString();
// Explicit Intent
Intent i = new Intent(
getApplicationContext(),
SecondActivity.class
);
// Passing the name to second activity
i.putExtra("name",userName);
startActivity(i);
}
});
}
}
Second Java:
public class SecondActivity extends AppCompatActivity {
TextView welcomeTxt, luckyNumberTxt;
Button share_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
welcomeTxt = findViewById(R.id.textView2);
luckyNumberTxt = findViewById(R.id.lucky_number_txt);
share_btn = findViewById(R.id.share_btn);
// Receiving the data from Main Activity
Intent i = getIntent();
String userName = i.getStringExtra("name");
// Generating Random Numbers
int random_num= generateRandomNumber();
luckyNumberTxt.setText(""+random_num);
share_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareData(userName, random_num);
}
});
}
public int generateRandomNumber(){
Random random = new Random();
int upper_limit = 1000;
int randomNumberGenerated = random.nextInt(upper_limit);
return randomNumberGenerated;
}
public void shareData(String username, int randomNum){
// Implicit Intent
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
// Additional Info
i.putExtra(Intent.EXTRA_SUBJECT,username + " got lucky today!");
i.putExtra(Intent.EXTRA_TEXT, "His lucky number is: "+randomNum);
startActivity(Intent.createChooser(i,"Choose a Platform"));
}
}
Comments
Post a Comment