• 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

I use toast as a way to verify if my code is working.

Code:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private Spinner chooseSize, chooseStore;
    private EditText linkUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        linkUrl = (EditText) findViewById(R.id.editText);
        //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(this);
        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);
    }

    public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
        String item = parent.getItemAtPosition(pos).toString();
        String siteUrl = linkUrl.getText().toString().toLowerCase();
        if(item.equals("Nike"))
        {
            // do your stuff
            String storeName = "http://gonike.me";
            if (siteUrl.contains(storeName)) {
                // It's Ok!
                Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
            }
            else {
                linkUrl.setError("Link doesn't match store");
            }
        } else if (item.equals("Addidas")){
            String storeName = "http://a.did.as/";
            if (siteUrl.contains(storeName)) {
                // It's Ok!
                Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
            }
            else {
                linkUrl.setError("Link doesn't match store");
            }
        }else if(item.equals("Footlocker")){
            String storeName = "http://spr.ly/";
            if (siteUrl.contains(storeName)) {
                // It's Ok!
                Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
            }
            else {
                linkUrl.setError("Link doesn't match store");
            }
        }
        else if(item.equals("Choose a store")){
            String storeName = "Default";
            if (siteUrl.contains(storeName)) {
                // It's Ok!
                Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
            }
        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }

    public void OnClickButton(View view) {
        if(linkUrl.getError() != null){
            Toast.makeText(getApplicationContext(), "Fix Error", Toast.LENGTH_LONG).show();
        }else if(linkUrl.getText().length() == 0) {
            linkUrl.setError("Enter Link");
        }
        else{
            //action to perform on button click
            String siteUrl = linkUrl.getText().toString();
            Toast.makeText(getApplicationContext(), "Btn clicked ", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(".WebViewActivity");
            intent.putExtra("text_label", siteUrl);
            startActivity(intent);
        }
    }
}
 
Last edited:
Upvote 0
Ok, don't use Toast to debug your app.
First thing you should do is learn to run your app in debug mode, and set breakpoints in the code to examine variable values, and trace execution. This is much better than peppering your code with Log or Toast statements.

Your validation code is in the onItemSelected, but I thought you were going to put this in the onclickButton() method - that's where it belongs.

So the code for onClickButton() should be something like this

- Get the currently selected item from the Spinner (show error if none selected)
- Get the value of the linkUrl EditText (show error if nothing entered)
- Do the validation on the above

Have a go at that. If you have difficulties, post your onClickButton() code.
 
Upvote 0
Ok, don't use Toast to debug your app.
First thing you should do is learn to run your app in debug mode, and set breakpoints in the code to examine variable values, and trace execution. This is much better than peppering your code with Log or Toast statements.

Your validation code is in the onItemSelected, but I thought you were going to put this in the onclickButton() method - that's where it belongs.

So the code for onClickButton() should be something like this

- Get the currently selected item from the Spinner (show error if none selected)
- Get the value of the linkUrl EditText (show error if nothing entered)
- Do the validation on the above

Have a go at that. If you have difficulties, post your onClickButton() code.

OK thanks for your help I fixed it. Now I'm trying to figure out how to set a background image for the app but what i see if you do it this way It could cause some devices to go out of memory.

Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootRL"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background">
</RelativeLayout>
 
Upvote 0
How do i used the same onItemSelected listener for more than one spinner. Right now i only have the onItemSelected listener on one spinner which get the text and turns it to a string. I want it to also turn some numbers from a different spinner to an int. This is all in my mainActivity.

Code:
public void onItemSelected(AdapterView<?> parent, View view,
                               int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
        item = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
 
Upvote 0
I wouldn't recommend using the same listener for two spinners, because your code will need lots of conditional logic to do different things, depending on which spinner is involved.
But if you really wish to use the same listener for both spinners, then I don't think there's anything stopping you from doing this -

Code:
Spinner spinner1;
Spinner spinner2;
MyListener listener = new MyListener();

spinner1.setOnItemSelectedListener(listener);
spinner2.setOnItemSelectedListener(listener);
 
Upvote 0
how to add a layout as a menu item in the appBar. The layout have a textView in the layout that will be updating as its the amount of tries remaining.

Code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_favorite"
        android:icon="@null"
        android:title="@string/action_tries"
        app:showAsAction="ifRoom"/>
 
</menu>

Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/Roundedbackground">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="38sp"
        android:text="5"
        android:id="@+id/textView3"
        android:layout_marginLeft="26dp"
        android:layout_marginStart="26dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/shoeSmaller"
        android:id="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="36dp"
        android:layout_marginStart="36dp" />
</RelativeLayout>

http://prntscr.com/bis734
 
Last edited:
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