Android Basic Class Code


Simple Android Application




first.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Number"
android:layout_marginTop="10dp"
android:id="@+id/firstnumber"
android:layout_marginRight="20dp"
android:inputType="number"
android:layout_marginLeft="20dp"
android:padding="20dp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:id="@+id/secondnumber"
android:inputType="number"
android:hint="Enter Second Number"
android:padding="20dp"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Me"
android:id="@+id/addbutton"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:background="@color/darkyellow"
android:textColor="@color/white"
android:padding="20dp"
android:textAllCaps="false"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract Me"
android:id="@+id/subbutton"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:background="@color/darkyellow"
android:textColor="@color/white"
android:padding="20dp"
android:textAllCaps="false"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply Me"
android:id="@+id/mulbutton"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:background="@color/darkyellow"
android:textColor="@color/white"
android:padding="20dp"
android:textAllCaps="false"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy Result"
android:padding="20dp"
android:id="@+id/resultdata"
android:textSize="30sp"
android:layout_marginTop="30dp"
android:layout_gravity="center"/>

</LinearLayout>

MainActivity.java

package com.example.piyushapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private EditText t1, t2;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
t1 = findViewById(R.id.firstnumber);
t2 = findViewById(R.id.secondnumber);
Button b1 = findViewById(R.id.addbutton);
Button b2 = findViewById(R.id.subbutton);
Button b3 = findViewById(R.id.mulbutton);

final TextView tv = findViewById(R.id.resultdata);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//u can write logic here
//get the values from edittext
String a = t1.getText().toString();
String b = t2.getText().toString();
//convert string into integer
int i = Integer.parseInt(a);
int j = Integer.parseInt(b);
int result = i + j;
//Log.e("result",String.valueOf(result));
//Toast.makeText(MainActivity.this,
                String.valueOf(result),Toast.LENGTH_LONG).show();

/* AlertDialog.Builder ab = new
                AlertDialog.Builder(MainActivity.this);
ab.setTitle("Result Here");
ab.setIcon(R.mipmap.ic_launcher_round);
ab.setMessage(String.valueOf(result));
ab.setCancelable(false);

ab.setPositiveButton("Yes", new
                DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ab.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ab.setNeutralButton("Cancel",
                 new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ab.show();
*/

tv.setText(String.valueOf(result));
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String a = t1.getText().toString();
String b = t2.getText().toString();
int i = Integer.parseInt(a);
int j = Integer.parseInt(b);
int result = i - j;
tv.setText(String.valueOf(result));
}
});

b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String a = t1.getText().toString();
String b = t2.getText().toString();
int i = Integer.parseInt(a);
int j = Integer.parseInt(b);
int result = i * j;
tv.setText(String.valueOf(result));
}
});

}


}

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#AC840B</color>

<color name="purple">#A1049C</color>
<color name="white">#fff</color>
<color name="darkyellow">#CDA815</color>
</resources>


No comments: