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) {
}
}); }
Related
I am inflating Spinner along with that I am also inflating some other views
LinearLayout child_linear = (LinearLayout) findViewById(R.id.child_linear);
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.add_custome_layout, child_linear, false);
name = (TextView) myView.findViewById(R.id.name);
price = (TextView) myView.findViewById(R.id.add_price);
names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
child_linear.addView(myView);
Now I getting data for the spinner. The question if there are 'n' no.of spinners how do I know which spinner item user selected?
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
//Show PopUp or Alerts
Toast.makeText(ProductView.this, "position" + names_spinner.getItemIdAtPosition
(position), Toast.LENGTH_SHORT).show(); }
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Even when I click on particular spinner item position is also not showing in Toast. What's my mistake?
You are showing toast only when user click on Select Toppings. Take a look to your code:
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
Change this line for
if (!parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
And alerts should be shown.
Additionally to know which item is clicked, the listener provided you the clicked position, you can search in you items array this position and you can know which item is.
EDIT:
Now, lets suppose you have more than one spinner. spinner1 and spinner2, for example. You could register an OnItemSelectedListener for each one.
Then you can create a global variable called Spinner clickedSpinner and inside the OnItemSelectedListener of the spinner1 you set the clickedSpinner to spinner1 and inside the OnItemSelectedListener of the spinner2 set the clickedSpinner to spinner2:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner1;
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And:
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner2;
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
So, after clicking any item of any Spinner you can now know which Spinner was clicked because is stored in clickedSpinner.
Please note that this variable is not mandatory, you could achieve the same behavior without this global variable, because in each OnItemSelectedListener you know which spinner is the clicked. So, you could do the following:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner1Position = spinner1.getItemIdAtPosition(position);
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And:
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner2Position = spinner2.getItemIdAtPosition(position);
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Without using a global variable.
Hope this helps.
Try this
final Spinner names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
ArrayAdapter<String> madapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.yourarray));
madapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
names_spinner.setAdapter(madapter);
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String mySpinner = (String) adapterView.getItemAtPosition(i);
Log.i("names_spinner",mySpinner);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(getApplicationContext(),"Your Message",Toast.LENGTH_LONG).show();
}
});
}
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);
}
}
I used two spinners in same fragment I want when the first spinner will complete its task then only the setOnItemSelectedListener will call for the second spinner.
Issue: But the issue is setOnItemSelectedListener is called on same time for both the spinners.How can I manage that. :(
What I want to do: I want to draw a graph on page load spinner 1 will darw a different graph and spinner 2 will draw a different graphon page load.
Any help will be Appreciated.
Why do you have same setOnItemSelectedListener for both the spinners.
Have structure like this:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
func1();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
func2();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
private void func1()
{
//do task of spinner1
}
private void func2()
{
//your task for second spinner
}
spinnerLeft = (Spinner) getActivity().findViewById(R.id.spinner_left);
dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerLeft.setAdapter(dataAdapter);
spinerRight = (Spinner) getActivity().findViewById(R.id.spinner_right);
rightSpinnerAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
rightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinerRight.setAdapter(rightSpinnerAdapter);
That is my two spinner that I calle d on onActivityCreated.
spinerRight.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
// here I called a method to download a chart and showing below 1st spinner
AppGlobals.getInstance().checkSpinner = 1;
}
private void addListenerOnSpinnerLeftItemSelection() {
spinnerLeft.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
//here I m called the same method to download and show chart below 2nd spinner
AppGlobals.getInstance().checkSpinner = 1;
}
But the before completing the first spinner functionality the second spinner called. I have but the int value so that when both the spinner's called the int value will tell the difference.
if (AppGlobals.getInstance().checkSpinner == 1) {
chartArea = (LinearLayout) getActivity().findViewById(R.id.chart);
} else if (AppGlobals.getInstance().checkSpinner == 0) {
chartArea = (LinearLayout) getActivity().findViewById(R.id.pie_chart);
}
i read many similar questions on this thread, but none of them help me...
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.imc_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.imc_array, 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);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// An item was selected. You can retrieve the selected item using
imc_met = parent.getItemAtPosition(pos).toString();
}
I declare imc_met as public String imc_met;. The problem is that imc_met does not contain the value of the selected item of the spinner, but it's null...
Where's the problem?
Thx in advance.
Use:
imc_met=Spinner.getSelectedItem().toString();
Instead:
imc_met = parent.getItemAtPosition(pos).toString();
Updated:
Seem you assigning Listener to your spinner not in correct way, do something like below:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String imc_met=spin.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Try this:
imc_met=Spinner.getSelectedItem().toString();
I'm sorry. I forgot
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
imc_met=Spinner.getSelectedItem().toString();
}
}
is
imc_met=spinner.getSelectedItem().toString();
not
imc_met=Spinner.getSelectedItem().toString();
int position = Arrays.asList(getResources().getStringArray(R.array.country_value_array)).indexOf(address.getCountry());
This will get index by the value.
If spinner isn't define in onCreate() method then have to use this:
String spinner_value = ((Spinner)findViewById(R.id.spinner1)).getSelectedItem().toString();
Try this code in your onCreate() method:
spinner.setOnItemSelectedListener(this);
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.