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

8 PR Ex

This document is a code snippet for an Android application that implements an AutoCompleteTextView for search functionality. It initializes a list of search suggestions and sets up a button to display a toast message with the user's query when clicked. The layout includes XML elements for the AutoCompleteTextView and the search button within a RelativeLayout.

Uploaded by

Pratiksha Wagh
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)
14 views2 pages

8 PR Ex

This document is a code snippet for an Android application that implements an AutoCompleteTextView for search functionality. It initializes a list of search suggestions and sets up a button to display a toast message with the user's query when clicked. The layout includes XML elements for the AutoCompleteTextView and the search button within a RelativeLayout.

Uploaded by

Pratiksha Wagh
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/ 2

protected void onCreate(Bundle

savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize AutoCompleteTextView and


Button
autoCompleteTextView =
<?xml version="1.0" encoding="utf-8"?
findViewById(R.id.searchAutoCompleteTextView);
>
searchButton =
<RelativeLayout
findViewById(R.id.searchButton);
xmlns:android="http://schemas.android.com/apk/re
s/android"
// Sample list of search terms (can be from a
android:layout_width="match_parent"
database, API, etc.)
android:layout_height="match_parent"
String[] searchSuggestions = new String[]{
android:padding="16dp">
"Android", "Apple", "Amazon",
"Artificial Intelligence", "Android Studio",
<AutoCompleteTextView
"Application Development", "AI
research", "Autonomous Vehicles", "Artificial
android:id="@+id/searchAutoCompleteTextView"
Intelligence Jobs"
android:layout_width="match_parent"
};
android:layout_height="wrap_content"
android:hint="Search here"
// Create an ArrayAdapter using the search
android:padding="10dp"
suggestions
android:textSize="16sp"
ArrayAdapter<String> adapter = new
android:dropDownHeight="200dp"/>
ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line,
<Button
searchSuggestions);
android:id="@+id/searchButton"
android:layout_width="wrap_content"
// Set the adapter to AutoCompleteTextView
android:layout_height="wrap_content"
autoCompleteTextView.setAdapter(adapter);
android:text="Search"
// Set up the search button click listener
android:layout_below="@id/searchAutoCompleteT
searchButton.setOnClickListener(new
extView"
View.OnClickListener() {
android:layout_marginTop="16dp"
@Override
android:layout_centerHorizontal="true" />
public void onClick(View v) {
// Get the text entered in the
</RelativeLayout>
AutoCompleteTextView
package com.example.myapplication10;
String query =
autoCompleteTextView.getText().toString();
import android.os.Bundle;
// Show a toast with the query (you can
import android.view.View;
replace this with actual search functionality)
import android.widget.ArrayAdapter;
Toast.makeText(MainActivity.this,
import android.widget.AutoCompleteTextView;
"Searching for: " + query,
import android.widget.Button;
Toast.LENGTH_SHORT).show();
import android.widget.Toast;
} });
}
import
}
androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends


AppCompatActivity {

private AutoCompleteTextView
autoCompleteTextView;
private Button searchButton;

@Override

You might also like