Practical n0.9
Practical n0.9
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;
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) {
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
<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
<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 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;
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));
}
}
});
}
}