0% found this document useful (0 votes)
4 views3 pages

Practical 8.1

The document contains an XML layout file and a Java activity class for an Android application. The layout includes a title, an AutoCompleteTextView for search suggestions, and a search button. The Java class initializes the views, sets up search suggestions, and handles button clicks to display a toast message with the search query or prompt for input if empty.

Uploaded by

Pruthesh Upadhye
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)
4 views3 pages

Practical 8.1

The document contains an XML layout file and a Java activity class for an Android application. The layout includes a title, an AutoCompleteTextView for search suggestions, and a search button. The Java class initializes the views, sets up search suggestions, and handles button clicks to display a toast message with the search query or prompt for input if empty.

Uploaded by

Pruthesh Upadhye
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/ 3

8.1 activity_main.

xml

<?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="20dp">

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search Engine"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center"
android:paddingBottom="20dp"/>

<AutoCompleteTextView
android:id="@+id/autoCompleteSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search here..."
android:completionThreshold="1"
android:padding="10dp"/>

<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:layout_marginTop="10dp"/>
</LinearLayout>
8.1 MainActivity.java

package com.example.practical7;

import android.os.Bundle;
import android.view.View;
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 autoCompleteSearch;


private Button btnSearch;

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

// Initialize Views
autoCompleteSearch = findViewById(R.id.autoCompleteSearch);
btnSearch = findViewById(R.id.btnSearch);

// Sample search suggestions


String[] searchSuggestions = {
"Google", "Bing", "Yahoo", "DuckDuckGo", "Baidu",
"Ask.com", "Yandex", "Ecosia", "AOL Search", "StartPage"
};

// Set up AutoCompleteTextView with suggestions


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

// Button Click Listener


btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String searchQuery =
autoCompleteSearch.getText().toString().trim();
if (!searchQuery.isEmpty()) {
Toast.makeText(MainActivity.this, "Searching for: " +
searchQuery, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please enter a search
term!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
8.1 Output

You might also like