0% found this document useful (0 votes)
6 views3 pages

Exercis 1

The document contains an Android application layout defined in XML, which includes two EditText fields for number input, a Button to calculate the sum, and TextViews to display results. It also includes a strings resource file for defining text used in the app and a MainActivity class in Kotlin that handles user input and performs the calculation. The app prompts users to enter two numbers and displays their sum when the button is clicked, with error handling for empty fields.

Uploaded by

Yehu Ye enatu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Exercis 1

The document contains an Android application layout defined in XML, which includes two EditText fields for number input, a Button to calculate the sum, and TextViews to display results. It also includes a strings resource file for defining text used in the app and a MainActivity class in Kotlin that handles user input and performs the calculation. The app prompts users to enter two numbers and displays their sum when the button is clicked, with error handling for empty fields.

Uploaded by

Yehu Ye enatu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercis1

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

class MainActivity : AppCompatActivity() {

private lateinit var result: TextView


private lateinit var num1: EditText
private lateinit var num2: EditText
private lateinit var cal: Button

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

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

You might also like