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

Practical No 8

The document contains a Java code for an Android application that implements an AutoCompleteTextView with a predefined list of fruit suggestions. It initializes the AutoCompleteTextView in the MainActivity and sets an ArrayAdapter to provide suggestions based on user input. The XML layout defines the user interface, positioning the AutoCompleteTextView in the center with a hint for user interaction.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Practical No 8

The document contains a Java code for an Android application that implements an AutoCompleteTextView with a predefined list of fruit suggestions. It initializes the AutoCompleteTextView in the MainActivity and sets an ArrayAdapter to provide suggestions based on user input. The XML layout defines the user interface, positioning the AutoCompleteTextView in the center with a hint for user interaction.

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 PDF, TXT or read online on Scribd
You are on page 1/ 2

PRACTICAL NO 8

#JAVA_FILE

package com.example.practicalno8;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

// Create an array of strings to be shown in the AutoCompleteTextView.


private String[] suggestions = {
"Apple", "Banana", "Cherry", "Date", "Grapes",
"Kiwi", "Lemon", "Mango", "Orange", "Papaya"
};

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

// Initialize the AutoCompleteTextView


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

// Create an ArrayAdapter to use the string array for suggestions


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

// Set the adapter to the AutoCompleteTextView


autoCompleteTextView.setAdapter(adapter);

// Optional: Set the threshold for how many characters to type before suggestions show
autoCompleteTextView.setThreshold(1); // Default is 2, set to 1 for quicker suggestions.
}
}
#xml_file

<?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">

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="Type to search"
android:layout_marginTop="200dp" />

</RelativeLayout>

You might also like