how to add items in edit text when i click on spinner? - android

In my application i have used spinner and edit text.i have created an XML file and set it as background to the spinner which looks like a drop down arrow.When i click the spinner the items selected from spinner should be set in edit text but it displays in edit text as well spinner as the below image. can any one help me with this??
public class newcard extends Activity {
Spinner spinner;
Button btn;
EditText ed,ed1,ed2;
List<String> list;
private String[] countries_list={"01/2014","02/2014","03/2014","04/2014"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnewcard);
btn= (Button) findViewById(R.id.button1);
spinner = (Spinner) findViewById(R.id.spinner2);
ed = (EditText) findViewById(R.id.editText3);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
list = new ArrayList<String>();
list.add(" ");
list.add("select");
list.add("01/2014");
list.add("02/2014");
list.add("03/2014");
list.add("04/2014");
ArrayAdapter<String> adp = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, list);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
switch(arg2) {
case 0 :
ed.setText("Select");
break;
case 1 :
ed.setText("01/2014");
break;
case 2 :
ed.setText("02/2014");
break;
case 3 :
ed.setText("04/2014");
break;
default :
ed.setText("Nothing");
break;
}
}
//#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

For setting spinner value to ""
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3)
{
item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setText("");
}

Related

Using Spinner And List Views with setOnItemSelectedListener

I'm supposed to make a task , I have a spinner connected with a String array x , this array contains three values , so I want when clicking any choice of the spinner a specific list view will will give a specific three values and , this is my code :
public class Four extends ActionBarActivity {
String x [] = {"Jordan","Saudi Arabia", "Syria"};
String Jordan[] = {"Amman","Aqaba","Sarqa"};
String Saudi[] = {"Riyadh","Jeddah","Khobar"};
String Syria[] = {"Hems","Halab","Demashk"};
Spinner sp1 ;
ListView lv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.four);
lv1 = (ListView)findViewById(R.id.listView1);
// Jordan List View
ArrayAdapter<String> jor = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Jordan);
lv1.setAdapter(jor);
// Saudi Arabia List View
ArrayAdapter<String> saud = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Saudi);
lv1.setAdapter(saud);
// Syria List View
ArrayAdapter<String> syr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Syria);
lv1.setAdapter(syr);
sp1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item , x);
sp1.setAdapter(a);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
The thir parameter of onItemSelected() (int arg2 in your example, but I would suggest you rename them) is the position you selected in the Spinner. So you could implement that method like this:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position == 0)
lv1.setAdapter(jor);
else if (position == 1)
lv1.setAdapter(saud);
else if (position == 1);
lv1.setAdapter(syr);
}
Remember that all the variables involved (lv1, jor, saud, syr) must be defined as final to be used inside the anonymous class, e.g.
final ArrayAdapter<String> jor = ...
final ArrayAdapter<String> saud = ...

Display EditText items on Spinner selection

I have a group of items in spinner and on their click I want to show the respective text in EditText . How I can achieve this. I thought of using switch but it doesnot work for strings. I want someone to tell me right approach for doing this.
I want the EditText array(mysuburb) to respond accordingly to the Spinner items(mystate) click .
Code:-
String[] mysuburb =new String[]{"sub1" ,"sub2","sub3","sub4","sub5","sub6"};
String[] mystate= new String[]{"NSW","Victoria","Qld","NT","WA","SA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
// LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
sstate = state.getSelectedItem().toString();
/* String sub= state.getItemAtPosition(0).toString();
if(sub=="sub1")
suburb.setText("sub1") ; */
suburb.setText(arg0.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,int i, long l) {
sstate = state.getSelectedItem().toString();
suburb.setText(sstate);
}
}

Spinner dropdown list doesn't show

