0

This is my code:

    package com.testotspeech;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidTestToSpeechActivity extends Activity implements
        TextToSpeech.OnInitListener, OnItemSelectedListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;
    private ArrayList<String> itemsList;
    private Spinner spinner;
    private String contry_name;
    private ArrayAdapter<String> dataAdapter;
    private TextView textview;
    private Iterator itr;
    private String[] t = {"Please Select An Item"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("----------",Arrays.toString(Locale.getAvailableLocales()));
        itemsList = new ArrayList<String>();
        itemsList.add(Arrays.toString(Locale.getAvailableLocales()));
        spinner = (Spinner)findViewById(R.id.spinner1);
        spinner.setOnItemSelectedListener(this);


        dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,t);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(dataAdapter);
        textview = (TextView)findViewById(R.id.textView1);






        tts = new TextToSpeech(this, this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.ENGLISH);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();

            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

    private void speakOut() {

        String text = txtText.getText().toString();
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

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

        if (position == 0)
        {
        }
        else
        {

        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
        textview.setText("");
    }


}

EDITED:

What i want to do is when i click on thw spinner it will collapse/open down and i will have for each item and box of it self and it will be in row for example i clicked on the spinner i will see now under it:

Hello

Bye

Daniel

and now if i click on Hello it will put the Hello in the textView1 if i click on the Bye it will put it also in textView1 and so on. But the graphics the designing of the spinner i want it to be that when i click on it it will collapse down and show me the items in a row so i can click and select each item in a single click.

Now what i did is just adding the spinner a text "Please Select An Item" I uploaded image of how i wanted it to be like for example:

spinner example

user1434011
  • 113
  • 2
  • 10
  • Updated my answer, hopefully that'll get you going in the right direction. Let us know how it goes. – Barak Jun 08 '12 at 15:44

2 Answers2

1

That is the behavior of the spinner, there is always a selected item.

You can create a flag and use that in an override of the onItemSelected, put a counter in there and use that to ignore the first time in.

Like this:

private int spinnerSelectCount = 0;

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(spinnerSelectCount == 0) {
        // Do nothing... initial item on spinner display is the selected item
    } else {
        // your code to process spinner selection here
    }
    }
});

EDIT

You need to put your string as the first item in your array, not a separate string.

itemsList = new ArrayList<String>();  
itemsList.add("Please Select An Item");
itemsList.add(Arrays.toString(Locale.getAvailableLocales())); 

Then call the spinner using the array:

    dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,itemsList);        
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);        
    spinner.setAdapter(dataAdapter);  

And since you're doing that you should change the filter to be based on position, not count.

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(pos == 0) {
        // Do nothing... initial item on spinner display is the selected item
    } else {
        // your code to process spinner selection here
        textview.setText(itemsList.get(pos));
    }
    }
});

EDIT 2

Are you looking for multi-selection? That's not possible with a standard spinner, it is set up to select one item only. If you are really looking to select multiple items you need to use a list or create a custom spinner.

Fortunately, it looks like someone has already done that. You can find the code in this SO Answer

EDIT 3

The MultiSpinner does not need a new project. You merely create a new class and use that to populate your spinner.

1) Create a new class called MultiSpinner
2) Copy the code from link (leave out the package name as you'll want to use your own)
3) In your xml call out the spinner like so (replacing com.yourpackage.name with your actual package name):

<com.yourpackage.name.MultiSpinner android:id="@+id/multi_spinner" /> 

4) Call the spinner as shown in the link:

MultiSpinner multiSpinner = (MultiSpinner) findViewById(R.id.multi_spinner); 
multiSpinner.setItems(items, getString(R.string.for_all), 
                this); 
Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84
  • I added: private String[] t = {"Please Select An Item"}; and changed only this line to: dataAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,t); so now when im running the application i see in the spinner the string t. But what do i do now in the else in the even onItemSelected i didnt understand how to make the items to be shown in a row when i click the spinner and how to make the selection in the ELSE – user1434011 Jun 07 '12 at 07:45
  • I son't understand what you mean by "make the items to be shown in a row"... You said you wanted the selection in a TextView, so you just set it to that like you did before with `textview.setText(itemsList.get(position));`. Updated my answer to show that. – Barak Jun 07 '12 at 12:37
  • Barak what i mean by shown in a row is that when i click on the spinner to collapse it down i want to see all the items in a row and then i will be able to select each one of them from the list. I will update my question post and show you what i mean. – user1434011 Jun 07 '12 at 13:10
  • Barak i just edited my main post the question and uploaded an image to describe what i wanted. In General the main idea is that i will have many items to select from like in the image i uploaded and somehow i want each item to put in the LOCALE. in my code i have itemsList array im using to get all LOCALE languages from my device. I want to see each language in the spinner in a row like in the image example and then i want when i select one item it will replace the language in the line LOCALE. now its LOCALE.ENGLISH but instead EMGLISH i want it to be the item i will select. – user1434011 Jun 07 '12 at 13:20
  • That's what it should be doing... the spinner drops down and shows all the items you loaded to it, you select one and it is loaded to the textview. What part of that is not happening? – Barak Jun 07 '12 at 13:34
  • Barak no i changed the code for this i will update it in the main post question above. – user1434011 Jun 07 '12 at 13:41
  • Barak what i changed in the code you can see above is that i put ,t in the dataAdapter instance instead of itemsList i put t since i wanted it to show first time "Please Select An Item" so now it show "Please Select An Item" but when i click on it i dont see all the items. Second im not sure what to do in the ELSE in the onItemSelected event. if position is 0 do nothing else what to do there.. – user1434011 Jun 07 '12 at 13:44
  • Barak if instead t i put back itemsList there so when im running the application all the items are at once in the spinner already and when i click on the spinner i see all the items on one level drop down button under the spinner. this is not good. i need to find out what to do with the dataAdapter instance line if im using t there then where and how im puting the itemsList so it will show me them in a row one under one when i click the spinner. – user1434011 Jun 07 '12 at 13:48
  • Barak ok its slmost working good. When i click the spinner i see under it a line like a button like a new spinner with all the items from itemsList inside. What i need is to see a button for each item in a separate button one under one. Now when i click the spinner all the items in one button . I need them to be each in one own button so i can select each time one item from the itemsList and to select them all togeather like its now. – user1434011 Jun 07 '12 at 15:22
  • I'm sorry, I completely don't understand your issue now. A spinner is made to display a list for you to select from, and that's what it's doing. You're going to have to show what you're seeing and a mockup of what you want, becasue I can't understand it from what you are saying. – Barak Jun 07 '12 at 15:43
  • Ok, re-reading your question/edits, I think I know what's going on. You actually want to be able to select multiple items, correct? See my updated (2nd time) answer. – Barak Jun 07 '12 at 15:54
  • Yes Barak thats what i wanted multiple items selection. I will look in the link you added in the edit 2 and let you know. Thanks. – user1434011 Jun 07 '12 at 17:03
  • I didnt understand yet how to use the class in the link Barak in your edit2 ? Lets say i want to keep the spinner im using now and also to add the spinner in the link do i need to add a new activity class to my project ? In the link it looks like the spinner have its own project. – user1434011 Jun 08 '12 at 05:39
0

An easy solution would be to set the first item of the spinner to a text like "Please select a value..". Then in the code..

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(pos == 0) {
    // do nothing..
    } else {
    // your code..
    }
}
});
hasilk
  • 21
  • 1
  • 6