Is there anything which will reduce search time?
Its AutoCompleteTextView. An AutoCompleteTextView is an advanced EditText, where if the user types a letter, all the words starting with that letter pops up automatically.
So, lets see how we gonna make it ...
First, create an xml file in your layout folder, name it single_textview. Then copy the following and paste it in this xml file. Please don't add anything else than the below content. We have a separate xml for that.
Its AutoCompleteTextView. An AutoCompleteTextView is an advanced EditText, where if the user types a letter, all the words starting with that letter pops up automatically.
So, lets see how we gonna make it ...
First, create an xml file in your layout folder, name it single_textview. Then copy the following and paste it in this xml file. Please don't add anything else than the below content. We have a separate xml for that.
-------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>-------------------------------------------------------------------------
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
android:textColor="#000000"
android:background="#ffffff"/>
The above mentioned xml file will only contain a TextView, we now have to call it through our AutoCompleteTextView.
-----------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>-----------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f05442"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/my_fav"
android:layout_marginTop="30dp"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ffffff"
android:padding="5sp"
android:textSize="14sp" />
</LinearLayout>
Moving to our Java code...
package com.app.yourappname;-----------------------------------------------------------
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
public class AutoCompleteTextViewActivity extends AppCompatActivity {
private String items[] = {"Android", "Animation", "Angry Birds", "Apple", "Bread", "Basket", "Bun", "Cake",
"Donut", "Diary","Elephant", "English"};
private AutoCompleteTextView act;
@Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.autocomplete_textview_activity);
act = findViewById(R.id.my_fav);
ArrayAdapter<String> My_arr_adapter= new ArrayAdapter(getApplicationContext(),R.layout.single_textview, items);
act.setThreshold(1);
act.setAdapter(My_arr_adapter);
act.setOnItemClickListener(new AdapterView.OnItemClickListener()
{public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {}});
}
}
In the Manifest Xml, declare the Activity name:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".AutoCompleteTextViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
As you can see above, if you type any letter starting from A to E, you will get at least one word suggestion, but when you typed kite (see the last screen shot) there are no suggestions because we did not any word which starts with letter 'k'.
No comments:
Post a Comment