Android spinner click outside spinner immediately? - android

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) {
}
});
}

Related

How to detect whether spinner item selected or not?

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)

Android: spinner causes app to crash

I'm making a fairly basic app which consists of a custom spinner and an image view. On selecting an item from the spinner, the imageView displays a custom image corresponding to the position of the item in the spinner.
Here's MainActivity.java:
ImageView imageView;
Spinner spinner;
int images[] = {R.drawable.defaultImage,R.drawable.mercury,R.drawable.venus,R.drawable.earth,R.drawable.mars,R.drawable.jupiter,R.drawable.saturn,R.drawable.uranus,R.drawable.neptune,R.drawable.pluto};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(MainActivity.this,
R.array.Planets, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(
new NothingSelectedSpinnerAdapter(
adapter,
R.layout.contact_spinner_row_nothing_selected,
MainActivity.this));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
((TextView) parent.getChildAt(0)).setTextSize(20);
imageView.setImageResource(images[pos]);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}}
I've used NothingSelectedSpinnerAdapter.java and contact_spinner_row_nothing_selected.xml from this question.
The app builds and runs fine. But here's the problem:
When I select any item in the spinner and press the "Home" button on my phone(with that particular item selected), and then reopen the app after some time(more than 15 minutes or so), the app crashes.
Can someone please help me find a solution for this?

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
}
});

spinner with ItemSelected

i have spinner with array that i create in a string.xml which called array.spinner_title
how can i do the on item select her so i can get the item selected
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( SendTeacher.this,
R.array.spinner_title, 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);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// Do something
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
thanks
As I understand your question , you want to get the selected item.
In:
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
CharSequence item = (CharSequence)adapterView.getAdapter().getItem(position);
}
For custom adapter which can contain any other objects (not only CharSequence), see this link for example

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