0% found this document useful (0 votes)
39 views2 pages

Practical No 9

The document contains an XML layout file for an Android activity that includes an EditText and ToggleButton. It also contains code for a Java class that implements an activity with buttons to perform calculations on expressions entered in the EditText, displaying the result in a TextView. The activity generates buttons dynamically in a grid layout to perform arithmetic operations and calculate expressions entered by the user.

Uploaded by

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

Practical No 9

The document contains an XML layout file for an Android activity that includes an EditText and ToggleButton. It also contains code for a Java class that implements an activity with buttons to perform calculations on expressions entered in the EditText, displaying the result in a TextView. The activity generates buttons dynamically in a grid layout to perform arithmetic operations and calculate expressions entered by the user.

Uploaded by

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

Practical No 9

Q1.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">

<EditText
android:layout_width="162dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Atharva Butte"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ToggleButton
android:id="@+id/tbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Q2.
activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
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">
<EditText
android:layout_width="162dp"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:singleLine="false"
android:text="Atharva Butte"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<LinearLayout
android:id="@+id/main_layout"
android:layout_width="413dp"
android:layout_height="670dp"
android:orientation="vertical"
android:padding="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/ans"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="viewEnd"
android:textSize="48sp" />

<Space
android:layout_width="match_parent"
android:layout_height="65dp" />

<TextView
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAlignment="viewEnd"
android:textSize="34sp" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.practicalno9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import android.widget.*;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


TextView res,ans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = findViewById(R.id.input);
ans = findViewById(R.id.ans);
LinearLayout l = findViewById(R.id.main_layout);
String btns_text ="789/456*123-C0=+";
for (int i = 1,ind = 0; i <= 4; i++) {
LinearLayout child = new LinearLayout(this);
child.setLayoutParams(new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutPara
ms.WRAP_CONTENT));
child.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 1; j <= 4; j++,ind++) {
Button b = new Button(this);

b.setWidth(300);
b.setHeight(200);
b.setText(btns_text.charAt(ind)+"");
b.setOnClickListener(this);
child.addView(b);
}
l.addView(child);
}
}
@Override
public void onClick(View v) {
Button b = (Button) v;
String text = b.getText().toString();
if (text.equals("C"))
res.setText("");
else if (text.equals("="))
ans.setText(Expression.eval(res.getText().toString()));
else
res.setText(res.getText().toString()+text);
}
}

You might also like