0% found this document useful (0 votes)
23 views4 pages

XML File: Java File

The XML file defines a vertical LinearLayout containing a ToggleButton and TextView. The Java file is a MainActivity class that sets the layout view, gets references to the ToggleButton and TextView, and defines an onToggleClick method to update the TextView text based on the ToggleButton state.
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)
23 views4 pages

XML File: Java File

The XML file defines a vertical LinearLayout containing a ToggleButton and TextView. The Java file is a MainActivity class that sets the layout view, gets references to the ToggleButton and TextView, and defines an onToggleClick method to update the TextView text based on the ToggleButton state.
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/ 4

XML File: Java File:

<LinearLayout package com.example.myapplication;


xmlns:android="http://schemas.android.com/apk/res/android" import androidx.appcompat.app.AppCompatActivity;
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" import android.annotation.SuppressLint;
android:layout_width="match_parent" import android.os.Bundle;
android:layout_height="match_parent" import android.view.View;
tools:context=".MainActivity" import android.widget.Button;
android:orientation="vertical"> import android.widget.EditText;
<EditText import android.widget.TextView;
android:id="@+id/t1" import android.widget.ToggleButton;
android:layout_width="wrap_content" public class MainActivity extends AppCompatActivity {
android:layout_height="wrap_content" EditText t1;
android:inputType="number" EditText t2;
/> Button b1;
<EditText Button b2;
android:id="@+id/t2" TextView t;
android:layout_width="wrap_content" @Override
android:layout_height="wrap_content" protected void onCreate(Bundle savedInstanceState) {
android:inputType="number" super.onCreate(savedInstanceState);
/> setContentView(R.layout.activity_main);
<Button t1=(EditText) findViewById(R.id.t1);
android:layout_width="wrap_content" t2=(EditText) findViewById(R.id.t2);
android:layout_height="wrap_content" b1=(Button) findViewById(R.id.b1);
android:text="ADD" b2=(Button) findViewById(R.id.b2);
android:onClick="add" t=(TextView) findViewById(R.id.r);
android:id="@+id/b1"/> }
<Button public void add(View view)
android:layout_width="wrap_content" {
android:layout_height="wrap_content" int a=Integer.parseInt(t1.getText().toString());
android:text="sub" int b=Integer.parseInt(t2.getText().toString());
android:onClick="sub" int r=a+b;
android:id="@+id/b2"/> t.setText(String.valueOf(r));
<TextView }
android:layout_width="wrap_content" public void sub(View view)
android:layout_height="wrap_content" {
android:id="@+id/r"/> int a=Integer.parseInt(t1.getText().toString());
</LinearLayout> int b=Integer.parseInt(t2.getText().toString());
int r=a-b;
t.setText(String.valueOf(r));
}
}
Output:
XML file: Java File:
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<LinearLayout import
xmlns:android="http://schemas.android.com/apk/res/androi androidx.appcompat.app.AppCompatActivit
d" y;
xmlns:app="http://schemas.android.com/apk/res-auto" import android.os.Bundle;
xmlns:tools="http://schemas.android.com/tools" import android.view.View;
android:layout_width="match_parent" import android.widget.TextView;
android:layout_height="match_parent" import android.widget.ToggleButton;
tools:context=".MainActivity"> import android.widget.Toolbar;
public class MainActivity extends
<ToggleButton AppCompatActivity {
android:layout_width="wrap_content" ToggleButton togglebutton;
android:layout_height="wrap_content" TextView textview;
android:id="@+id/togglebutton" @Override
android:onClick="onToggleClick"/> protected void onCreate(Bundle
<TextView savedInstanceState) {
android:layout_width="wrap_content" super.onCreate(savedInstanceState);
android:layout_height="wrap_content"
android:id="@+id/textview" setContentView(R.layout.activity_main);
/> togglebutton=(ToggleButton)
</LinearLayout> findViewById(R.id.togglebutton);
textview=(TextView)
findViewById(R.id.textview);
}
public void onToggleClick(View view)
{
if (togglebutton.isChecked()) {
textview.setText("Toggle is ON");
}
else {
textview.setText("Toggle is OFF");
}
}
}
Output:

You might also like