Exercis 1
Exercis 1
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/myapptxt"
android:id="@+id/myappid"
android:layout_alignParentTop="true"
android:textSize="20pt"
android:textColor="#110014" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hinttxt"
android:id="@+id/num1id"
android:layout_below="@+id/myappid"
android:textSize="20pt"
android:inputType="number"
android:textColor="#1100ff"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hinttxt"
android:id="@+id/num2id"
android:layout_below="@+id/num1id"
android:textSize="20pt"
android:inputType="number"
android:textColor="#1100ff"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/caltxt"
android:id="@+id/calid"
android:layout_below="@+id/num2id"
android:textSize="20pt"
android:inputType="number"
android:textColor="#110014"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/resultbelowtxt"
android:id="@+id/resulthereid"
android:textStyle="italic"
android:textAlignment="center"
android:layout_below="@+id/calid"
android:textSize="20pt"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/resulttxt"
android:id="@+id/resultid"
android:textStyle="bold"
android:textAlignment="center"
android:layout_below="@+id/resulthereid"
android:textSize="15pt"
android:textColor="#ff0000"
/>
</RelativeLayout>
String.xml
<resources>
<string name="app_name">Exercise1</string>
<string name="myapptxt">My First Application</string>
<string name="caltxt">Calculate</string>
<string name="hinttxt">Enter Number Here</string>
<string name="resultbelowtxt">Results Below</string>
<string name="resulttxt"></string>
</resources>
MainActivity.java
cal = findViewById(R.id.calid)
cal.setOnClickListener {
num1 = findViewById(R.id.num1id)
num2 = findViewById(R.id.num2id)
result = findViewById(R.id.resultid)
if (num1.text.toString().isEmpty() ||
num2.text.toString().isEmpty()) {
Toast.makeText(applicationContext, "Please fill the
required fields", Toast.LENGTH_LONG).show()
} else {
val sum = num1.text.toString().toFloat() +
num2.text.toString().toFloat()
result.text = "${num1.text} + ${num2.text} = $sum"
}
}
}
}