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

Mad 9

The document contains XML layouts and Java code for two Android applications: one for a Bluetooth toggle button and another for a simple calculator. The first application features a toggle button that updates the status text based on its state, while the second application allows users to perform basic arithmetic operations. Both applications utilize color resources defined in XML for UI elements.

Uploaded by

prasad wadkar
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)
26 views8 pages

Mad 9

The document contains XML layouts and Java code for two Android applications: one for a Bluetooth toggle button and another for a simple calculator. The first application features a toggle button that updates the status text based on its state, while the second application allows users to perform basic arithmetic operations. Both applications utilize color resources defined in XML for UI elements.

Uploaded by

prasad wadkar
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

 XML code:-

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="Bluethoot"
android:textColor="@color/black"
android:textSize="30sp"
android:textStyle="bold|normal" />

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tglbtn"
android:layout_toRightOf="@id/text1"
android:layout_marginLeft="20sp"
android:onClick="ToggleEvent"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/status"
android:textColor="@color/blue"
android:layout_below="@id/text1"
android:layout_marginTop="30sp"
android:layout_marginLeft="50sp"
android:textSize="20sp"
android:text=""/>
</RelativeLayout>

 Colour XML code :-


<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="blue">#001289</color>
</resources>

 Java Code :-

package com.example.mad9;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

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 {


ToggleButton btn;
TextView status;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.tglbtn);
status=findViewById(R.id.status);
}
public void ToggleEvent(View v){
if(btn.getText().toString().toLowerCase().equals("off")){
status.setText("It is OFF Now");
}else {
status.setText("It is On Now");
}
}
}
Output :-
2)

 XML code :-

<?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="20dp"
android:layout_marginTop="60dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|center"
android:text="calculator"
android:layout_marginLeft="130dp"
android:textSize="20dp"
android:layout_marginBottom="40dp">
</TextView>

<EditText
android:id="@+id/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"
android:textSize="20sp"/>

<EditText
android:id="@+id/num2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
android:textSize="20sp"/>

<Button
android:id="@+id/btnAdd"
android:backgroundTint="@color/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Addition (+)" />

<Button
android:id="@+id/btnSubtract"
android:backgroundTint="@color/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtraction (-)" />

<Button
android:id="@+id/btnMultiply"
android:backgroundTint="@color/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiplication (×)" />

<Button
android:id="@+id/btnDivide"
android:backgroundTint="@color/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Division (÷)" />

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="20sp"
android:textStyle="bold"
android:padding="10dp" />

</LinearLayout>

 Colour XML code:-

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="blue">#001289</color>
</resources>

 Java code :-
package com.example.mad9;

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.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


EditText num1, num2;
Button btnAdd, btnSubtract, btnMultiply, btnDivide;
TextView result;

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

num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
result = findViewById(R.id.result);

btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('+');
}
});

btnSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('-');
}
});

btnMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('*');
}
});
btnDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('/');
}
});
}

private void calculate(char operator) {


String value1 = num1.getText().toString();
String value2 = num2.getText().toString();

if (value1.isEmpty() || value2.isEmpty()) {
Toast.makeText(this, "Please enter both numbers",
Toast.LENGTH_SHORT).show();
return;
}

double number1 = Double.parseDouble(value1);


double number2 = Double.parseDouble(value2);
double output = 0;

switch (operator) {
case '+':
output = number1 + number2;
break;
case '-':
output = number1 - number2;
break;
case '*':
output = number1 * number2;
break;
case '/':
if (number2 == 0) {
Toast.makeText(this, "Number cannot divide by 0",
Toast.LENGTH_SHORT).show();
return;
}
output = number1 / number2;
break;
}
result.setText("Result: " + output);
}
}

 OutPut :-

You might also like