Ad Code

Responsive Advertisement

Basic Android Program 12

Question 12: Write a program to create a simple calculator.

Download hole Program / Project code, by clicking following link:

List four Android Toggle Button control attributes?

Here are four common attributes of the Android ToggleButton control:
  1. android:textOn: This attribute sets the text to be displayed when the toggle button is in the "on" state.
  2. android:textOff: This attribute sets the text to be displayed when the toggle button is in the "off" state.
  3. android:thumb: This attribute sets the image to be used as the thumb (the circular button that slides back and forth) for the toggle button.

  4. android:track: This attribute sets the image to be used as the track (the background behind the thumb) for the toggle button.
Note that these are just a few of the many attributes that can be used with the ToggleButton control. Other attributes include android:checked (to set the initial state of the button), android:onClick (to specify a method to be called when the button is clicked), and many more.


Programming Code:

Following code write in: MainActivity.java
package com.go2collage.practical_12;

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;

public class MainActivity extends AppCompatActivity {

private EditText ed1, ed2;
private Button plus, minus, div, mul, mod;
private TextView calResult;

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

ed1 = findViewById(R.id.edNum1);
ed2 = findViewById(R.id.edNum2);
plus = findViewById(R.id.btnPlus);
minus = findViewById(R.id.btnMinus);
div = findViewById(R.id.btnDivider);
mul = findViewById(R.id.btnMul);
mod = findViewById(R.id.btnMod);
calResult = findViewById(R.id.txtResult);

plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float n1 = Float.parseFloat(ed1.getText().toString());
float n2 = Float.parseFloat(ed2.getText().toString());
float result = n1 + n2;
calResult.setText("Addition is: "+ result);
}
});

minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float n1 = Float.parseFloat(ed1.getText().toString());
float n2 = Float.parseFloat(ed2.getText().toString());
float result = n1 - n2;
calResult.setText("Subtraction is: "+ result);
}
});

div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float n1 = Float.parseFloat(ed1.getText().toString());
float n2 = Float.parseFloat(ed2.getText().toString());
float result = n1 / n2;
calResult.setText("Division is: "+ result);
}
});

mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float n1 = Float.parseFloat(ed1.getText().toString());
float n2 = Float.parseFloat(ed2.getText().toString());
float result = n1 * n2;
calResult.setText("Multiplication is: "+ result);
}
});

mod.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float n1 = Float.parseFloat(ed1.getText().toString());
float n2 = Float.parseFloat(ed2.getText().toString());
float result = n1 % n2;
calResult.setText("Percentage is: "+ result);
}
});

}

}

Following code write in: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="20dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="0dp"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="go2collage"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculator"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Number"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_marginTop="30dp"
tools:ignore="HardcodedText" />

<EditText
android:id="@+id/edNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="10"
android:hint="Enter Integer Number 1"
android:minHeight="48dp"
android:textColor="@color/black"
android:textColorHint="#757575"
android:textSize="18sp"
tools:ignore="Autofill,HardcodedText,TextFields" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Number"
android:textSize="18sp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_marginTop="15dp"
tools:ignore="HardcodedText" />

<EditText
android:id="@+id/edNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Enter Integer Number 2"
android:minHeight="48dp"
android:textColor="@color/black"
android:textColorHint="#757575"
android:textSize="18sp"
tools:ignore="Autofill,HardcodedText,TextFields" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose following"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_marginTop="18dp"
android:layout_marginBottom="10dp"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp"
android:gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="50dp"
android:id="@+id/btnPlus"
android:text="+"
android:textStyle="bold"
android:textSize="32sp"
tools:ignore="ButtonStyle,HardcodedText,RtlHardcoded" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnMinus"
android:text="-"
android:textStyle="bold"
android:textSize="32sp"
tools:ignore="ButtonStyle,HardcodedText" />


</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp"
android:gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDivider"
android:text="/"
android:layout_marginRight="50dp"
android:textStyle="bold"
android:textSize="32sp"
tools:ignore="ButtonStyle,HardcodedText,RtlHardcoded" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnMul"
android:text="X"
android:textStyle="bold"
android:textSize="32sp"
tools:ignore="ButtonStyle,HardcodedText" />

</LinearLayout>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_marginRight="20dp"
android:id="@+id/btnMod"
android:text="%"
android:textStyle="bold"
android:textSize="32sp"
tools:ignore="ButtonStyle,HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculation Result"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:text=""
android:textAlignment="center"
android:textColor="@color/black"
android:layout_marginTop="2dp"
android:layout_marginBottom="30dp"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
</LinearLayout>

</ScrollView>

Output:



Post a Comment

0 Comments

Ad Code

Responsive Advertisement