Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Spinner ComboBox DropDown List Android example code


AD MOB

Spinner in Android application is equivalent of ComboBox in WinApi.
Spinner get selected item to string example.
Spinner get selected item position to int example.

Main activity class MainComboBox.java

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

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.countries_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

        Button myButton =(Button)findViewById(R.id.button1);
        myButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	
            Spinner sp =	(Spinner)findViewById(R.id.spinner);
            String spinnerString = null;
            spinnerString = sp.getSelectedItem().toString();
            int nPos = sp.getSelectedItemPosition();
            
             
                Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString,
                    Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos,
                		Toast.LENGTH_LONG).show();
            }
        });
    }
}



File MyOnItemSelectedListener.java

public  class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
      Toast.makeText(parent.getContext(), "Item is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}


strings.xml

<resources>
    <string name="hello">Hello World, MainComboBox!</string>
    <string name="app_name">ComboBox</string>
    <string name="prompt">Choose a country</string>
    <string-array name="countries_array">
        <item>China</item>
        <item>India</item>
        <item>USA</item>
        <item>Indonesia</item>
        <item>Brazil</item>
        <item>Pakistan</item>
        <item>Nigeria</item>
        <item>Bangladesh</item>
        <item>Russia</item>
    </string-array>    
</resources>


main.xml

<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/prompt"
    />
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/prompt"
    />
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>





397LW NO topic_id




AD

Další témata ....(Topics)


209

Chrome youtube shockwave flash plugin crashes solution | chrome-shockwave-flash-plugin-crashes


Google Chrome and Shockwave Flash crashes on youtube.com

  • open chrome://plugins/ with Google Chrome

  • click on Plus button for more details

  • find Flash (3 files)
  • disable two other Shockwave Flash

  • for more see image below



google-chrome-flash-crash-solution-1

google-chrome-flash-crash-solution-2
37

LayoutLib is too recent. Update your tool Eclipse Android | layoutlib-is-too-recent-update-your-tool-eclipse-android


LayoutLib is too recent. Update your tool!
Eclipse Android Graphical layout resolving problem.


  1. Open in Eclipse menu Help ->Check for Updates

  2. Select updates: [caption id="attachment_593" width="300" caption="Android Eclipse updates dialog"]android-eclipse-updates[/caption]


  3. Press Next and update all


  4. Restart Eclipse


  5. [caption id="attachment_596" align="alignleft" width="300" caption="Restart Eclipse if updates finished."]layoutlib-is-too-recent-update-your-tool[/caption]






20

How set gray text to EditText Android example | how-set-gray-text-to-edittext-android-example


Example source code for Android development. How set gray text to EditText when EditText is blank.
Hint text to display when the text is empty.
In layout/main.xml insert to EditText row Attribute Name android:hint="Some text"


<EditText
        android:id="@+id/myEdit" 
	android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:hint="Some text"
	android:singleLine="true"
</EditText>	



Programatically you can use method setHint:


// setHint(CharSequence hint) example
(EditText)findViewById(R.id.myEdit).setHint("My text");
// or int resId as method setHint(int resId) 
EditText myEdit = findViewById(R.id.myEdit);
myEdit.setHint(R.string.app_name);



315

Importing google-play-service library project to workspace issue | importing-google-play-service-library-project-to-workspace-issue


Try two way in Eclipse IDE:

1.) Import the library project into your Eclipse workspace.
Click File > Import,
select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.
If project not visible with checkbox try next step as see below.

2.) Click
File > New > Other
select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it.
284

WAV Sound Quality is Poor Android MediaPlayer | wav-sound-quality-is-poor-android-mediaplayer


Try convert WAV file to MP3 format for example by Audacity sound editor and replay:
//audacity.sourceforge.net/help/faq_i18n?s=install&i=lame-mp3


MediaPlayer mp  = MediaPlayer.create(getApplicationContext(), R.raw.mymp3file);
mp.setLooping(true);
mp.start();



Editace: 2011-09-26 20:46:41
Počet článků v kategorii: 397
Url:spinner-combobox-dropdown-list-android-code-example

AD
11 Share
Share
Tweet