Question 10: Write a program to display all the subjects of sixth semester using Auto Complete Text View.
Download hole Program / Project code, by clicking following link:
How to create AutoCompleteTextView field in XML?
To create an AutoCompleteTextView field in XML, you can use the following steps:
- Open the XML file where you want to add the AutoCompleteTextView field.
- Add the <AutoCompleteTextView> tag to create the AutoCompleteTextView field.
- Set the android:id attribute to a unique ID for the AutoCompleteTextView field.
- Set the android:layout_width and android:layout_height attributes to specify the width and height of the field.
- Set the android:completionThreshold attribute to specify the minimum number of characters that must be typed before the AutoCompleteTextView starts suggesting completions.
- Set the android:completionHint attribute to specify a hint text that appears below the AutoCompleteTextView when it is used to display a list of suggestions (optional).
- Set the android:inputType attribute to specify the type of input expected, such as text or phone number.
- Set the android:imeOptions attribute to specify additional options for the input method editor (IME), such as the action button to display on the keyboard.
- Set the android:textColor attribute to specify the color of the text in the field.
- Set the android:textSize attribute to specify the size of the text in the field.
- Set the android:hint attribute to specify a hint text that appears in the field when it is empty.
<AutoCompleteTextViewThis will create an AutoCompleteTextView field with a unique ID, width and height set to match_parent and wrap_content respectively, a minimum completion threshold of 1 character, a completion hint of "Search for a city", input type set to text, IME option set to actionSearch, text color set to black, text size set to 16sp and a hint text of "Enter city name". You can customize these attributes based on your requirements.
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:completionHint="Search for a city"
android:inputType="text"
android:imeOptions="actionSearch"
android:textColor="#000000"
android:textSize="16sp"
android:hint="Enter city name" />
Programming Code:
Following code write in: MainActivity.java
package com.go2collage.practical_10;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextView;
private String search_engine[] = {"RDBMS","Programming with Python", "Capstone Project", "Web Application Development", "Data Structure",
"Programming with C", "Entrepreneurship Development", "Advance Java", "Object Oriented Programming with C++"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView = findViewById(R.id.autoCompleteTxt);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, search_engine);
autoCompleteTextView.setThreshold(2);
autoCompleteTextView.setAdapter(adapter);
}
}
Following code write in: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:layout_margin="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/logoTxt"
android:text="go2collage"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="50dp"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sixth Semester Subjects"
android:id="@+id/nameTxt"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginBottom="25dp"
android:layout_below="@+id/logoTxt"
android:textAlignment="center"
tools:ignore="HardcodedText" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameTxt"
android:hint="Enter Subject name"
android:textColor="@color/black"
android:textColorHint="#78909C"
android:textSize="20sp"
tools:ignore="HardcodedText" />
</RelativeLayout>
Output:
0 Comments