Creating a simple android application
MainActivity.java
package com.example.whoami;
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;
/* Interface is used for Reusability */
// Overloading - same method different paramters within the class
// Overriding - same method different class used in inheritance
// Activity Life Cycle
// on , Pause , oncreate, ondestroy
public class MainActivity extends AppCompatActivity {
TextView result;
TextView result2;
Button btn;
EditText num1field;
EditText num2field;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1field = (EditText)findViewById(R.id.num1);
num2field = (EditText)findViewById(R.id.num2);
btn = (Button)findViewById(R.id.btn);
result = (TextView)findViewById(R.id.result);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// On click Handling
int n1 = Integer.parseInt(num1field.getText().toString());
int n2 = Integer.parseInt(num2field.getText().toString());
String greater;
if (n1>n2){
greater = n1 +" is greater than "+ n2;
}
else{
greater = n2 +" is greater than "+ n1;
}
result.setText(greater);
}
});
}
}
Activity main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter first number"
android:inputType="numberSigned" />
<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter second number"
android:inputType="numberSigned" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/result2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Comments
Post a Comment