0% found this document useful (0 votes)
8 views4 pages

AutoCompleteTextView Example Code

The document contains XML and Java code for an Android application featuring an AutoCompleteTextView and a Button. The AutoCompleteTextView allows users to select from a list of countries or subjects, while the Button displays the selected item using a Toast message. Two separate implementations are provided, one for country selection and another for subject selection.

Uploaded by

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

AutoCompleteTextView Example Code

The document contains XML and Java code for an Android application featuring an AutoCompleteTextView and a Button. The AutoCompleteTextView allows users to select from a list of countries or subjects, while the Button displays the selected item using a Toast message. Two separate implementations are provided, one for country selection and another for subject selection.

Uploaded by

gaurangrane4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, 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