Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to show the spinner value from edittext input.
Example . if i enter x value in edittext then spinner should show x value without clicking any button.

I have got a code which do insert the edittext value into spinner by clicking button whereas i would like to change my spinner automatically while entering the input value in to edittext

What I have tried:

HTML
<pre><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="74dp" />

<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="74dp"
android:layout_marginRight="34dp" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:ems="10"
android:inputType="textPersonName" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:text="Button" />
</RelativeLayout>
Java
Java
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
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;

public class MainActivity extends Activity {

List<String> li;
Spinner sp1,sp2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

li=new ArrayList<String>();

li.add("Data 1");
li.add("Data 2");

sp1=(Spinner) findViewById(R.id.spinner1);
sp2=(Spinner) findViewById(R.id.spinner2);
Button b=(Button) findViewById(R.id.button1);
final EditText et=(EditText) findViewById(R.id.editText1);
call();

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
li.add(et.getText().toString());
et.setText(null);
call();
}
});
}

public void call() {
// TODO Auto-generated method stub

ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,li);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(adp);
sp2.setAdapter(adp);
sp1.setSelection((li.size()-1));


sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
sp2.setSelection(arg2);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Posted
Updated 4-Apr-18 7:20am
v2
Comments
David Crow 5-Apr-18 22:20pm    
I posted a solution to a very similar question here.

Quote:
i would like to change my spinner automatically while entering the input value in to edittext

What you need for your editText is Text Watcher
 
Share this answer
 
v4
Comments
Maybeok 4-Apr-18 13:10pm    
edittext is used for searching the data from database...
wseng 4-Apr-18 13:14pm    
Add the call function in the TextWatcher editText. Once there are value in editText, the function will be invoked.
Maybeok 4-Apr-18 13:16pm    
i tried but application stop working. do you have any sample code would suffice to proceed further..
wseng 4-Apr-18 13:17pm    
please edit your post by posting the latest code and error message.
Maybeok 4-Apr-18 13:21pm    
hi i am not expert in textwatcher. pls advise with your expertise
Java
et.addTextChangedListener(new TextWatcher() {

          public void onTextChanged(CharSequence s, int start, int before, int count) {
               {

                 call();


             }


          }

          public void afterTextChanged(Editable s) {

          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }


      });
 
Share this answer
 
Comments
wseng 4-Apr-18 13:25pm    
what error message you get ?
wseng 4-Apr-18 13:29pm    
You may refer https://stackoverflow.com/questions/29261532/adding-item-from-edit-text-to-spinner to get the editText value add to spinner
Maybeok 4-Apr-18 13:34pm    
hello wseng the link answer already avail in my details but what i am looking is without clicking an button the edittext data should show in spinner
Maybeok 4-Apr-18 13:37pm    
no error as such while entering the text in edittext i mean no reaction ..
wseng 4-Apr-18 13:49pm    
Can you put the method to afterTextChanged?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900