0% found this document useful (0 votes)
7 views5 pages

Practical NO - 8

The document provides a practical implementation of an AutoCompleteTextView in an Android application. It includes the XML layout for the AutoCompleteTextView and the corresponding Java code in MainActivity to populate it with a list of country names. The program initializes the AutoCompleteTextView and sets an ArrayAdapter to provide suggestions based on user input.
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)
7 views5 pages

Practical NO - 8

The document provides a practical implementation of an AutoCompleteTextView in an Android application. It includes the XML layout for the AutoCompleteTextView and the corresponding Java code in MainActivity to populate it with a list of country names. The program initializes the AutoCompleteTextView and sets an ArrayAdapter to provide suggestions based on user input.
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/ 5

Practical NO.

08

Develop a program to Implement Auto Complete Text View

• activity_main.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Country"
android:completionThreshold="1"
android:textColor="@android:color/black"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

• MainActivity.java

package com.example.prac8;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

// Initialize AutoCompleteTextView
AutoCompleteTextView autoCompleteTextView =
findViewById(R.id.autoCompleteTextView);

// List of countries (for demonstration purposes)


String[] countries = {"India", "United States", "United Kingdom", "Australia", "Canada",
"Germany", "France", "Italy", "China", "Japan"};

// Adapter to provide suggestions


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

// Set adapter to AutoCompleteTextView


autoCompleteTextView.setAdapter(adapter);
}
}
Output

You might also like