0% found this document useful (0 votes)
6 views2 pages

Practical No 8

The document contains an XML layout for an Android application featuring an AutoCompleteTextView for searching cities and a TextView to display the selected city. The Java code in MainActivity sets up the AutoCompleteTextView with a predefined list of cities and updates the TextView with the selected city upon user interaction. This implementation allows users to easily search and select a city from the provided list.

Uploaded by

Vaman Kulkarni
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)
6 views2 pages

Practical No 8

The document contains an XML layout for an Android application featuring an AutoCompleteTextView for searching cities and a TextView to display the selected city. The Java code in MainActivity sets up the AutoCompleteTextView with a predefined list of cities and updates the TextView with the selected city upon user interaction. This implementation allows users to easily search and select a city from the provided list.

Uploaded by

Vaman Kulkarni
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/ 2

Practical No 8

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- AutoCompleteTextView -->


<AutoCompleteTextView
android:id="@+id/autocompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownHeight="wrap_content"
android:hint="Search City"
android:minHeight="48dp"
android:padding="10dp" />

<!-- TextView to display the selected item -->


<TextView
android:id="@+id/selectedCityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/autocompleteTextView"
android:layout_marginTop="16dp"
android:text="Selected City will appear here"
android:textSize="18sp" />
</RelativeLayout>

Main.java

package com.example.auto_complete;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Reference AutoCompleteTextView and TextView


AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autocompleteTextView);
TextView selectedCityTextView = findViewById(R.id.selectedCityTextView);

// Data for AutoCompleteTextView (can be replaced with dynamic data)


String[] cities = {
"New Delhi", "Mumbai", "Kolkata", "Bangalore", "Chennai",
"Hyderabad", "Pune", "Ahmedabad", "Jaipur", "Lucknow",
"Chandigarh", "Indore", "Bhopal", "Nagpur", "Surat",
"Patna", "Visakhapatnam", "Vadodara", "Coimbatore", "Nashik",
"Gurgaon", "Noida", "Faridabad", "Mysuru", "Kochi",
"Thane", "Kanpur", "Agra", "Vijayawada", "Guwahati",
"Madurai", "Ranchi", "Bhubaneswar", "Shimla", "Udaipur"
};

// Create an ArrayAdapter to provide data to AutoCompleteTextView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,
cities);

// Set the adapter to AutoCompleteTextView


autoCompleteTextView.setAdapter(adapter);

// Add item click listener to AutoCompleteTextView


autoCompleteTextView.setOnItemClickListener((parent, view, position, id) -> {
// Get the selected city from the adapter
String selectedCity = (String) parent.getItemAtPosition(position);

// Display the selected city in the TextView


selectedCityTextView.setText("Selected City: " + selectedCity);
});}}

Output:

You might also like