Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>use this</p> <pre><code>import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.view.View.OnClickListener; 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.Toast; public class dynamic_spinner_main extends Activity { private Spinner m_myDynamicSpinner; private EditText m_addItemText; private ArrayAdapter&lt;CharSequence&gt; m_adapterForSpinner; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_spinner); /////////////////////////////////////////////////////////////// //grab our UI elements so we can manipulate them (in the case of the Spinner) // or add listeners to them (in the case of the buttons) m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner); m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText); Button addButton = (Button)findViewById(R.id.AddBtn); Button clearButton = (Button)findViewById(R.id.ClearBtn); //////////////////////////////////////////////////////////////// //create an arrayAdapter an assign it to the spinner m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item); m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); m_myDynamicSpinner.setAdapter(m_adapterForSpinner); m_adapterForSpinner.add("gr"); m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView&lt;?&gt; parentView, View selectedItemView, int position, long id) { // your code here Intent mIntent=new Intent(dynamic_spinner_main.this,sampleLocalization.class); mIntent.putExtra("lang", m_myDynamicSpinner.getItemIdAtPosition(position)); System.out.println("Spinner value...."+m_myDynamicSpinner.getSelectedItem().toString()); startActivity(mIntent); } @Override public void onNothingSelected(AdapterView&lt;?&gt; parentView) { // your code here } }); //////////////////////////////////////////////////////////////// //add listener for addButton addButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { addNewSpinnerItem(); } }); //////////////////////////////////////////////////////////////// //add listener for addButton clearButton.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { clearSpinnerItems(); } }); } private void addNewSpinnerItem() { CharSequence textHolder = "" + m_addItemText.getText(); m_adapterForSpinner.add(textHolder); } private void clearSpinnerItems() { m_adapterForSpinner.clear(); m_adapterForSpinner.add("dummy item"); } } </code></pre> <p>main_spinner.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;EditText android:layout_height="wrap_content" android:layout_margin="4px" android:id="@+id/newSpinnerItemText" android:layout_width="fill_parent"&gt;&lt;/EditText&gt; &lt;Button android:layout_height="wrap_content" android:id="@+id/AddBtn" android:layout_margin="4px" android:layout_width="fill_parent" android:text="Add To Spinner"&gt;&lt;/Button&gt; &lt;Button android:layout_height="wrap_content" android:id="@+id/ClearBtn" android:layout_margin="4px" android:layout_width="fill_parent" android:text="Clear Spinner Items"&gt;&lt;/Button&gt; &lt;Spinner android:layout_height="wrap_content" android:id="@+id/dynamicSpinner" android:layout_margin="4px" android:layout_width="fill_parent"&gt;&lt;/Spinner&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload