I am trying to set an error message on spinner when items are not selected. Also I would love focusing to spinner when the error occurs. I tried some codes for it but it doesn't work well. Especially, the code for focusing on spinner.
You can see my codes below.
public boolean controlCheck(){
int selectedItemOfMySpinner=spinner.getSelectedItemPosition();
// String actualPositionOfMySpinner= (String) spinner.getItemAtPosition(selectedItemOfMySpinner);
if(selectedItemOfMySpinner<1){
Toast.makeText(context, "LOKANATA:)", Toast.LENGTH_SHORT).show();
TextView errorText= (TextView) spinner.getSelectedView();
errorText.setError("");
errorText.setTextColor(Color.RED);
errorText.setText("my actual error text");
return false;
}
if(TextUtils.isEmpty(btnDate.getText().toString())){
btnDate.setError("Bu alan boş olamaz");
btnDate.requestFocus();
return false;
}
if(TextUtils.isEmpty(btnTime.getText().toString())){
btnTime.setError("Bu alan boş olamaz");
btnTime.requestFocus();
return false;
}
if(TextUtils.isEmpty(kisisayisi.getText().toString())){
kisisayisi.setError("Bu alan boş olamaz");
kisisayisi.requestFocus();
return false;
}
if(TextUtils.isEmpty(edtTel.getText().toString())){
edtTel.setError("Bu alan boş olamaz");
edtTel.requestFocus();
return false;
}
if(edtTel.getText().toString().length()<18)
{
edtTel.setError("telefon numarasını eksiksiz giriniz");
edtTel.requestFocus();
return false;
}
if(TextUtils.isEmpty(namesurname.getText().toString())){
namesurname.setError("Bu alan boş olamaz");
namesurname.requestFocus();
return false;
}
return true;
}
Any suggestion?
Use this
Spinner in layout
<Spinner
android:id="@+id/mySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
/>
Adding adapter to spinner in activity
final Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
ArrayList<String> list = new ArrayList<>();
list.add("Select");
list.add("Enamul");
list.add("Tonu");
ArrayAdapter spinnerAdapter = new ArrayAdapter(getApplicationContext(),
R.layout.cutom_spinner_layout, list);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(spinnerAdapter);
cutom_spinner_layout.xml that use layout resource in adapter
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:gravity="left|center_vertical|center_horizontal"
android:textColor="#000"
android:padding="2dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:fadingEdge="horizontal"
android:scrollHorizontally="true"
android:selectAllOnFocus="true"
android:layout_gravity="center_horizontal"
/>
Add error
if(mySpinner.getSelectedItemPosition()<1){
TextView errorText= (TextView) mySpinner.getSelectedView();
errorText.setError("");
errorText.setTextColor(Color.RED);
errorText.setText("Please select one option");
}
Output when error
Add these codes inside the textview in xml.
<TextView
...
android:focusable="true"
android:focusableInTouchMode="true"
/>
Add this line below set text where you are showing error for spinner
errorText.requestFocus();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.