You are on page 1of 3

Practical 09: Develop a program to implement Button, Image Button, and Toggle

Button

1. Implementation of Button

File code for: activity_main.xml file location layout

<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:padding="20dp"
android:orientation="vertical">
<Button
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click to check"
android:textSize="15dp"
/>
<TextView
android:id="@+id/showView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:hint="See when button clicked"
android:textColor="#A10505"
android:textSize="30sp" />
</LinearLayout>

File code for: MainActivity.java file location java

Button b1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
b1 = findViewById(R.id.display);
t1 = findViewById(R.id.showView);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t1.setText("Button Clicked");
}
});
}

2. Implementation of Image Button

File code for: activity_main.xml file location layout

<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:padding="20dp"
android:orientation="vertical">
<ImageButton
android:id="@+id/display"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="Click to check"
android:textSize="15dp"
android:src="@drawable/android"
/>
<TextView
android:id="@+id/showView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:hint="See when button clicked"
android:textColor="#A10505"
android:textSize="30sp" />
</LinearLayout>

File code for: MainActivity.java file location java

ImageButton ib1;
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
ib1 = findViewById(R.id.display);
t1 = findViewById(R.id.showView);
ib1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t1.setText("Image Button Clicked by User");
}
});
}

3. Implementation of Toggle Button

File code for: activity_main.xml file location layout

<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:padding="20dp"
android:orientation="vertical">
<ToggleButton
android:id="@+id/display"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="Click to check"
android:textSize="15dp"
android:src="@drawable/android"
/>
</LinearLayout>

File code for: MainActivity.java file location java

ToggleButton tb1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
tb1 = findViewById(R.id.display);
tb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tb1.setText("ON");
}
});
}

You might also like