• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Spinner and EditText Validation help

S12

Newbie
I have a spinner that has 3 different stores as its items and a edittext that takes a link that the user insert and I want to validate if the link matches the store the user selected, if so the button they click will do something if not they get an error message.
 
What have you tried so far?
Please post your code.
I haven't tried anything yet so far because i'm still figuring out how to go about it. Here's my code so far

MainActivity
Code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {
    private Spinner chooseSize, chooseStore;
    private EditText linkEditText;
    private Button addButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initiate editText and get the inputed text
        linkEditText = (EditText) findViewById(R.id.editText);
        String linkText = linkEditText.getText().toString();

        addButton =(Button) findViewById(R.id.Addbutton);

        //initiate size spinner and set array
        chooseSize =(Spinner) findViewById(R.id.size_spinner);
        chooseSize.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.size_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chooseSize.setAdapter(adapter);

        //initiate store spinner and set array
        chooseStore =(Spinner) findViewById(R.id.store_spinner);
        chooseStore.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        ArrayAdapter<CharSequence> storeAdapter = ArrayAdapter.createFromResource(this,
                R.array.stores_array, android.R.layout.simple_spinner_item);
        storeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chooseStore.setAdapter(storeAdapter);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}

CustomOnItemSelectedListener
Code:
import android.view.View;
import android.widget.AdapterView;

public class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // An item was selected. You can retrieve the selected item using
        //parent.getItemAtPosition(position);
        switch (parent.getId()) {
            case R.id.store_spinner:
                // do stuff1
                break;
            case R.id.size_spinner:
                // do stuff
                break;
            default:
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}
 
It's a good start, you have all the components you need.
When you say "validate if the link matches the store the user selected", is this what you're unsure about?
Instead of allowing the user to enter a URL (link), why not have a list of stores, with associated URLs? When you pick a store name from the spinner, it automatically populates the EditText with the link.
 
It's a good start, you have all the components you need.
When you say "validate if the link matches the store the user selected", is this what you're unsure about?
Instead of allowing the user to enter a URL (link), why not have a list of stores, with associated URLs? When you pick a store name from the spinner, it automatically populates the EditText with the link.

It's because the url will be different all the time. For example the user may enter nike.com/yzx13 which will bring them to a specfic page. I am having trouble to validate if the user inputed that url and choosed nike from the spinner to make sure its a nike link. Maybe i could match the first couple charachters to a define nike link to make sure its a right link.
 
Well you could try to match the store name as a substring of the URL. But it's not guaranteed to work 100%.
 
Code:
String storeName;
String siteUrl;

if (siteUrl.contains(storeName)) {
  // It's Ok!
}

Very simplistic validation, as the URL isn't guaranteed to contain the store name.
 
where would be best do this for each store on the spinner onitemselected or the button onclicklistner.
 
Well OnItemSelected would be the place to do it, wouldn't it? i.e. when the user has selected the store.

Actually, no. On second thought, your button click listener would be better, assuming the user has entered a URL into the EditText
 
Well OnItemSelected would be the place to do it, wouldn't it? i.e. when the user has selected the store.

Actually, no. On second thought, your button click listener would be better, assuming the user has entered a URL into the EditText

Ok so i should create a switch with those conditions for each store on the button onClickListner right.
 
Put the URL checking code in the onClick() method

Code:
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG = "test1" ;
    private Spinner chooseSize, chooseStore;
    private EditText linkEditText;
    private Button addButton;
    private String storeName = "somestore";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initiate editText and get the inputed text
        linkEditText = (EditText) findViewById(R.id.editText);
        final String siteUrl = linkEditText.getText().toString();

        addButton =(Button) findViewById(R.id.Addbutton);

        //initiate size spinner and set array
        chooseSize =(Spinner) findViewById(R.id.size_spinner);
        chooseSize.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.size_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chooseSize.setAdapter(adapter);

        //initiate store spinner and set array
        chooseStore =(Spinner) findViewById(R.id.store_spinner);
        chooseStore.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        ArrayAdapter<CharSequence> storeAdapter = ArrayAdapter.createFromResource(this,
                R.array.stores_array, android.R.layout.simple_spinner_item);
        storeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chooseStore.setAdapter(storeAdapter);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (siteUrl.contains(storeName)) {
                    // It's Ok!
                    Log.d(LOG_TAG,"work");
                }
            }
        });
    }
}

