0

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?

Md. Enamul Haque
  • 926
  • 8
  • 14
Aylin Sivari
  • 29
  • 1
  • 4
  • 2
    does this answer your question? https://stackoverflow.com/questions/3749971/creating-a-seterror-for-the-spinner – Masoom Badi Jan 16 '20 at 07:28
  • also do check.. https://stackoverflow.com/a/28582158/8809599 – Masoom Badi Jan 16 '20 at 07:30
  • Check out this answer [here](https://stackoverflow.com/questions/28235689/how-can-an-error-message-be-set-for-the-spinner-in-android) . Hope it helps. – Brian O Jan 16 '20 at 08:02

2 Answers2

0

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

enter image description here

Md. Enamul Haque
  • 926
  • 8
  • 14
-1

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();
Jarin Rocks
  • 975
  • 10
  • 17
  • you should accept an answer only if that answer solve your issue... other wise no one will be interested to answer after accepting a answer... – Md. Enamul Haque Jan 16 '20 at 08:06