0% found this document useful (0 votes)
5 views8 pages

Mad PRG

The document provides code for three Android applications: a simple calculator, a radio button selection interface, and a student marksheet using a table layout. Each application includes an XML layout file and a corresponding Java activity file that handles user interactions and calculations. The calculator performs basic arithmetic operations, the radio button app displays the selected gender, and the marksheet calculates total marks and percentage from three subjects.

Uploaded by

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

Mad PRG

The document provides code for three Android applications: a simple calculator, a radio button selection interface, and a student marksheet using a table layout. Each application includes an XML layout file and a corresponding Java activity file that handles user interactions and calculations. The calculator performs basic arithmetic operations, the radio button app displays the selected gender, and the marksheet calculates total marks and percentage from three subjects.

Uploaded by

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

5.

WAP TO IMPLEMENT SIMPLE CALCULATOR


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 1"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 2"
android:inputType="numberDecimal" />

<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+" />

<Button
android:id="@+id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-" />

<Button
android:id="@+id/mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="x" />

<Button
android:id="@+id/div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/" />
</LinearLayout>
MainActivity.java
package com.example.simplecalc;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
EditText et1, et2;
Button add, sub, mul, div;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
add = findViewById(R.id.add);
sub = findViewById(R.id.sub);
mul = findViewById(R.id.mul);
div = findViewById(R.id.div);

add.setOnClickListener(this);
sub.setOnClickListener(this);
mul.setOnClickListener(this);
div.setOnClickListener(this);
}

public void onClick(View view){


String n1 = et1.getText().toString();
String n2 = et2.getText().toString();

double number1, number2;


try {
number1 = Double.parseDouble(n1);
number2 = Double.parseDouble(n2);
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this,"Invalid

Input",Toast.LENGTH_LONG).show();
return;
}

double output;
if (view.getId() == R.id.add) {
output = number1 + number2;
} else if (view.getId() == R.id.sub) {
output = number1 - number2;
} else if (view.getId() == R.id.mul) {
output = number1 * number2;
} else if (view.getId() == R.id.div) {
if (number2 == 0) {

return;
}
output = number1 / number2;
} else {
return;
}
Toast.makeText(MainActivity.this,"Result =
"+output,Toast.LENGTH_LONG).show();
}

}
6. WAP TO IMPLEMENT RADIOGROUP AND RADIOBUTTON
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="30dp" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="30dp" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="40dp"/>
</RadioGroup>
</LinearLayout>
MainActivity.java
package com.example.radiobutton;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity


{
RadioGroup rg1;
RadioButton r1,r2;
TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rg1 = findViewById(R.id.rg1);
r1 = findViewById(R.id.r1);
r2 = findViewById(R.id.r2);
tv1 = findViewById(R.id.tv1);
rg1.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup g, int i)
{
if(i==R.id.r1)
{
tv1.setText("Male");
} else if (i==R.id.r2)
{
tv1.setText("Female");
}
}
});

}
}
7. WAP DOR STUDENT MARKSHEET USING TABLELAYOUT, 3
SUBJECTS MARKS WITH TOTAL & PERCENTAGE
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Subject 1: "
android:textSize="40dp"
android:layout_gravity="left"/>
<EditText
android:id="@+id/et1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Subject 1: "
android:textColorHint="#6A6A6A"
android:textSize="30dp"
android:layout_gravity="right" />
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Subject 2: "
android:textSize="40dp"
android:layout_gravity="left"/>
<EditText
android:id="@+id/et2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Subject 2: "
android:textColorHint="#6A6A6A"
android:textSize="30dp"
android:layout_gravity="right" />
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Subject 3: "
android:textSize="40dp"
android:layout_gravity="left"/>
<EditText
android:id="@+id/et3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Subject 3: "
android:textColorHint="#6A6A6A"
android:textSize="30dp"
android:layout_gravity="right" />
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Total Marks: "
android:textSize="40dp"
android:layout_gravity="left"/>
<TextView
android:id="@+id/tv1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" "
android:textSize="40dp"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:text="Percentage: "
android:textSize="40dp"
android:layout_gravity="left"/>
<TextView
android:id="@+id/tv2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=" "
android:textColor="@color/white"
android:textSize="40dp"
android:layout_gravity="left"/>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center">
<Button
android:id="@+id/b1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"
android:text="Submit"
android:textSize="30dp" />
</TableRow>
</TableLayout>
MainActivity.java
package com.example.tablelayout2;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {


Button b1;
EditText et1,et2,et3;
TextView tv1,tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//----------------------------------------------
b1=findViewById(R.id.b1);
et1=findViewById(R.id.et1);
et2=findViewById(R.id.et2);
et3=findViewById(R.id.et2);

//------------------------------------------------
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a,b,c,total;
String per;
a=Integer.parseInt(et1.getText().toString());
b=Integer.parseInt(et2.getText().toString());
c=Integer.parseInt(et3.getText().toString());
total=a+b+c;
per= String.valueOf((total/300)*100);
tv1.setText(total);
tv2.setText(per);
Toast.makeText(MainActivity.this,"Result is
"+per+"%",Toast.LENGTH_LONG).show();
}
});
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy