You are on page 1of 6

MAD CO6I Practical 8

Name: Pranjal Shahane Roll No: 06


Practical: 8

Q. Write a program to create a first display screen of any search


engine using Auto Complete Text View.
XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter search query"
android:completionThreshold="1" />

<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/autoCompleteTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Search" />

</RelativeLayout>

Java code:
package com.example.myapplication;

Gramin Technical & managemnet campus 1


MAD CO6I Practical 8

import android.os.Bundle;
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 static final String[] SEARCH_SUGGESTIONS = {


"Android Development",
"Machine Learning",
"Web Development",
"Java Programming",
"Python Programming",
"Data Science",
"Artificial Intelligence",
"Mobile App Development",
"UI/UX Design",
"Computer Networks"
};

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

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

// Create adapter for AutoCompleteTextView


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

// Set adapter to AutoCompleteTextView


autoCompleteTextView.setAdapter(adapter);

// Set threshold for suggesting options


autoCompleteTextView.setThreshold(1);

// Initialize search button


Button buttonSearch = findViewById(R.id.buttonSearch);

Gramin Technical & managemnet campus 2


MAD CO6I Practical 8

// Set click listener for search button


buttonSearch.setOnClickListener(v -> {
String query = autoCompleteTextView.getText().toString();
if (!query.isEmpty()) {
// Perform search operation with the entered query
performSearch(query);
} else {
// Display a message if the search query is empty
Toast.makeText(MainActivity.this, "Please enter a search query",
Toast.LENGTH_SHORT).show();
}
});
}

private void performSearch(String query) {


// Placeholder method to perform search operation
// In a real application, you would implement your search logic here
Toast.makeText(this, "Performing search for: " + query, Toast.LENGTH_SHORT).show();
}
}

Gramin Technical & managemnet campus 3


MAD CO6I Practical 8

Q. Write a program to display all the subjects of sixth semester


using Auto Complete Text View.
XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter subject"
android:completionThreshold="1" />

</RelativeLayout>

Java code:
package com.example.subjectsapp;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

Gramin Technical & managemnet campus 4


MAD CO6I Practical 8

private static final String[] SIXTH_SEMESTER_SUBJECTS = {


"Advanced Java Programming",
"Software Engineering",
"Database Management Systems",
"Operating Systems",
"Computer Networks",
"Web Development",
"Mobile App Development",
"Project Management"
};

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

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

// Create adapter for AutoCompleteTextView


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

// Set adapter to AutoCompleteTextView


autoCompleteTextView.setAdapter(adapter);

// Set threshold for suggesting options

Gramin Technical & managemnet campus 5


MAD CO6I Practical 8

autoCompleteTextView.setThreshold(1);
}
}

OUTPUT:

Gramin Technical & managemnet campus 6

You might also like