How to detect whether spinner item selected or not? - android

Normally spinner can get item by setOnItemSelected but what if the user didn't click to select at all? Then how should I edit the code for detect whether the user click on the spinner to select item or not? Can anyone please advice me?
Here's my code:
s = (Spinner) findViewById(R.id.Rg);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.gender_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
gender = adapterView.getItemAtPosition(i).toString();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(Register.this, "Please select your gender", LENGTH_SHORT).show();
return;
}
});

you can use
Spinner.getSelectedItem()
or
Spinner.getSelectedItemId() //return the id of the selected item (should override the method"getItemId" in the adapter class )
or
Spinner.getSelectedItemPosition() //return the position of the selected item (index in the adapter)

Related

How to set last recorded value to spinner dropdown?

String Food;
String[] FoodArray;
Spinner spinFood;
spinFood= (Spinner) findViewById(R.id.spin_food);
FoodArray=getResources().getStringArray(R.array.carray);
spinFood.setAdapter(new
CustomSpinnerAdapter(ChildActivity.this,R.layout.activity_child,FoodArray));
spinFood.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
if(position==1){
Food=getString(R.string.textYes);
}
else if (position==2){
Food=getString(R.string.textNo);
}
else {
Food="0";
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
i tried a lot but didn't get the answer,
please help me.
the spinner should be set to a last recorded value which comes from the database, by default.
String compareValue;
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinFood.setAdapter(new CustomSpinnerAdapter(
getApplicationContext().this, R.layout.yourlayout,
compareValue));
if (!compareValue.equals(null)) {
int spinnerPosition = adapter.getPosition(compareValue);
spinFood.setSelection(spinnerPosition);
}
take your last recorded value i.e compareValue from sqlite database and set that value to ArrayAdapter and check for the null condition.
You can use spinner.setSelection() method to record your last transaction

Android spinner click outside spinner immediately?

When i click and open the "Spinner dialog dropdown",
i want to click and trigger another spinner dropdown immediately in same layout.
Example:
1. "spinner1" and "spinner2" in same layout
2. When i click "spinner1" and open its dropdown,
3. then i want to click "spinner2" and open its dropdown immediately
4. with no wait for "spinner1" close its menu for click "spinner2"
5. (and also "spinner1" should close its menu)
when i click after any dropdown is open, anywhere in screen is blocked for close the opened dropdown, can't click any view until dropdown is close?
Is it possible to click any other view when dropdown is open in Android?
Try this code:
Spinner sp1 = (Spinner) findViewById(R.id.sp_category);
final Spinner sp2 = (Spinner) findViewById(R.id.sp_category2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_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
sp1.setAdapter(adapter);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.values, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
sp2.setAdapter(adapter2);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
sp2.performClick();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

How to add default value in Spinner other then adapter Value

i have some data coming from server and i have to show that value in Android Spinner.
Adapter that is attach to Spinner also getiing from server.
please let me know how to show initial value coming from server.and show adapter value after click on Spinner.
Spinner mySpinner = (Spinner) findViewById(R.id.householdspinner);
mySpinner.setAdapter(new ArrayAdapter<String>(Edit_Voter_Information.this,
android.R.layout.simple_spinner_dropdown_item,
householdIncome));
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String item = arg0.getItemAtPosition(position).toString();
house_Hold_Income = item;
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
Initially i stored some values in String[] names. when activity is run spinner position 0(zero)is selected by default.
Check my code below:
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.spinner_style, names);
spinner.setAdapter(arrayAdapter);
spinner.getSelectedItemPosition();//which returns names[0] initially.
if click other position(i) it returns names[i], where i =0,1,2,..

how to shorten spinner selected item text in android?

I have used android custom spinner.
My custom spinner look like bellow
My List data is like
List<String> list = new ArrayList<String>();
list.add("001-00434016457-Md. Mokhhlesur Rahman");
list.add("001-00434016724-Mohammad Nazim Ullah");
list.add("001-00434016725-Mohsin Kabir");
list.add("001-00434016881-Maruf Pervaz Khan");
list.add("001-00434017171-Md.Nazmul Hasan");
My Spinner Adapter and setting data in spinner is like bellow
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
My problem is spinner's item text is large like "001-00434016881-Maruf Pervaz"
I want to select only id like 001-00434016881.
I don't want to get full text of the spinner item selection. How can I remove name part like -Maruf Pervaz Khan from 001-00434016881-Maruf Pervaz on item selection from spinner.
Please Help me
Try this. If the row item maintains same pattern as 001-00434016881-Md.Some Name
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItem(position).substring(0,adapter.getItem(position).lastIndexOf("-")), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

android:opening new intent in spinner view

I am making an app in which i have to use spinner view to show some items and i want on click of item it should go to that page. i want when i clicke on spanish i should go to spanish pageMy code is as follows.
System.out.println("test1");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
System.out.println("test2");
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.models, android.R.layout.simple_spinner_item);
System.out.println("test3");
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
System.out.println("test3");
spinner.setAdapter(adapter);
System.out.println("test4");
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
You have to write intent in your onItemSelected method for page/activity which you want to open.
and set position for that.
#Override
public void onItemSelected(AdapterView<?> adaptername, View view,int position, long id)
{
if(position==0)
{
// write the intent for page which you want to open
}
if(position==1)
{
//
}
.
.
.
and same
}

Categories

Resources