Practical 8
xml code
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<!-- AutoCompleteTextView for displaying suggestions -->
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter country name"
android:dropDownHeight="200dp"
android:padding="16dp" />
<!-- Button to show the selected item -->
<Button
android:id="@+id/showSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected Country"
android:layout_marginTop="20dp" />
</LinearLayout>
Java code
package com.example.practical8;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextView;
private Button showSelectionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Link to the XML layout
// Initialize the views
autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
showSelectionButton = findViewById(R.id.showSelectionButton);
// Create a list of suggestions for the AutoCompleteTextView
String[] countries = new String[] {
"India", "United States", "Australia", "Canada", "Germany", "Brazil",
"United Kingdom", "France", "Italy", "Spain", "Russia", "China", "Japan"
};
// Create an ArrayAdapter for AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, countries);
// Set the adapter to AutoCompleteTextView
autoCompleteTextView.setAdapter(adapter);
// Set an item click listener to handle when a user selects a suggestion
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedCountry = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "You selected: " + selectedCountry,
Toast.LENGTH_SHORT).show();
}
});
// Set a listener for the button to show the selected item
showSelectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the current text in AutoCompleteTextView
String selectedCountry = autoCompleteTextView.getText().toString();
if (!selectedCountry.isEmpty()) {
Toast.makeText(MainActivity.this, "You selected: " + selectedCountry,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No country selected",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Practical 9
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_marginBottom="20dp" />
<ToggleButton
android:id="@+id/myToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
android:layout_marginBottom="20dp"
android:checked="false"/>
<ImageButton
android:id="@+id/myImageButton"
android:layout_width="319dp"
android:layout_height="383dp"
android:contentDescription="Image Button"
android:scaleType="fitCenter"
android:src="@drawable/img_1" />
</LinearLayout>
Java code
package com.example.practical9;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Inflate the layout
// Find the ToggleButton by its ID
ToggleButton myToggleButton = findViewById(R.id.myToggleButton);
// Set an OnCheckedChangeListener to handle the toggle state change
myToggleButton.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Check the toggle state (ON/OFF)
if (isChecked) {
// If the toggle is ON
Toast.makeText(MainActivity.this, "Toggle ON", Toast.LENGTH_SHORT).show();
} else {
// If the toggle is OFF
Toast.makeText(MainActivity.this, "Toggle OFF", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Practical 10
<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="25dp"
android:orientation="vertical"
android:layout_marginTop="160">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Username"
android:textSize="25dp"
android:textColor="@color/black"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter valid username"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="25dp"
android:textColor="@color/black"
android:layout_marginTop="29dp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter password"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_gravity="center"
android:layout_marginTop="29dp"
android:textSize="20"/>
</LinearLayout>