I want to make my app an auto search .
This is my app.
My App:
And I want my app to make like this
I want to make like this:
But how ? Can anyone help me ?
This is my codes
MainActivity.class
package com.skholingua.android.searchview;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener {
String[] stateList;
String[] anotherStringArray;
private SearchView searchView;
private ListView listView;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView = (SearchView) findViewById(R.id.searchView1);
listView = (ListView) findViewById(R.id.listView1);
stateList = getResources().getStringArray(R.array.stateList);
anotherStringArray = getResources().getStringArray(R.array.anotherStringArray);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stateList);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
// Sets the default or resting state of the search field.
// If true, a single search icon is shown by default and
// expands to show the text field and other buttons when pressed
//searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
// Sets the hint text to display in the query text field
//searchView.setQueryHint("State Name");
int searchPlateId = searchView.getContext().getResources()
.getIdentifier("android:id/search_plate", null, null);
View searchPlateView = searchView.findViewById(searchPlateId);
if (searchPlateView != null) {
searchPlateView.setBackgroundColor(Color.TRANSPARENT);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String stateName = listView.getAdapter().getItem(position).toString();
int pos=0;
for(int i=0;i<stateList.length;i++)
{
if(stateList[i].equals(stateName))
{
pos=i;
break;
}
}
// Put selected state to intent.
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("selectedState", stateName);
intent.putExtra("anotherStringArray", anotherStringArray[pos]);
startActivity(intent);
}
});
}
@Override
public boolean onQueryTextChange(String newText) {
android.widget.Filter filter = adapter.getFilter();
if (TextUtils.isEmpty(newText)) {
filter.filter("");
} else {
filter.filter(newText);
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:focusableInTouchMode="true"
tools:context="com.skholingua.android.searchview.MainActivity" >
<SearchView
android:id="@+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/search_view_border"
android:iconifiedByDefault="false"
android:padding="2dp"
android:queryHint="Search...."
android:layout_alignParentTop="true" >
</SearchView>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchView1"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
Try this code
public class MainActivity extends Activity {
AutoCompleteTextView text;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
ArrayAdapter adapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);
} }
and use 'AutoCompleteTextView' in place text view or edit text and for more help refer below mentioned link https://www.tutorialspoint.com/android/android_auto_complete.htm