Open Text plain when spinner option get selected - android

i got a spinner with some options, and i want to change visibility of text box when the specific "other" option get selected , how should i do this ?
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);

Try this:
.................
........................
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
// Views
categories= (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
// Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
// Set onItemSelected listener to spinner
categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView spinnerText = (TextView) view;
// Checking weather selected item is "Other" or not
if (spinnerText.getText().equals("Other"))
{
// Hide TextView
textView.setVisibility(View.INVISIBLE); // or View.GONE
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
.............
...................
Hope this will help

you can Implement OnItemSelected in the spinner and do the things what you want.
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(arraySpinner!=null && arraySpinner[position].equalIgnoreCase("Other")){
// your code here
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

Related

android :populating spinner based on another spinner selection

spinner is not populating based on the another spinner selection , I have been on this for hours , still can't get the issue , No error in LOGCAT. Thanks in advance.
This is my code
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, SPINNERCOUNTRY);
spinnerCountry = (MaterialBetterSpinner) findViewById(R.id.spinnerState);
spinnerCountry.setAdapter(adapter);
spinnerCity = (Spinner) findViewById(R.id.spinnerCity);
spinnerCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
adapterView.getItemAtPosition(pos);
if (pos == 0) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(SpinnerStateCitySelectorActivity.this, R.array.city_Tamil_Nadu,
android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
} else if (pos == 1) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(SpinnerStateCitySelectorActivity.this, R.array.city_Kerela,
android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
} else if (pos == 2) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(SpinnerStateCitySelectorActivity.this, R.array.city_Karnataka,
android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
You may try it with value of selected item, using onItemSelectedListner
yourMainSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String spinnerValue = yourMainSpin.getSelectedItem().toString();
if (spinnerValue.equals("requierdString")) {
subSpinner.setAdapter(yourAdapter);
}else if (spinnerValue.equals("requierdString")){
subSpinner.setAdapter(yourAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Hope this will Help you.
User onItemClickListener instead of setOnItemSelectedListener

I want to display selected item with some title in Spinner,Like item is:item6

I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

Item selection in spinner doesn't detect in MyOnItemSelectedListener

I have activity with 3 spinners and selection of 1st spinner is related to 2nd and 2nd selection is related to the 3rd, so I have the spinner code one inside one.
CrustSp, SizeSp and extraDescriptionOneSP are the 3 spinners I have. I have loaded the items to the Spinners and I can select a value as well. But my problem is whenever i made a selection in CrustSp and SizeSp doesn't show in MyOnItemSelectedListener(). But if I made a selection in extraDescriptionOneSP it shows inside MyOnItemSelectedListener().
I can't figure where I have gone wrong, anyone can point out where I have gone wrong it will be really helpful.
This the code
crust = Utils.removeDuplicatesFromList(crust);
ArrayAdapter<String> dataAdapterCru = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, crust);
dataAdapterCru
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
crustSP.setAdapter(dataAdapterCru);
crustSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String crustSelectedItem = crustSP.getSelectedItem()
.toString();
List<String> resultSizes = getFilteredSizes(crustSelectedItem);
ArrayAdapter<String> dataAdapterDes = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultSizes);
dataAdapterDes
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSP.setAdapter(dataAdapterDes);
sizeSP.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this) {
public void onNothingSelected(AdapterView<?> arg0) {
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String sizeSelectedItem = sizeSP
.getSelectedItem().toString();
List<String> resultTopping = getFilteredToppings(sizeSelectedItem);
extraDescriptionOneSP
.setOnItemSelectedListener(new MyOnItemSelectedListener(
ActivityPizzaCustomize.this));
ArrayAdapter<String> dataAdapterExtraDesOne = new ArrayAdapter<String>(
ActivityPizzaCustomize.this,
android.R.layout.simple_spinner_item,
resultTopping);
dataAdapterExtraDesOne
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
extraDescriptionOneSP
.setAdapter(dataAdapterExtraDesOne);
extraDescriptionOneSP
.setAdapter(new NothingSelectedSpinnerAdapter(
dataAdapterExtraDesOne,
R.layout.contact_spinner_row_nothing_selected,
ActivityPizzaCustomize.this));
}
});
}
});
MyOnItemSelectedListener method,
public class MyOnItemSelectedListener implements OnItemSelectedListener {
Activity activity;
String StrOneSPPosition;
public MyOnItemSelectedListener(Activity p_activity) {
activity = p_activity;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// check which spinner triggered the listener
switch (parent.getId()) {
case R.id.sp_crust: {
String StrCrustSPPosition = String.valueOf(crustSP
.getSelectedItemPosition());
System.out.println("crust = " + StrCrustSPPosition);
System.out.println("crust = " + position);
CrustSPPosition = Integer.parseInt(StrCrustSPPosition);
}
I don't see you using the selected position to determine the data for next spinner. BTW you should not make new adapter for each selection, instead reuse the once made adapter.

not able to change the color of item in spinner

I have add two spinners in my code. But bydefault the item of the spinner is displayed inblack color. I want to change the color of the spinner item.I have add the color in simple_spinner_item.xml file. but color is not changed. I have pasted my code here.plz help me...
public class Alert_activity extends Activity implements OnItemSelectedListener{
Spinner s1,s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_activity);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String sp1= String.valueOf(s1.getSelectedItem());
if(sp1.contentEquals("Events")) {
List<String> list = new ArrayList<String>();
list.add("Heavy Rain(mm/hr)");
list.add("Smoke/Fog");
list.add("Heat/Cold Wave");
list.add("Strong Winds");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
Log.d("Item",item);
FragmentTransaction tx = getFragmentManager() .beginTransaction();
if(item=="Heavy Rain(mm/hr)")
{
tx.replace(android.R.id.content, new heavyrain());
tx.commit();
}
});
}
if(sp1.contentEquals("Forecast")) {
List<String> list = new ArrayList<String>();
list.add("Heavy Rain(mm/hr)");
list.add("Cyclone");
list.add("Cloudburst");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
String item = adapter.getItemAtPosition(position).toString();
Log.d("Inside loop","inside loop");
FragmentTransaction tx = getFragmentManager() .beginTransaction();
if(item=="Cyclone")
{
tx.replace(android.R.id.content, new Cyclone());
}
else if(item=="Heavy Rain(mm/hr)")
{
tx.replace(android.R.id.content, new forecast_heavyrain());
}
else if(item=="Cloudburst")
{
tx.replace(android.R.id.content, new forecast_cloudburst());
}
tx.commit();
}
});
}
}
simple_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textColor="#FFFFFF"/>
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
or refer this link:How to change spinner text size and text color?
if your simple_spinner_item.xml contains the background color for your spinner item then use
dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item);
instead of dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
similarly,
dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item); instead of dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
you might also have to change the layout set in the ArrayAdapter to a custom layout to solve the issue.

