i have two spinners, city and neighborhood, when i select a city it populates neighborhoods list. but i want when i click neighborhood spinner and city is empty to show a message or an Alertdialog.
i tried to use ontouchevent on the second spinner but it doesn't work.
i m using custom spinner com.toptoche.searchablespinnerlibrary.SearchableSpinner thanks for help
neighborhoodSpinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(getApplicationContext(),"city emty",Toast.LENGTH_LONG).show();
}
return false;
}
});
why don't you use onClickListener instead?
s.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
cityspinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItemcity = parent.getItemAtPosition(position).toString();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
neighborhoodSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItemneighbour = parent.getItemAtPosition(position).toString();
if(selectedItemcity.equals("your item name")) //Also you can check previous spinners position here if 0 then show popup or dilaog
{
// do your stuff
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
Use setOnItemSelectedListener to get spinner item selection listener.
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
// Write your code here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
It depends whether your spinner has a default value to mean nothing.
For example, if your spinner has a city value of "Please select a city" or something like that, which doesn't have to be the case.
Your spinner may already have a city selected when it's populated.
In the case that "Please select a city" is an option, this code might help:
Spinner city= findViewById(R.id.spinner_city);
Spinner neighborhood = findViewById(R.id.spinner_neighborhood);
neighborhood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(city.getSelectedItem().toString().equals("Please select a city")){
Toast.makeText(MyActivity.this, "Please select a city first", Toast.LENGTH_SHORT).show();
}else{
// do nothing
}
}
});
Related
I am trying to add spinner selected position value and edittext into sqlite database but I don't know how would I use Spinner position value during insertion into sqlite database.
Someone help me please.
Below is the spinner Item position code:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select expense type")) {
// Do nothing
} else {
item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing
}
Below is the Code where I want to use Spinner Item Position selected value and edittext value togather for inserting into sqlite database. so how could I get spinner position value for use into InsertExpense() function .
public void InsertExpense() {
addExpId.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onClick(View v) {
}
});
declare a global variable to store the selected item in spinner
String selectedItem=";
and here
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//yourList is the array your are using in your spinner adapter
selectedItem=yourList.get(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing
}
and then use this variable in your insertion method
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 dynamically inflate a view which contains spinner and edit text.how to understand which spinner listener is currently working.
count = 1;
buttonadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
Spinner spin_securityType = (Spinner) MyItem[count].findViewById(R.id.spin_securityType);
EditText ed_amount = (EditText) MyItem[count].findViewById(R.id.ed_amount);
SpinnerLogoAdapter spinnerAdapter33 = new SpinnerLogoAdapter(Activity.this.getApplicationContext(),
securityTpeIcons, securityTpeitems);
spin_securityType.setAdapter(spinnerAdapter33);
MainItemLayout.addView(MyItem[count]);
spin_securityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
//how to know which spinner is selected ????????
}
#Override
public void onNothingSelected(AdapterView << ? > parent) {}
});
}
});
what i need is add a validation on one which validates all 5 items at a same time.I have tried setTag but it does not work.please help
I found the solution myself. I programatically added id for each spinner and access it inside the listener to know which spinner is selected.For that i added id to the spinner
spin_securityType.setId(id); ex:id=1
and access it inside spin_securityType.setOnItemSelectedListener()
spin_securityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
int Id=parent.getId();//shows id as 1
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This question already has answers here:
Spinner : onItemSelected not called when selected item remains the same
(11 answers)
Closed 5 years ago.
I have used Spinner to display toast on particular item selected in Spinner.
I am displaying toast when "other" is selected in Spinner.
I have done it as below :
spinnerTemp=(Spinner)findViewById(R.id.spinnerTemp);
spinnerTemp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinnerTemp.getSelectedItem().toString().equals("other")){
Toast.makeText(SocialLoginActivity.this, "Displayed", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
The issue is When I am selecting "other" for the first time, toast is displaying.. : FINE. Now, on the spot If i select "other" again, then toast is not displaying. Why ?
Now, if i select some other value and try again selecting "other", its working fine. Issue is with the selecting "other" one after another.
What might be the issue ?
Thanks.
Here a little better implementation:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
Try this
spinnerTemp=(Spinner)findViewById(R.id.spinnerTemp);
spinnerTemp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = spinnerTemp.getSelectedItem().toString();
if(item.equalsIgnoreCase("other")){
Toast.makeText(SocialLoginActivity.this, "Displayed", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
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);
}