Here is a sample Java code for a Love Calculator app in Android Studio. The app takes the names of two individuals as input and calculates a love percentage based on a random value between 1 and 100.
in activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name 1"
android:inputType="text"
android:maxLines="1" />
<EditText
android:id="@+id/name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name 2"
android:inputType="text"
android:maxLines="1" />
<Button
android:id="@+id/calculateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate" />
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
in MainActivity.java
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private EditText name1EditText;
private EditText name2EditText;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name1EditText = findViewById(R.id.name1);
name2EditText = findViewById(R.id.name2);
resultTextView = findViewById(R.id.resultText);
Button calculateButton = findViewById(R.id.calculateButton);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name1 = name1EditText.getText().toString().trim();
String name2 = name2EditText.getText().toString().trim();
if (TextUtils.isEmpty(name1) || TextUtils.isEmpty(name2)) {
Toast.makeText(MainActivity.this, "Please enter both names", Toast.LENGTH_SHORT).show();
return;
}
int lovePercentage = new Random().nextInt(100) + 1;
String result = String.format("%s and %s are %d%% compatible", name1, name2, lovePercentage);
resultTextView.setText(result);
}
});
}
}
Android Studio Tutorials |
0 Comments