multiple spinner's simple doubts

Im new to android. i have tried to built an application which has 2 spinner...
i have few doubts to clear before going deep to my application..
please give brief answer to my question...
is it possible to have 2 spinners in one view?
can i add setOnItemSelectedListener(this) for both?
if yes, how could i know which option is selected on both the spinners? like spinner.getSelectedItemPosition(); on both the spinners?
waiting for ur valuable replies. reference materials are also appreciable
Yes, you can have multiple spinner's that can be managed by single listener or you can have seperate Listener for each.
For Single Listener you can do something like this,
Set the Listener
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
Then you can use onItemSelected to get perform the action related to the spinner's.
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long arg3)
{
int id = parent.getId();
switch (id)
{
case R.id.spinner1:
// your stuff here
break;
case R.id.spinner2:
// your stuff here
break;
}
Check this:
Spinner spinner0, spinner1, spinner2;
TextView textView0, textView1, textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView0 = (TextView) findViewById(R.id.text0);
spinner0 = (Spinner) findViewById(R.id.spinner0);
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text0);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(adapter0);
spinner0.setOnItemSelectedListener(onItemSelectedListener0);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setSelection(0,false); //make sure only one listener is
//active in the onCreate method by setting the other listener as false
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
textView2 = (TextView) findViewById(R.id.text2);
spinner2 = (Spinner) findViewById(R.id.spinner2);
MySpinnerAdapter adapter2 = new MySpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_item, obj2);
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(0,false);
spinner2.setOnItemSelectedListener(onItemSelectedListener2);
}
OnItemSelectedListener onItemSelectedListener0 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner0
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener1 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner1
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener2 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner2
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
If the reference of two spinners are different(which will be , if you are using 2 spinners) , give different IDs for them and you can uniquely identify them, Yes you can set click listener to both of them individually.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
yes
yes
spinner1.getSelectedItemPosition(), spinner2.getSelectedItemPosition()

Categories

Resources