Question 11: Write a program to create a toggle button to display ON / OFF Bluetooth on the display screen.
Download hole Program / Project code, by clicking following link:
Write a piece of code to set id of the button in Android Studio?
In Android, you can set the ID of a button in the XML layout file or programmatically in the Java/Kotlin code. Here's an example code snippet to set the ID of a button programmatically in Java:
Button button = new Button(this); // Create a new button instanceIn this example, we first create a new instance of the Button class using the new keyword. Then, we set the ID of the button using the setId() method and passing the resource ID R.id.my_button as the argument. The resource ID my_button can be any unique name you choose for the button ID. Note that the above code assumes that you are setting the ID of the button within an Activity or other Context object. If you are setting the ID from within a Fragment, Service or other non-Activity context, you will need to use a reference to the Context object in place of this. Also note that if you are setting the ID of the button in the XML layout file, you can do so by adding the android:id attribute to the <Button> tag and setting it to a unique ID value. For example:
button.setId(R.id.my_button); // Set the ID of the button
<ButtonThis will create a button with the ID my_button and a text "Click me!".
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!" />
How to add image to resources file in Android Studio?
To add an image to the resources file in Android Studio, you can follow these steps:
- Open your Android Studio project.
- Right-click on the res folder in the project structure panel and select New > Image Asset.
- In the Asset Studio dialog that appears, select Image from the Asset Type dropdown.
- Select the image file that you want to add to the resources by clicking on the path button and browsing to the location of the image file on your computer.
- Choose a name for the image in the Resource Name field.
- Choose the directory where you want to store the image file. By default, Android Studio creates separate folders for different densities of images (mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) to ensure the image is appropriately scaled for different screen resolutions.
- Choose the File Type for the image. If the image is a bitmap, choose PNG, JPG or WEBP format. For vector images, choose SVG format.
- You can also add additional attributes like padding or background color for the image asset.
- Click Next and then Finish to create the image asset.
- The image file will be added to the appropriate directory in the res folder and can be accessed in your code using its resource ID.
Alternatively, you can manually copy and paste the image file into the appropriate folder under the res directory in your project structure. For example, to add an image to the drawable folder, you can copy the image file to res/drawable folder in your project directory.
Note that it is recommended to use Asset Studio to create image assets as it automatically generates images of different densities to support various screen sizes and densities.
Programming Code:
Following code write in: MainActivity.java
package com.go2collage.practical_11;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = findViewById(R.id.toggleBtn);
textView = findViewById(R.id.txtView);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
textView.setText("Bluetooth is "+ toggleButton.getTextOn());
}
else {
textView.setText("Bluetooth is "+ toggleButton.getTextOff());
}
}
});
}
}
Following code write in: activity_main.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"
tools:context=".MainActivity"
android:layout_margin="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="go2collage"
android:textStyle="bold"
android:textAlignment="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="50dp"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toggle Button Program"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/black"
android:textAlignment="center"
android:layout_marginBottom="30dp"
tools:ignore="HardcodedText" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toggleBtn"
android:text="Toggle Button"
android:layout_gravity="center"
android:textOn="ON"
android:textOff="OFF"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtView"
android:text="Bluetooth if OFF"
android:textSize="18sp"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="@color/black"
android:layout_marginTop="20dp"
tools:ignore="HardcodedText" />
</LinearLayout>
Output:
0 Comments