Note that there are some explanatory texts on larger screens.

plurals
  1. POAllow users to enter new value in Spinner
    primarykey
    data
    text
    <p>First of all, I am new to android development. I am building a small application which contains a spinner in it's UI which has predefined set (array) of four numbers and I am using <code>ArrayAdapter</code> to feed the value to spinner from a resource file. </p> <p>The spinner is working fine and users can select the values . But I also want users be able to enter new value if they wish to enter a new value. How would I do this?</p> <p><strong>Codes in onCreate Method of Activity</strong>:</p> <p><code>Spinner spinner = (Spinner) findViewById(R.id.spinner); // Create an ArrayAdapter using the string array and a default spinner layout ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this, R.array.points_preset, android.R.layout.simple_spinner_item); // Specify the layout to use when the list of choices appears adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new SpinnerActivity());</code></p> <p><strong>Code for SpinnerActivity Class:</strong></p> <p><strong>UPDATED to include input dialog:</strong></p> <pre><code> public class SpinnerActivity extends Activity implements OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; parent, final View view, int pos, long id) { if (pos==3) { // Set an EditText view to get user input final EditText input = new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this) .setMessage("Enter your Point here") .setView(input) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Editable editable = input.getText(); //if I uncomment following line, the application terminates // Spinner spinner = (Spinner) findViewById(R.id.spinner); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Do nothing. } }).show(); } } public void onNothingSelected(AdapterView&lt;?&gt; parent) { // Another interface callback } } ` </code></pre> <p><strong>Strings.xml resource file</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string name="app_name"&gt;PointCalculator&lt;/string&gt; &lt;string name="menu_settings"&gt;Settings&lt;/string&gt; &lt;string-array name="points_preset"&gt; &lt;item&gt;3&lt;/item&gt; &lt;item&gt;10&lt;/item&gt; &lt;item&gt;0&lt;/item&gt; &lt;item&gt;Oth&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt; </code></pre> <p>Updated and working version of SpinnerActivity Class</p> <p>` </p> <pre><code>public class SpinnerActivity extends Activity implements OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; parent, final View view, int pos, long id) { if (pos==3) { // Set an EditText view to get user input final EditText input = new EditText(MainActivity.this); new AlertDialog.Builder(MainActivity.this) .setMessage("Enter your Point here") .setView(input) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Editable editable = input.getText(); arrayList.add(editable.toString()); adapter.notifyDataSetChanged(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Do nothing. } }).show(); } } public void onNothingSelected(AdapterView&lt;?&gt; parent) { // Another interface callback } }` </code></pre> <p>Thanks,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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