I010 Practical1 MAD
I010 Practical1 MAD
Part A
AIM: Android Layouts (Linear & Relative Layout) and Signal Handler.
Scenario: Create a Calculator using relative layout and Linear Layout for the buttons, which would accept
two numbers and onClick of any of the four Buttons (+,-,*,/) would display the answer in a TextField. The
onClick Listener has to be registered to the ActivityMain class.
SVKM’s NMIMS University
(Students must submit the soft copy as per the following segments. The soft copy must be uploaded on
the Blackboard. The filename should be Batch_RollNo_Exp_No)
1. Program Scenario and Program code: (Write Scenario and Paste your program code (Java,
xml resource and layout)).
activity_main.xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="@string/my_name"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@color/mycolor" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Number"
android:textSize="20sp" />
<EditText
android:id="@+id/txt_num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<TextView
android:layout_width="wrap_content"
SVKM’s NMIMS University
<EditText
android:id="@+id/txt_num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="+" />
<Button
android:id="@+id/btn_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="-" />
<Button
android:id="@+id/btn_mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="*" />
<Button
android:id="@+id/btn_div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="/" />
</LinearLayout>
<TextView
android:id="@+id/txt_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
SVKM’s NMIMS University
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
</LinearLayout>
SVKM’s NMIMS University
MainActivity.java
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
EditText text_num1;
EditText text_num2;
Button button_add, button_sub, button_mul, button_div;
TextView text_output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_num1 = findViewById(R.id.txt_num1);
text_num2 = findViewById(R.id.txt_num2);
button_add = findViewById(R.id.btn_add);
button_sub = findViewById(R.id.btn_sub);
button_mul = findViewById(R.id.btn_mul);
button_div = findViewById(R.id.btn_div);
text_output = findViewById(R.id.txt_out);
button_add.setOnClickListener(this);
button_sub.setOnClickListener(this);
button_mul.setOnClickListener(this);
button_div.setOnClickListener(this);
@Override
public void onClick(View view) {
String num1 = text_num1.getText().toString();
String num2 = text_num2.getText().toString();
if(num1.isEmpty() || num2.isEmpty()){
Toast.makeText(MainActivity.this,"One of the number is blank",
Toast.LENGTH_SHORT).show();
}
SVKM’s NMIMS University
colors.xml
strings.xml
<resources>
<string name="app_name">Calculator</string>
<string name="my_name">My Basic Calculator</string>
</resources>
SVKM’s NMIMS University
Addition
SVKM’s NMIMS University
Subtraction
SVKM’s NMIMS University
Multiplication
SVKM’s NMIMS University
Division
SVKM’s NMIMS University
Divide by 0
SVKM’s NMIMS University
3. Observations: A brief description of the design aspects and working of the code in your own
words.
In this experiment, we constructed a basic calculator in which the user must enter two values
and choose between addition, subtraction, multiplication, or division as an operation. The
result is computed using the Java programming language. A notice indicating "One input is
blank" would be displayed if one of the two inputs was missing.
Linear layout tags, text view tags, edit text tags, and button tags have all been utilised.
4. Questions:
a. Explain any five android concepts you learned after performing this practical.
b. Draw & Explain with respect to layouts the Scene Graph of the experiment.
SVKM’s NMIMS University
5. Conclusion (Learning Outcomes): How were the outcomes defined for the experiment in
Part A fulfilled through the scenarios?
Ans. The linear layout tag was used to align all of the children in the same direction, the text
view tag was used to add text, the edit text tag was used to enter text/data from the user, and
the button tag was used to inject buttons into the app to do the desired action. The java
programming language is also utilised to compute the findings.