MultiAutoCompleteTextView in Android with Mysql Database API
An editable text view, extending AutoCompleteTextView
, that can show completion suggestions for the substring of the text where the user is typing instead of necessarily for the entire thing.
You must provide a Tokenizer
to distinguish the various substrings.
The following code snippet shows how to create a text view which suggests various countries names while the user is typing:
An AutoCompleteTextView only offers suggestion about the whole text. But MultiAutoCompleteTextView offers multiple suggestions for the substring of the text.
Step 1: Create a New Project
Step 2: Working with the activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_gravity="center"
android:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- 1st MultiAutoCompleteTextView instance identified with
multiAutoCompleteTextViewDefault and here when user starts
to type, it will show relevant substrings and after user chooses it,
the text is ended with "," as comma is the default tokenizer -->
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextViewDefault"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Enter Search Terms here" />
<!-- 2nd MultiAutoCompleteTextView instance identified with
multiAutoCompleteTextViewCustom and here when user starts
to type, it will show relevant substrings and after user
chooses it, the text is ended with " " as code is done to
have space as separator -->
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextViewCustom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Add your necessary tags here" />
</LinearLayout>
package nanoakhi.rcfinternal.PL;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nanoakhi.rcfinternal.GetDataAdapter;
import nanoakhi.rcfinternal.R;
import nanoakhi.rcfinternal.retiree.retiree;
public class PLoooo extends AppCompatActivity {
// Defining two MultiAutoCompleteTextView
// This is to recognize comma separated.
MultiAutoCompleteTextView multiAutoCompleteTextViewDefault;
AutoCompleteTextView autoCompleteTextViewcoach_type;
ArrayList<String> arr;
private JSONArray result, resultPO;
public static final String JSON_ARRAY = "result";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pl_layout);
multiAutoCompleteTextViewDefault = findViewById(R.id.multiAutoCompleteTextViewDefault);
// In order to show the substring options in a dropdown, we need ArrayAdapter
// and here it is using simple_list_item_1
// By ID get the AutoCompleteTextView
// which id is assign in xml file
arr = new ArrayList<>();
autoCompleteTextViewcoach_type = (AutoCompleteTextView) findViewById(R.id.desc1);
coachTypePopup();
}
private void coachTypePopup() {
final ProgressDialog loading = ProgressDialog.show(PL.this, "Processing", "Please wait...", false, true);
// Toast.makeText(this, EMP_NO, Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, NumberQuery,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Toast.makeText(hospital.this, response, Toast.LENGTH_SHORT).show();
Log.d("SHORT_DESC:", "onResponse: " + response);
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(JSON_ARRAY);
if (result.length() > 0) {
loading.dismiss();
JSON_PARSE_DATA_AFTER_WEBCALL(result);
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(PL.this);
// Setting Dialog Title
alertDialog.setTitle("No record Found");
// Setting Dialog Message
alertDialog.setMessage("Try Again!!");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.certificate);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Got!!!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
// Showing Alert Message
alertDialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
// stopping swipe refresh
// Toast.makeText(vendor_name.this, "Error", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(PL.this);
requestQueue.add(stringRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
String SHORT_DESC = json.getString("SHORT_DESC");
arr.add(SHORT_DESC);
// GetDataAdapter2.setFORMS(json.getString(JSON_FORMS));
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<>
(this, android.R.layout.simple_dropdown_item_1line, arr);
multiAutoCompleteTextViewDefault.setAdapter(adapter);
multiAutoCompleteTextViewDefault.setThreshold(2);
multiAutoCompleteTextViewDefault.setTokenizer(new SpaceTokenizer());
multiAutoCompleteTextViewDefault.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
multiAutoCompleteTextViewDefault.setText("");
String str = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
Log.d("PLNO-", "onItemClick: " + str.substring(0, 8));
multiAutoCompleteTextViewDefault.setText(str);
searchPLDETAILS(str.substring(0, 8));
}
});
}
}
}
Post a Comment
0Comments