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.practical11;
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();
}
}
});
}
}
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 subject 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 Subject"
android:layout_marginTop="20dp" />
</LinearLayout>
Java code
package com.example.practical11;
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 subjects for the AutoCompleteTextView
String[] subjects = new String[] {
"MAD", "MAN", "AAM", "NIS", "EDE", "CPE"
};
// Create an ArrayAdapter for AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, subjects);
// 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 selectedSubject = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "You selected: " + selectedSubject,
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 selectedSubject = autoCompleteTextView.getText().toString();
if (!selectedSubject.isEmpty()) {
Toast.makeText(MainActivity.this, "You selected: " + selectedSubject,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No subject selected",
Toast.LENGTH_SHORT).show();
}
}
});
}
}