Android Program : Create an Android Application to find the factorial of a number and Display the Result on Alert Box.
Soln :
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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_apti_number"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="20dp"
android:padding="10dp"
/>
<Button
android:id="@+id/b_apti_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="20dp"
android:padding="10dp"/>
<TextView
android:id="@+id/tv_apti_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="20dp"
android:padding="10dp"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.slipap3a;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText number;
TextView answer;
Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
number = (EditText) findViewById(R.id.et_apti_number);
number.setHint("Enter number to be factorialized :P");
answer = (TextView) findViewById(R.id.tv_apti_answer);
calculate = (Button) findViewById(R.id.b_apti_calc);
calculate.setOnClickListener(this);
}
private long calcFactorial() {
long factorial = 1;
try {
factorial = Long.parseLong(number.getText().toString());
for (int i = (int) (factorial - 1); i > 0; i--) {
factorial = i * factorial;
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
}
} catch (NumberFormatException e) {
Toast.makeText(this, "Incorrect Input", Toast.LENGTH_LONG).show();
} finally {
}
return factorial;
}
@Override
public void onClick(View v) {
answer.setText("Factorial of " + number.getText().toString() + " is : " + calcFactorial());
// Create the object of
// AlertDialog Builder class
AlertDialog.Builder builder
= new AlertDialog
.Builder(MainActivity.this);
// Set the message show for the Alert time
builder.setMessage("Do you want to exit ?Factorial of " + number.getText().toString() + " is : " + calcFactorial());
// Set Alert Title
builder.setTitle("Alert !");
// Set Cancelable false
// for when the user clicks on the outside
// the Dialog Box then it will remain show
builder.setCancelable(false);
// Set the positive button with yes name
// OnClickListener method is use of
// DialogInterface interface.
builder
.setPositiveButton(
"Yes",
new DialogInterface
.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which)
{
// When the user click yes button
// then app will close
finish();
}
});
// Set the Negative button with No name
// OnClickListener method is use
// of DialogInterface interface.
builder
.setNegativeButton(
"No",
new DialogInterface
.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which)
{
// If user click no
// then dialog box is canceled.
dialog.cancel();
}
});
// Create the Alert dialog
AlertDialog alertDialog = builder.create();
// Show the Alert Dialog box
alertDialog.show();
}
}
0 Comments