It doesn't work maybe a spinner is not the best choice. I want the user to input a url if it matches the stores url not the name then they can do something with the button or else error or change to choose the correct store. I dont know if theres a way I can show the store name to the user but associate a url to it in the backend to do the validation with the edittext.
 
Code:
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String LOG_TAG = "test1" ;
    private Spinner chooseSize, chooseStore;
    private EditText linkEditText;
    private Button addButton;
    private String storeName = "somestore";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //initiate editText and get the inputed text
        linkEditText = (EditText) findViewById(R.id.editText);
        final String siteUrl = linkEditText.getText().toString();

        addButton =(Button) findViewById(R.id.Addbutton);

        //initiate size spinner and set array
        chooseSize =(Spinner) findViewById(R.id.size_spinner);
        chooseSize.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.size_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chooseSize.setAdapter(adapter);

        //initiate store spinner and set array
        chooseStore =(Spinner) findViewById(R.id.store_spinner);
        chooseStore.setOnItemSelectedListener(new CustomOnItemSelectedListener());
        ArrayAdapter<CharSequence> storeAdapter = ArrayAdapter.createFromResource(this,
                R.array.stores_array, android.R.layout.simple_spinner_item);
        storeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chooseStore.setAdapter(storeAdapter);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (siteUrl.contains(storeName)) {
                    // It's Ok!
                    Log.d(LOG_TAG,"work");
                }
            }
        });
    }
}

It doesn't work maybe a spinner is not the best choice. I want the user to input a url if it matches the stores url not the name then they can do something with the button or else error or change to choose the correct store. I dont know if theres a way I can show the store names to the user but associate a url to it in the backend to do the validation with the edittext.
 
What do you mean it doesn't work?

What's the value of siteUrl, and what's the value of storeName?

If storeName is a substring of siteUrl, your log statement will be printed. Simple as that.

If you want to do case insenstivity, then convert both Strings to lower case before calling contains()
 
private String storeName = "somestore";
final String siteUrl = linkEditText.getText().toString().toLowerCase();

I don't see my log statement when I click the button.
 
Last edited:
Oh come on man, think for yourself a little.

This line of code

Code:
final String siteUrl = linkEditText.getText().toString().toLowerCase();

Is in your onCreate() method, which gets executed when your Activity starts. At that point, you've typed nothing into your EditText. So siteUrl will have a value of "".

What do you think you need to do to make this work properly?
 
Oh come on man, think for yourself a little.

This line of code

Code:
final String siteUrl = linkEditText.getText().toString().toLowerCase();

Is in your onCreate() method, which gets executed when your Activity starts. At that point, you've typed nothing into your EditText. So siteUrl will have a value of "".

What do you think you need to do to make this work properly?
I fixed code and its working now. Is it possible to set an OnClick for each spinner item.
 
I have String variable that gets its text from an edittext in the main activity. How would pass that variable to another class.
 
By calling a method on that class which takes a String parameter.
 
By calling a method on that class which takes a String parameter.

Ok I got the spinner and editText to work how I want it to be. But I'm having trouble calling the onItemSelected spinner in the Onclick of the button.
 
Um... You don't call the onItemSelected() method of your Spinner listener class. That is called by the system automatically, when an item of your Spinner is selected.
 
Um... You don't call the onItemSelected() method of your Spinner listener class. That is called by the system automatically, when an item of your Spinner is selected.

I don't know these things as I am learning java and Android development as I go Plus im just 16 so google helps alot. What method from the Spinner listener class I can call in the button on click to give me the same result or is there a better way to do it.
 
This might be a good point to re-post your current code, so we can see where you are, and clarify what the problems are.

As I understand it, what you're trying to do is:

1. Select a store from a list presented in a Spinner
2. Enter a URL into a TextEdit box
3. Click a button: This should validate the entered URL against the selected store

Is this correct?
If so, let's see your current code, because I think you may have a few misunderstandings.
 
Back
Top Bottom