Spinner ComboBox DropDown List Android example code
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
File MyOnItemSelectedListener.java
strings.xml
main.xml
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>
396LW NO topic_id
AD
Další témata ....(Topics)
Very dark picture, in photo editor black remains black
Very bright picture, in photo editor white will white
Dark picture but the chart is not in black color area - dark shades can be lighten in graphics editor
Very bright picture, in photo editor white will white
Dark picture but the chart is not in black color area - dark shades can be lighten in graphics editor
How setup color coloring highlight highlighting syntax font size and family in Eclipse Java and XML editor Android example
Java editor
xml editor
xml editor double click in Preferences dialog on xml -> Editor -> Syntax coloring
[caption id="attachment_627" align="alignleft" width="297" caption="Eclipse editor syntax color settings"][/caption]
Java editor
- Go to Eclipse menu Window -> Preferences
- Doubleclick on Java
- Double click on Editor
- Select Java, Javadocs or Comments and setup color and font
- Font size and family change from Window->Preferences-> General->Appearance->Colors and Fonts
- Press OK for saving changes
xml editor
xml editor double click in Preferences dialog on xml -> Editor -> Syntax coloring
[caption id="attachment_627" align="alignleft" width="297" caption="Eclipse editor syntax color settings"][/caption]
getContext() getApplicationContext() method Java Android example source
Context myContext_1 = ThisClassName.this; // to open a Dialog
Context myContext_2 = getContext();
Context myContext_3 = this.getContext();
Context myContext_4 = this;
Context myContext_5 = this.getApplicationContext ();
OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();
}
};
// Toast
Toast.makeText(getApplicationContext(), "Context == getApplicationContext "
, Toast.LENGTH_SHORT).show();
// store Context in public class
public class MyActivity extends Activity {
public static Context myCnt = null;
...
protected void onCreate(Bundle icicle) {
...
myCnt = this;
MyStorage.setContext(myCnt);
// or
// MyStorage.setContext(this);
// cntxFromStorage == this
Context cntxFromStorage = MyStorage.getContext();
...
};
};
public class MyStorage
{
private static Context cntStorageContext = null;
public static Context getContext() {
return cntStorageContext;
}
public static void setContext(Context context) {
MyStorage.cntStorageContext = context;
}
};
class DataBaseHelper extends SQLiteOpenHelper {
// get MyActivity context
Context cnt = MyStorage.getContext();
}
Context context = getApplicationContext();
String DB_PATH = "/data/data/"
+ context.getPackageName()
+ "/databases/";
Physical pixel (px)
Density independent pixel (dp)
Dots per inch (dpi) .. physical pixels per inch
px = dp * (dpi / 160)
Density independent pixel (dp)
Dots per inch (dpi) .. physical pixels per inch
px = dp * (dpi / 160)
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
public static int pxToDp(int px)
{
return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}
Editace: 2011-09-26 20:46:41
Počet článků v kategorii: 396
Url:spinner-combobox-dropdown-list-android-code-example