• 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
Dec 24, 2015
39
1
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
    }
}
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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()
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones