0% found this document useful (0 votes)
38 views6 pages

Autocomplete

The document describes an Android autocomplete text view example. It includes XML layout files to display an autocomplete text view and static text, strings for text values, a Java class to populate the autocomplete text view with an array of strings and set a threshold of 2 characters for autocomplete suggestions, and an Android manifest file. The autocomplete text view is populated with a list of place names for autocomplete as the user types.

Uploaded by

Varad Gorhe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
38 views6 pages

Autocomplete

The document describes an Android autocomplete text view example. It includes XML layout files to display an autocomplete text view and static text, strings for text values, a Java class to populate the autocomplete text view with an array of strings and set a threshold of 2 characters for autocomplete suggestions, and an Android manifest file. The autocomplete text view is populated with a list of place names for autocomplete as the user types.

Uploaded by

Varad Gorhe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Autocomplete

Code :-
activity_main.xml
<?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"
tools:context=".MainActivity">

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_showing_autocomplete_text_view"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
tools:ignore="VisualLintOverlap" />

<AutoCompleteTextView
android:id="@+id/ct1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview2"
android:layout_alignStart="@+id/textview2"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="@string/app_name"
android:minHeight="48dp"
tools:ignore="LabelFor,DuplicateSpeakableTextCheck" />

</RelativeLayout>

strings.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<string name="app_name">autocomplete</string>
<string name="example_showing_autocomplete_text_view">Example Showing
AutoComplete Text View</string>
</resources>
MainActivity.java
package com.example.autocomplete;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autocomplete;
String[] arr={"Paris,France","India","pune","Mumbai","Thane","PA,United
States","Brazil,Parana","Pandua,Itely"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autocomplete= (AutoCompleteTextView)
findViewById(R.id.ct1);
ArrayAdapter<String> adapter= new ArrayAdapter<>(this,
android.R.layout.select_dialog_item, arr);
autocomplete.setThreshold(2);
autocomplete.setAdapter(adapter);
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.autocomplete">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@drawable/ic_launcher_background"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Autocomplete"
tools:targetApi="31">
<activity
android:name="com.example.autocomplete.MainActivity"
android:exported="true"
android:label="autocomplete">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>
</manifest>
OutPut :-

You might also like