1

I am having problems implementing a spinner into my code. Not quite sure how it works. But here is a brief idea of what I want to do. (Ex: click on the spinner and you have 2 options "page 2" and "page 3". So if you click on "page 2" it will go to page two with new content. Is there a way to accomplish that??

I also have image buttons to go to the next page and back. Just want to implement a spinner box so that you can navigate to different pages quicker.

public class AppActivity extends Activity implements OnTouchListener  {

private MediaPlayer mp;
ImageButton button;
ImageButton button3;
ImageView imgView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    final String[] spstr = getResources().getStringArray(R.array.spinnervalue);
    final Spinner sp = (Spinner)findViewById(R.id.spinner1);
    final ArrayAdapter<String> ar = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spstr);
    sp.setAdapter(ar);

    sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            String s=((TextView)view).getText().toString();
            if(s.equals("page2"))
                startActivity(new Intent(view.getContext(),App2Activity.class));
            if(s.equals("page3"))
                startActivity(new Intent(view.getContext(),App3Activity.class));

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }


    });


}



public void addListenerOnButton() {



    final Context context = this;
    button3  = (ImageButton) this.findViewById(R.id.imageButton5);
    button3.setOnTouchListener(this);


    mp = MediaPlayer.create(this, R.raw.vanilla_twilight);




    button = (ImageButton) findViewById(R.id.imageButton1);
    button.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, App2Activity.class);
                        startActivity(intent);   

        }

    });

}

@Override
 public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        MediaPlayer mp = MediaPlayer.create(getBaseContext(),
                R.raw.vanilla_twilight);
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    };
    return true;
}

}

XML

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

Logcat errors

05-22 01:32:40.058: E/SpannableStringBuilder(16206): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 05-22 01:32:40.058: E/SpannableStringBuilder(16206): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

resource page2 page3

Dan
  • 63
  • 1
  • 2
  • 8
  • Set up 3 Activities. e.g., AppActivity1, AppActivity2, AppActivity3. AppActivity1 lets you choose AppActivity2 or 3, AppActivity2 lets you chose 1 or 3, etc. – mrres1 May 22 '13 at 02:06
  • I have three activities app1 app2 and app3 set up. I can navigate between those three pages easily with buttons but not sure how i would go about implementing them with the spinners. – Dan May 22 '13 at 02:31
  • `startActivity(new Intent(context, AppActivity2.class));` – mrres1 May 22 '13 at 03:25

3 Answers3

1

Use startActivity to open the other Activities, it requires a basic Intent

sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        String s=((TextView)arg1).getText().toString();

        if(s.equals("page2"))
            startActivity(new Intent(view.getContext(), AppActivity2.class));

        if(s.equals("page3"))
            startActivity(new Intent(view.getContext(), AppActivity3.class));
    }
});

You're array in resources may not be set up correctly.

It should look similar to this:

<string-array name="spinnervalue">
    <item>page2</item>
    <item>page3</item>
</string-array>

mrres1
  • 1,147
  • 6
  • 10
  • I updated my code. I decided to include my entire code because i tried what you posted and the spinner is still not showing up. So i figure it might be my positioning of codes?? I also included the logcat errors. – Dan May 22 '13 at 08:38
  • Can you post `R.array.spinnervalue`? By the stack trace it seems like that array is empty. Set a break-point at `final String[] spstr ...`, step forward one line and then see is `spstr` has any values or if it's null. – mrres1 May 23 '13 at 01:33
  • Or, maybe `String s` is not being assigned anything. Although, I presume you would get a NullPointerException for that. You can try `String s = getResources().getStringArray(R.array.spinnervalue)[position]` – mrres1 May 23 '13 at 01:42
  • i feel like my array might be empty how do i check that with break? – Dan May 23 '13 at 19:02
  • I presume you are using Eclipse, or at least some type of IDE, so just use the debugging tool. – mrres1 May 23 '13 at 23:33
0

I agree with @mrres1. You should just create separate Activities for each screen (or Fragments) for each. setContentView(layout) won't do anything at is not allowed.

You could also look into implementing a Dropdown ActionBar (example here).

deubaka
  • 1,447
  • 12
  • 22
  • I'm still new to Android development so not quite sure what you mean when you use the word fragments. Can you briefly example that? Thanks :) – Dan May 22 '13 at 02:32
  • Sorry for that. :D Check the links provided by @Tommy. They are good starting points. I personally recommend [this](http://mobile.tutsplus.com/tutorials/android/android-sdk_fragments/) though. – deubaka May 22 '13 at 02:51
  • hey deubaka, i have three activities set up so how would i implement that into the spinner? so when i pick the option "page 3" it would go to app3. What do i replace setontentView(layout) with? – Dan May 22 '13 at 03:05
  • don't use `setContentView`, instead, launch it via `startActivity(new Intent(getApplicationContext(), Activity2.class))`. I would also suggest to use the `position`-argument combined with a switch-case, as this would be better that checking the strings literally. – deubaka May 22 '13 at 04:44
  • can you help me figure out whats wrong now? The spinners are not showing up still.. And i get this logcat error message "E/SpannableStringBuilder(16206): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length " – Dan May 22 '13 at 08:39
  • could you post your code? regarding the error, you may check [this](http://stackoverflow.com/questions/13670374/android-span-exclusive-exclusive-spans-cannot-have-a-zero-length) – deubaka May 23 '13 at 00:31
  • my code is on the top ^ for some reason my spinner box is not even showing on the screen so it doesn't work not sure why.. – Dan May 23 '13 at 19:03
0

I recommend to consider Fragments. http://developer.android.com/guide/components/fragments.html

Or to easily implement it, you can use ActionBarSherlock library. http://actionbarsherlock.com

Tommy
  • 366
  • 3
  • 3