Android - how to use Spinner to hide/display View objects

12,968

There are a few small omissions in the code you posted. When I made the following changes, I was able to compile and run your code successfully.

  1. You are missing a semicolon after

    mLabel = (TextView) findViewById(R.id.field_label)
    
  2. Insert a break; statement in between your two case options.

  3. You can remove unnecessary braces around your case statements.

    case SPINNER_OPTION_FIRST:
        mLabel.setVisibility(View.GONE);
        mTextField.setVisibility(View.GONE);
        break;        
    case SPINNER_OPTION_SECOND:
        mLabel.setVisibility(View.VISIBLE);
        mTextField.setVisibility(View.VISIBLE);
    
  4. Although not necessary to get your program to run, it would be better to explicitly specify ArrayAdapter<CharSequence> when defining adapter1 to avoid type issues.

Share:
12,968
Keeb13r
Author by

Keeb13r

Updated on June 26, 2022

Comments

  • Keeb13r
    Keeb13r almost 2 years

    For an Android application, I'm trying to use a Spinner which, when an option is selected, will hide/display relevant View objects. For my application, these objects are an EditText and an associated TextView label for the field. Unfortunately, I can't seem to get the EditText to hide/display, and when I add it code to hide/display the TextView, I get a NullPointerException. I presume that since I am laying out the view objects in a RelativeLayout, by hiding one of the view objects, I am removing its relationship with other view objects, hence the NullPointer.

    Can anyone figure out why this might be happening? Here's my code:

    public class FormFields extends Activity {
        private Spinner mSpinner;
        private EditText mTextField;
        private TextView mLabel;
    
        private static final int SPINNER_OPTION_FIRST = 0;
        private static final int SPINNER_OPTION_SECOND = 1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.form_fields);
    
            mTextField = (EditText) findViewById(R.id.text_field);
            mLabel = (TextView) findViewById(R.id.field_label)
            mSpinner = (Spinner) findViewById(R.id.spinner);
    
            ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
                this, R.array.spinnerOptions, android.R.layout.simple_spinner_item);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            mSpinner.setAdapter(adapter1);
    
            mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                    switch(position) {
                        case SPINNER_OPTION_FIRST: {
                            mLabel.setVisibility(View.GONE);
                            mTextField.setVisibility(View.GONE);
                        }
                        case SPINNER_OPTION_SECOND: {
                            mLabel.setVisibility(View.VISIBLE);
                            mTextField.setVisibility(View.VISIBLE);
                        }
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    // Do nothing
                }
            });
        }
    }
    

    form_fields.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#104667">
    
            <TextView
                android:id="@+id/spinner_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dip"
                android:textStyle="bold"
                android:text="Please select an option" />
    
            <Spinner
                android:id="@+id/spinner"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/spinner_label"
                android:layout_marginLeft="25dip"
                android:layout_marginRight="25dip"
                android:drawSelectorOnTop="true"
                android:prompt="@string/spinnerPrompt" />
    
            <TextView
                android:id="@+id/field_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/spinner"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dip"
                android:textStyle="bold"
                android:text="Enter text here: "
                android:visibility="gone" />
    
            <EditText
                android:id="@+id/text_field"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dip"
                android:layout_marginRight="25dip"
                android:layout_below="@+id/field_label" 
                android:visibility="gone" />
        </RelativeLayout>
    </ScrollView>
    
  • Keeb13r
    Keeb13r over 13 years
    Thanks a bunch. Since this code was slightly adjusted from my actual application, the semicolon wasn't the actual problem (otherwise, my application wouldn't have compiled). It was actually that I didn't set my mLabel member variable at all! Go figure. I'm curious - what is the purpose of the break statement?
  • Brian
    Brian over 13 years
    Without break; statements separating each case statement, execution "falls through" to each case statement successively. There are reasons why you might want this to happen, but ordinarily a switch block is used to provide alternative options and each case should only run the code for that particular condition. For some examples and a detailed explanation of the switch statement, see: download.oracle.com/javase/tutorial/java/nutsandbolts/…