Mad Lab 7 - 24
Mad Lab 7 - 24
Mad Lab 7 - 24
Practical-7
AIM: Implementation of Explicit Intent in Android
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ExplicitIntent"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
INDUS UNIVERSITY 1
IU2141050184 MAD-SEM-6
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<EditText android:id="@+id/n1"
android:layout_width="800px"
android:layout_height="wrap_content"
android:layout_marginBottom="20px"
android:ems="10"
android:hint="Number 1"
android:inputType="text"
android:textAlignment="center"
tools:inputType="number" />
<EditText android:id="@+id/n2"
android:layout_width="800px"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Number 2"
android:inputType="text"
INDUS UNIVERSITY 2
IU2141050184 MAD-SEM-6
android:textAlignment="center"
tools:inputType="number" />
<LinearLayout
android:layout_width="900px"
android:layout_height="wrap_content"
android:layout_marginTop="70px"
android:layout_marginBottom="20px"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_width="400px"
android:layout_height="150px"
android:layout_weight="1"
android:text="Addition" />
<Button
android:id="@+id/sub"
android:layout_width="400px"
android:layout_height="150px"
android:layout_weight="1"
android:text="Subtraction" />
</LinearLayout>
<LinearLayout
android:layout_width="900px"
android:layout_height="wrap_content"
android:orientation="horizontal">
INDUS UNIVERSITY 3
IU2141050184 MAD-SEM-6
<Button
android:id="@+id/mul"
android:layout_width="400px"
android:layout_height="150px"
android:layout_weight="1"
android:text="Multiplication" />
<Button
android:id="@+id/div"
android:layout_width="400px"
android:layout_height="150px"
android:layout_weight="1"
android:text="Division" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
add.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int num1 = Integer.parseInt(n1.getText().toString());
int num2 = Integer.parseInt(n2.getText().toString());
Intent message = new Intent(getApplicationContext(), Answer.class);
message.putExtra("answer",num1+" + "+num2+" =
"+(num1+num2));
startActivity(message);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int num1 = Integer.parseInt(n1.getText().toString());
int num2 = Integer.parseInt(n2.getText().toString());
Intent message = new Intent(getApplicationContext(),
Answer.class); message.putExtra("answer",num1+"
- "+num2+" = "+(num1-num2));
startActivity(message);
}
});
mul.setOnClickListener(new View.OnClickListener() {
INDUS UNIVERSITY 5
IU2141050184 MAD-SEM-6
@Override public void onClick(View v) {
int num1 = Integer.parseInt(n1.getText().toString());
int num2 = Integer.parseInt(n2.getText().toString());
Intent message = new Intent(getApplicationContext(), Answer.class);
message.putExtra("answer",num1+" * "+num2+" = "+(num1*num2));
startActivity(message);
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override public void
onClick(View v) {
int num1 = Integer.parseInt(n1.getText().toString());
int num2 = Integer.parseInt(n2.getText().toString());
Intent message = new Intent(getApplicationContext(), Answer.class);
message.putExtra("answer",num1+" / "+num2+" =
"+(float)((float)num1/(float)num2));
startActivity(message);
}
});
}
}
answer.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
INDUS UNIVERSITY 6
IU2141050184 MAD-SEM-6
<TextView android:id="@+id/ans"
android:layout_width="wrap_content"
android:layout_height="150px"
android:hint="Answer" android:text="TextView"
android:textAlignment="center"
android:textSize="70px"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Answer.java:
Output:
INDUS UNIVERSITY 7
IU2141050184 MAD-SEM-6
INDUS UNIVERSITY 8