Thursday, April 28, 2011

UI component Snippets

In this article having the set of android sinppets used to set the value and listener action.

Spinner Control

      String selectedState  ="";
    String[] stateList = new String[] { "Select State", "MA", "NY" }; // Dropdown values
    Spinner sprState = (Spinner) findViewById(R.id.sprstate);


    // Set the value using the ArrayAdapter
    ArrayAdapter<String> stateListAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,stateList);
    stateListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    stateListAdapter.setAdapter(stateList);


    // Listener
    sprState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,View view, int position, long id) {
        selectedState = String.valueOf(sprState.getSelectedItem());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });



ListView Control

    ArrayList<String> tempListViewData = new ArrayList<String>();
   tempListViewData.add("One");
   tempListViewData.add("two");

   ListView l = (ListView) findViewById(R.id.ListView01);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, tempListViewData);
    l.setAdapter(adapter);

    l.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        Log.d("Position", arg2 + "");       
        }
    });


Button
Button btnFindStore = (Button) findViewById(R.id.btnhomego);
    private class FindStoreListener implements
            android.view.View.OnClickListener {
        @Override
        public void onClick(View v) {
       }
    }

btnCityStateOption.setOnClickListener(new FindStoreListener());

EditText
    EditText etUserInput = (EditText) findViewById(R.id.etuserinput);
    String.valueOf(etUserInput.getText()).trim();