Android | Auto Complete TextBox and How to Create it | Mysql | database | Json API

Jyotishgher Astrology
By -
0

 

Android | Auto Complete TextBox and How to Create it



Auto Complete TextView provides the following methods: 

  1. setThreshold() method to start the suggestion words in first character. 
  2. setAdapter() method to set all the data in AutoCompleteTextView. 
  3. setTextColor this is used for the text with the black color.  

How to create an Android App to use AutoComplete TextViews

This example will help to develop an Android App that creates AutoComplete TextViews according to the example shown above:
Some colours are stored in String array. Now words are shown in the list so we use ArrayAdapter Class
ArrayAdapter Class is used for holding the data of Color String array as a list item as shown below and then Set all the above methods in AutoCompleteTextView. 



LET JSON WE CREATED :


{"result":[{"COACH_TYPE":"RA"},{"COACH_TYPE":"LWACCW"}]}


ANDROID CODE:


public class input_form_by_form_no extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextViewcoach_type;
ArrayList<String> arr;
String APP_DRG_NO,APP_WI_NO,MOD_CARRIED_OUT,TRIAL_ITEMS_FITTED,REMARKS,coach_type,shell_no,prod_yr;
private JSONArray result;
public static final String JSON_ARRAY = "result";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input_form_by_form_no);

// By ID get the AutoCompleteTextView
// which id is assign in xml file
arr=new ArrayList<>();
autoCompleteTextViewcoach_type = (AutoCompleteTextView) findViewById(R.id.coach_type);
coachTypePopup();

}

private void coachTypePopup() {

final ProgressDialog loading = ProgressDialog.show(input_form_by_form_no.this, "Checking Coaches", "Please wait...", false, true);
// Toast.makeText(this, EMP_NO, Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, coach_type_lov,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

// Toast.makeText(hospital.this, response, Toast.LENGTH_SHORT).show();
Log.d("forms_lists:", "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(input_form_by_form_no.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();

}
})/* {
@Override
protected Map<String, String> getParams() {

Log.d("PARAMETERS-", "getParams: " + ORACLEUSER);
//PASSING STRING VALUE TO THE TAGGING PARAMETER WITH TENDS TO GO FOR TBALE NAME
Map<String, String> params = new HashMap<String, String>();

params.put("USERNAME", ORACLEUSER);//ORACLEUSER
return params;
}

}*/;

RequestQueue requestQueue = Volley.newRequestQueue(input_form_by_form_no.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 COACH_TYPE = json.getString("COACH_TYPE");
arr.add(COACH_TYPE);
// GetDataAdapter2.setFORMS(json.getString(JSON_FORMS));



} catch (JSONException e) {

e.printStackTrace();
}
ArrayAdapter<String> adapter= new ArrayAdapter<>
(this, android.R.layout.select_dialog_item, arr);
autoCompleteTextViewcoach_type.setThreshold(1);//will start working from first character
autoCompleteTextViewcoach_type.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
autoCompleteTextViewcoach_type.setTextColor(Color.RED);
}


}

}

OUTPUT HERE LIKE :



autoCompleteTextViewcoach_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
String str = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
});

Place this So that when item is selected The value will be pop up:

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 COACH_TYPE = json.getString("COACH_TYPE");
arr.add(COACH_TYPE);
// GetDataAdapter2.setFORMS(json.getString(JSON_FORMS));


} catch (JSONException e) {

e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<>
(
this, android.R.layout.select_dialog_item, arr);
autoCompleteTextViewcoach_type.setThreshold(1);//will start working from first character
autoCompleteTextViewcoach_type.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
autoCompleteTextViewcoach_type.setTextColor(Color.RED);

autoCompleteTextViewcoach_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
String str = (String) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}
});

}


}

Post a Comment

0Comments

Post a Comment (0)