Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Mad Lab 7 - 24

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

IU2141050184 MAD-SEM-6

Practical-7
AIM: Implementation of Explicit Intent in Android

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<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" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<activity
android:name=".Answer"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>

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:

package com.example.explicitintent; import


androidx.appcompat.app.AppCompatActivity; import
android.content.Intent; import android.os.Bundle;
import android.view.View; import
android.widget.Button; import
android.widget.EditText;

public class MainActivity extends AppCompatActivity {


Button add,sub,mul,div;
EditText n1,n2; @Override protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
INDUS UNIVERSITY 4
IU2141050184 MAD-SEM-6
setContentView(R.layout.activity_main); add =
findViewById(R.id.add); sub =
findViewById(R.id.sub); mul =
findViewById(R.id.mul); div =
findViewById(R.id.div); n1 =
findViewById(R.id.n1); n2 =
findViewById(R.id.n2);

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:

package com.example.explicitintent; import


android.content.Intent; import android.os.Bundle;
import android.widget.TextView; import
androidx.appcompat.app.AppCompatActivity; public
class Answer extends AppCompatActivity {
TextView ans;
@Override
protected void onCreate(Bundle temp){
super.onCreate(temp); setContentView(R.layout.answer);
ans = findViewById(R.id.ans); Intent answer = getIntent();
ans.setText(answer.getStringExtra("answer"));
}
}

Output:

INDUS UNIVERSITY 7
IU2141050184 MAD-SEM-6

INDUS UNIVERSITY 8

You might also like