Lab Sheet 7: Switch and Toggle Buttons
Lab Sheet 7: Switch and Toggle Buttons
1.Design an app for home automation system. When ever the switch is on , change the color to
green.
MainActivity.java
package com.example.switcheslab7;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Switch;
import android.widget.TextView;
int images [] =
{R.drawable.light,R.drawable.tv,R.drawable.hometheatre,R.drawable.vide
ogame};
GridView grv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grv = findViewById(R.id.gv);
grv.setAdapter(new MyAdapter(this));
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v =
LayoutInflater.from(context).inflate(R.layout.layout,viewGroup,
false);
s1 = v.findViewById(R.id.s1);
t1 = v.findViewById(R.id.t1);
t2 = v.findViewById(R.id.t2);
iv1= v.findViewById(R.id.iv1);
t1.setText(names[i]);
t2.setText(names1[i]);
iv1.setImageResource(images[i]);
return v;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/linearLayout"
android:layout_width="150dp"
android:layout_height="200dp">
<ImageView
android:id="@+id/iv1"
android:layout_width="62dp"
android:layout_height="70dp"
android:layout_marginStart="28dp"
android:layout_marginEnd="38dp"
android:layout_marginBottom="13dp"
app:layout_constraintBottom_toTopOf="@+id/t1"
app:layout_constraintEnd_toStartOf="@+id/s1"
app:layout_constraintStart_toStartOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/t1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="12dp"
android:hint="names"
android:text="TextView"
android:textColor="#0B0909"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/t2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/t2"
app:layout_constraintTop_toBottomOf="@+id/s1" />
<TextView
android:id="@+id/t2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="28dp"
android:layout_marginEnd="28dp"
android:hint="names1"
android:text="TextView"
android:textColor="#191616"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />
<Switch
android:id="@+id/s1"
android:layout_width="47dp"
android:layout_height="33dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="9dp"
android:layout_marginBottom="36dp"
android:shadowColor="@color/purple_200"
android:textColor="#4CAF50"
android:textColorHint="#4CAF50"
app:layout_constraintBottom_toTopOf="@+id/t1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/iv1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
OUTPUT:
2. Design an App for a Home Automation using Toggle button.
MainActivity.java:
package com.example.togglebutton;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v =
LayoutInflater.from(c).inflate(R.layout.layout2,viewGroup, false);
t1 = v.findViewById(R.id.t1);
tb1 = v.findViewById(R.id.tb1);
t1.setText(names[i]);
return v;
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<ListView
android:id="@+id/lsv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFEB3B">
<TextView
android:id="@+id/t1"
android:layout_width="181dp"
android:layout_height="73dp"
android:layout_marginStart="57dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="7dp"
android:background="#009688"
android:backgroundTint="#E91E63"
android:text="TextView"
android:textAlignment="center"
android:textColor="#2196F3"
android:textSize="20sp"
app:layout_constraintEnd_toStartOf="@+id/tb1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="@+id/tb1"
android:layout_width="129dp"
android:layout_height="68dp"
android:layout_marginEnd="57dp"
android:background="#65AFEA"
android:scrollbarStyle="insideOverlay"
android:scrollHorizontally="false"
android:shadowColor="@color/purple_700"
android:text="ToggleButton"
app:layout_constraintBottom_toBottomOf="@+id/t1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/t1"
app:layout_constraintTop_toTopOf="@+id/t1" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output: