0% found this document useful (0 votes)
3 views

AutoCompleteTextView MAD

The document contains XML and Java code for an Android application that features an AutoCompleteTextView for selecting countries or subjects. It includes a button that displays the selected item using Toast messages. Two separate implementations are provided, one for countries and another for subjects, with similar functionality.

Uploaded by

bye128168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

AutoCompleteTextView MAD

The document contains XML and Java code for an Android application that features an AutoCompleteTextView for selecting countries or subjects. It includes a button that displays the selected item using Toast messages. Two separate implementations are provided, one for countries and another for subjects, with similar functionality.

Uploaded by

bye128168
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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();
}
}
});
}
}

You might also like