0% found this document useful (0 votes)
16 views7 pages

Practical n0.9

Uploaded by

sdevi.pal.19
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)
16 views7 pages

Practical n0.9

Uploaded by

sdevi.pal.19
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/ 7

------------------------------------------------------------------------------------------

Practical No: 09
Title: Develop a program to implement Button, Image Button and Toggle
Button
class: TYCO Roll No: 12

1. Button
// Java Code
package com.example.button_demo;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


implements View.OnClickListener {

Button bt1,bt2,bt3,bt4;
ImageButton img_btn;
EditText et1,et2;
String str1,str2;
int res;

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

bt1=(Button)findViewById(R.id.b1); // Button
xml id bind with Button class object
bt2=(Button)findViewById(R.id.b2);
bt3=(Button)findViewById(R.id.b3);
bt4=(Button)findViewById(R.id.b4);

img_btn=(ImageButton)findViewById(R.id.img1) ;
et1=(EditText)findViewById(R.id.e1); // EDit
text box
et2=(EditText)findViewById(R.id.e2);
;
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
img_btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {

switch(v.getId()) // V is the Curernt View


of Screen
{
case R.id.b1:
str1=et1.getText().toString();
str2=et2.getText().toString();
res=Integer.parseInt(str1)+
Integer.parseInt(str2);
Toast.makeText(this,"Addition
"+res,Toast.LENGTH_SHORT).show();
break;

case R.id.b2:
str1=et1.getText().toString();
str2=et2.getText().toString();
res=Integer.parseInt(str1)-
Integer.parseInt(str2);

Toast.makeText(this,"Substration is
"+res,Toast.LENGTH_SHORT).show();
break;

case R.id.b3:
str1=et1.getText().toString();
str2=et2.getText().toString();
res=Integer.parseInt(str1)*
Integer.parseInt(str2);
Toast.makeText(this," Multiplication
is "+res,Toast.LENGTH_SHORT).show();
break;

case R.id.b4:
str1=et1.getText().toString();
str2=et2.getText().toString();
if(str1.equals("") &&
str2.equals(""))
{
Toast.makeText(this,"Plz Enetr
the Number", Toast.LENGTH_SHORT).show();
}
else {
res = Integer.parseInt(str1) /
Integer.parseInt(str2);
Toast.makeText(this, " Division
is " + res, Toast.LENGTH_SHORT).show();
}
break;

case R.id.img1 :

Toast.makeText(getApplication(),"You Have
Click on Image ",Toast.LENGTH_SHORT).show();

break;

}
}

// XML

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


<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/e1"></EditText>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/e2"></EditText>
<Button
android:layout_width="400px"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Addition"
android:layout_marginTop="100px"
android:layout_gravity="center"
>
</Button>

<Button
android:layout_width="420px"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="Substration"
android:layout_gravity="center"

>
</Button>

<Button
android:layout_width="400px"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:text="Multiplication"
android:layout_gravity="center"
>
</Button>
<Button
android:layout_width="400px"
android:layout_height="wrap_content"
android:id="@+id/b4"
android:text="Division"
android:layout_gravity="center"
>
</Button>
<ImageButton
android:layout_width="620px"
android:layout_height="wrap_content"
android:id="@+id/img1"
android:src="@drawable/submit"
android:layout_gravity="center"
></ImageButton>

</LinearLayout>
Fig. Button

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


<RelativeLayout
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">

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tg1"
android:textOff=" OFF"
android:textOn=" ON"
android:drawableStart="@drawable/selector"
android:drawableLeft="@drawable/selector"
android:layout_marginLeft="150dp"
></ToggleButton>

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:id="@+id/image_v"
></ImageView>
</RelativeLayout>

******************************************************************************************

/// Selector.xml ////


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

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true"
android:drawable="@drawable/ic_power_on"></item>

<item android:state_checked="false"
android:drawable="@drawable/ic_power_off"></item>

</selector>

*********************************************************************************************************************************

// JAVA CODE
package com.example.tg_demo;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

ToggleButton tg_bt;
ImageView img_v;

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

tg_bt=(ToggleButton)findViewById(R.id.tg1);
img_v=(ImageView)findViewById(R.id.image_v);

img_v.setImageDrawable(getResources().getDrawable(R.drawable.light_o
ff));

tg_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(tg_bt.isChecked())
{

img_v.setImageDrawable(getResources().getDrawable(R.drawable.light_o
n));
}
else
{

img_v.setImageDrawable(getResources().getDrawable(R.drawable.light_o
ff));
}
}
});

}
}

Fig. Toggle Button

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy