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

Android XML Layout and Java Code Example

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)
127 views2 pages

Android XML Layout and Java Code Example

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

Practical No 9

Q1.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />

</[Link]>

Q2.
activity_main.xml

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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>
</[Link]>

[Link]

package [Link].practicalno9;

import [Link];

import [Link];
import [Link];

import [Link].*;

public class MainActivity extends AppCompatActivity implements [Link] {


TextView res,ans;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
res = findViewById([Link]);
ans = findViewById([Link]);
LinearLayout l = findViewById([Link].main_layout);
String btns_text ="789/456*123-C0=+";
for (int i = 1,ind = 0; i <= 4; i++) {
LinearLayout child = new LinearLayout(this);
[Link](new
[Link]([Link].MATCH_PARENT,[Link]
ms.WRAP_CONTENT));
[Link]([Link]);
for (int j = 1; j <= 4; j++,ind++) {
Button b = new Button(this);

[Link](300);
[Link](200);
[Link](btns_text.charAt(ind)+"");
[Link](this);
[Link](b);
}
[Link](child);
}
}
@Override
public void onClick(View v) {
Button b = (Button) v;
String text = [Link]().toString();
if ([Link]("C"))
[Link]("");
else if ([Link]("="))
[Link]([Link]([Link]().toString()));
else
[Link]([Link]().toString()+text);
}
}

You might also like