http://i.stack.imgur.com/HGsTw.jpg
This is the error I get and the app just crashes and shows nothing!.
PS:When I moved those last 4 lines of code from the OnCreate method to the beginning of the OnItemSelected method I get no errors and this layout (http://i.stack.imgur.com/1DiLI.jpg) shows but no dropdown lists (I expect a list of colors when I click the arrow button next to background).
Here is my code:
public class SetLayout extends Activity implements OnItemSelectedListener {
TextView o_markertv, x_markertv, bcktv, gridtv;
String[] colors ={"Red", "Green", "Blue", "Black", "White"};
Spinner spinner1, spinner2, spinner3, spinner4;
LinearLayout bck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setlayout);
bck= (LinearLayout) findViewById(R.id.bck);
o_markertv= (TextView) findViewById(R.id.o_markertv);
x_markertv= (TextView) findViewById(R.id.x_markertv);
bcktv= (TextView) findViewById(R.id.bcktv);
gridtv= (TextView) findViewById(R.id.gridtv);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
//Button BtnSubmit = (Button) findViewById(R.id.btnSubmit);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (SetLayout.this,android.R.layout.simple_spinner_item,colors);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position= spinner1.getSelectedItemPosition();
switch(position){ //red, green blue black white
case 0:
bck.setBackgroundColor(color.red);
break;
case 1:
bck.setBackgroundColor(color.green);
break;
case 2:
bck.setBackgroundColor(color.blue);
break;
case 3:
bck.setBackgroundColor(color.black);
break;
case 4:
bck.setBackgroundColor(color.white);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

Select item from spinner to show edittext

I am trying to show an EditText when a certain Item from a Spinner is selected. So far I have created the spinner and the EditText but I don't really know what my next step is.
I don't want to display the selection in the EditText, I just want to display the EditText field.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = (EditText) findViewById(R.id.edit_text_box);
editText.getText().toString();
}
public void addItemsOnSpinner()
{
spinner1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.spinner_item, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter (adapter);
}
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
editText.setText(spinner1.getSelectedItem());
}
This is also what I have in my string array + wish to only display the edittext box when item2 is selected
<string-array name="spinner_item">
<item>Item 1</item>
<item value="Item2">Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
You should set visibility of editText to invisible or gone in XML, depends on what suits your needs better. Then you can use following to make it appear:
public void onItemSelected(AdapterView<?> spinner1, View view, int pos, long id)
{
yourEditText.setVisibility(View.VISIBLE);
}
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int x = spinner1.getSelectedItemPosition();
valSel = items[x];
}
String spinner1val = spin1.getText().toString(); //extract value of text from spinner
EditText text = (EditText) findViewById(R.id.EditText1); //get id of EditText box
text.setText(spinner1val);
Alternatively, if you want to check the value of item selected in Spinner, the 'valsel' contains the value. Just Toast the valsel.
Get the selected values from the spinner,
String anyvariable=String.valueOf(spin.getSelectedItem());
Now you can show this String value in the edit text,
EditText text = (EditText) findViewById(R.id.your_text);
text.setText(anyvariable);
#Override
public void onItemSelected(AdapterView<?> month, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
selectedMonth= month.getItemAtPosition(arg2).toString();
Log.d("Tag",""+selectedMonth);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
//selected_item= arg0.getChildAt(1).toString();
}
});
selected_item contains the item ,so just set it to editView like
editView.setText(selected_item)
You have to set text on edittext onItemSelected . First do change as per MKJParekh Suggest.
public void onItemSelected(AdapterView<?> spinner1, View view,int pos, long id)
{
yourEditText.setText(spinner1.getSelectedItem())
}
EditText height1, weight1, height, weight; Spinner height_spinner,
weight_spinner; String heightInputString, weightInputString; Button
calculatebmi;
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
// set your class members as they start out null.
// do this for all of them
height1 = (EditText) findViewById(R.id.idofheight1inxml);
height_spinner = (Spinner) findViewById(R.id.idofheightspinnerinxml);
....
// Show the Up button in the action bar.
setupActionBar();
setupSpinners(); }
void setupSpinners(){
height_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
//I.E. if in the height spinner CM is selected I would like to hide the second height edittext field.
// I'm not sure if this is meant to be "height1" or "height"
if (position == 0){
height.setVisibility(View.GONE);
} else {
height.setVisibility(View.VISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// if you want to add similar logic for weight spinner, do that with this :
weight_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// put your code here for weight spinner
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}); }

Adding OnItemSelectedListener to Spinner

I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.
This is my OnCreate function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);
load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});
routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}
This is my load_routes function
private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);
TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}
This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!
final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);
spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);
spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ed.setText(s2[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.

Categories

Resources