Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 4.84 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android 2.2 requestFocus() and spinners
  2. spinner.setFocusable(true);
  3. spinner.setFocusableInTouchMode(true);
  4.        
  5. import java.util.ArrayList;  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.text.Editable;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.AdapterView;
  13. import android.widget.AdapterView.OnItemSelectedListener;
  14. import android.widget.ArrayAdapter;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.Spinner;
  18. import android.widget.Toast;
  19.  
  20. public class dynamic_spinner_main extends Activity {
  21.  
  22.     private Spinner m_myDynamicSpinner;
  23.     private EditText m_addItemText;
  24.     private ArrayAdapter<CharSequence> m_adapterForSpinner;
  25.  
  26.     /** Called when the activity is first created. */
  27.     @Override
  28.     public void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.main_spinner);
  31.  
  32.         ///////////////////////////////////////////////////////////////
  33.         //grab our UI elements so we can manipulate them (in the case of the Spinner)
  34.         //    or add listeners to them (in the case of the buttons)
  35.         m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);        
  36.         m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
  37.         Button addButton = (Button)findViewById(R.id.AddBtn);
  38.         Button clearButton = (Button)findViewById(R.id.ClearBtn);
  39.  
  40.         ////////////////////////////////////////////////////////////////
  41.         //create an arrayAdapter an assign it to the spinner
  42.         m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
  43.         m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
  44.         m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
  45.         m_adapterForSpinner.add("gr");        
  46.         m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  47.             @Override
  48.             public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
  49.                 // your code here
  50.                 Intent mIntent=new Intent(dynamic_spinner_main.this,sampleLocalization.class);
  51.                 mIntent.putExtra("lang", m_myDynamicSpinner.getItemIdAtPosition(position));
  52.                 System.out.println("Spinner value...."+m_myDynamicSpinner.getSelectedItem().toString());
  53.                 startActivity(mIntent);
  54.             }
  55.  
  56.             @Override
  57.             public void onNothingSelected(AdapterView<?> parentView) {
  58.                 // your code here
  59.             }
  60.  
  61.         });
  62.         ////////////////////////////////////////////////////////////////
  63.         //add listener for addButton
  64.         addButton.setOnClickListener(new OnClickListener(){
  65.  
  66.             @Override
  67.             public void onClick(View v) {              
  68.                 addNewSpinnerItem();
  69.             }                  
  70.         });
  71.  
  72.         ////////////////////////////////////////////////////////////////
  73.         //add listener for addButton
  74.         clearButton.setOnClickListener(new OnClickListener(){
  75.  
  76.             @Override
  77.             public void onClick(View v) {
  78.                 clearSpinnerItems();
  79.             }          
  80.         });  
  81.     }
  82.  
  83.     private void addNewSpinnerItem() {
  84.         CharSequence textHolder = "" + m_addItemText.getText();
  85.         m_adapterForSpinner.add(textHolder);
  86.     }
  87.  
  88.     private void clearSpinnerItems() {
  89.         m_adapterForSpinner.clear();
  90.         m_adapterForSpinner.add("dummy item");
  91.     }      
  92. }
  93.        
  94. <?xml version="1.0" encoding="utf-8"?>
  95. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  96.     android:orientation="vertical"
  97.     android:layout_width="fill_parent"
  98.     android:layout_height="fill_parent">
  99.  
  100.     <EditText android:layout_height="wrap_content"
  101.             android:layout_margin="4px"
  102.             android:id="@+id/newSpinnerItemText"
  103.             android:layout_width="fill_parent"></EditText>
  104.     <Button android:layout_height="wrap_content"
  105.             android:id="@+id/AddBtn"
  106.             android:layout_margin="4px"
  107.             android:layout_width="fill_parent"
  108.             android:text="Add To Spinner"></Button>
  109.     <Button android:layout_height="wrap_content"
  110.             android:id="@+id/ClearBtn"
  111.             android:layout_margin="4px"
  112.             android:layout_width="fill_parent"
  113.             android:text="Clear Spinner Items"></Button>
  114.     <Spinner android:layout_height="wrap_content"
  115.             android:id="@+id/dynamicSpinner"
  116.             android:layout_margin="4px"
  117.             android:layout_width="fill_parent"></Spinner>
  118. </LinearLayout>
  119.        
  120. <Spinner
  121.     android:id="@+id/spinner"
  122.     android:focusable="true"
  123.     android:layout_width="wrap_conten"
  124.     android:layout_height="wrap_content" />
  125.        
  126. findViewById(R.id.spinner).setFocusable(true);