I followed steps in how to change value of textview according to selected item and here is a piece of the code
public void onCreate(Bundle savedInstanceState){.......
final TextView privacyTextView = (TextView) findViewById(R.id.eventPrivacy);
privacySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id3) {
final String selectedItem = parent.getItemAtPosition(position).toString();
privacyTextView.setText(selectedItem);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
list is :
<string-array name="privacy_levels">
<item>Everyone</item>
<item>Friends of Friends</item>
<item>Friends Only</item>
<item>Customize</item>
</string-array>
I have text view with value : privacy when running app it's automatically changed to Everyone -frist one on the list- so what is going wrong ?!!!
Add android:prompt in Spinner xml and set a name like
android:prompt="Privacy";
So first time it will show privacy then user can select from the drop down list.
Try this...!
final String selectedItem = parent.getSelectedItem().toString().trim();
privacyTextView.setText(selectedItem);
If you want to get first item as privacy then you need to add another item in your array as Privacy.
There is a attribute called android:spinnerMode this is used to get the spinner in two modes as dialog or dropdown.
where as the attribute android:prompt is to get the spinner Title or Heading after opening it(in Dialog mode).
Just try to this so you will get selected string:
String name = spinner.getSelectedItem().toString();
Related
What I want to do is set the texts depends on the spinner item selected by users. But there is nothing that showed up when running the app and there is no errors either. Did I miss any steps that cause the outcome didn't come out?? I'm still new to android.
UPDATE:
I forgot to say that I have three different spinners depending on which button users liked in 1st activity so I think its because I only declare for the spinner's item but didn't declare which spinner is "Japan" referring to.
So this is my code for 1st activity, I will just put only one first :
btnAsia.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
intent.putExtra("continent", "asia");
startActivity(intent);
}
});
This is the string array in strings.xml:
<string-array name="asia">
<item>Japan</item>
<item>Thailand</item>
<item>Vietnam</item>
<item>South Korea</item>
</string-array>
and this is the code in 2nd activity:
private void DestinationSpinner()
{
Bundle bundle=getIntent().getExtras();
if(bundle!=null) {
String continent = bundle.getString("continent");
switch (continent) {
case "asia":
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(MainActivity2.this, R.array.asia, android.R.layout.simple_spinner_item); // Create an ArrayAdapter using the string array and a default spinner layout
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinnerDestination.setAdapter(adapter); // Apply the adapter to the spinner
break; }
and lastly this is the code for retrieve selected item from spinner:
public void AvailableAirlines()
{
spinnerDestination.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected=spinnerDestination.getItemAtPosition(position).toString();
if (continent.equals("asia") && selected.equals("Japan"))
{
availableAirlines.setText("Available airports");
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
and I also want to pass the keyidentifer from 1st activity to 2nd activity but I tried using getExtras and it didn't work maybe I do it wrongly or what.
I think the issue is this line if(selected=="Japan")
In Java you will need to use equals instead of the == operator.
The == checks if the two pointers are pointing to the same memory location.
Equals will compare their content.
I try to explain by showing as many examples as possible, I chose a word from the spinner, for example it is "cat", after I select it in the spinner i.e. after I click it should also appear in the textview.
Spinner click "cat"
TextView change "cat"
I again click Spinner "dog"
TextView change "dog"
now my not full code:
```Spinner sababspinner = findViewById(R.id.Sababspinner);
sababspinner.setOnItemClickListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}```
all spinner texts in string.xml
Make an ArrayList with your spinner dataset, then in the onItemSelected method write something like textView.setText(dataset.get(position)).
I have a profile activity where user select there country for the first time ,after that when user wish to change the country he will go to profile activity there he should see the previous selected country in the spinner and then he must be able to select another country from the spinner.
I am trying to update my profile activity ,I have a spinner named country in it..I am using set selected for getting the previous selected value .i am getting the value but when i am trying to change that value its not happening .
The Country_value is string
Below is the code for spinner :
country_adapter_list = new ArrayAdapter(MyProfile.this, R.layout.support_simple_spinner_dropdown_item,Country_listItems);
country = (Spinner) findViewById(R.id.sp_country);
country.setAdapter(country_adapter_list);
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
country.setSelection(Country_listItems.indexOf(country_value));
country_data = Country_listItems.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
OnItemSelected() method will select the item present in list at position.
The setSelection() will internally call the onItemSelected() to set the selected item in the spinner. So calling setSelection() inside onItemselected() will not help you.
Try below code.
#Override
public void onItemSelected(AdapterView<?> **adapterView**, View view,int position, long l) {
**adapterView**.setSelection(Country_listItems.indexOf(country_value));
country_data = Country_listItems.get(position);
}
Your code is working fine, but it will not set selection on Spinner if "country_value" is not found in arraylist that you set.
Video Link
I found error in findViewById in static function.
my function is--
public static void onYearSelect() {
Spinner yearSelector;
String yearName;
yearSelector = (Spinner) findViewById(R.id.year_submit);
yearName = yearSelector.getSelectedItem().toString();
}
is there any way to solve this error!
can i store value of Spinner in yearName as String
You need to add view inside function as parameter.
public static void onYearSelect(View view) {
Spinner yearSelector;
yearSelector = (Spinner) view.findViewById(R.id.year_submit);
yearName = yearSelector.getSelectedItem().toString();
}
The view which contains your layout data.
When you call that function add onYearSelect(rootView) here rootView have the layout view.
EDIT 1:
What is View here ?
A View in Android is a widget that displays something. Buttons, listviews, imageviews, etc. are all subclasses of View. When you say "change view" I assume you mean change the layout by using setContentView(). This usually only needs to be done once per activity. An Activity is basically what you are referring to as a screen.
You can read more here.
Set setOnItemSelectedListener on spinner object and inside onItemSelected() you can set value of yearName. Like below,
yearSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String yearName = parentView.getItemAtPosition(position).toString()
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}});
I want to change EditText field for each spinner selection, my EditText field should be able to take input in Feet+inch or in Cms based on the input selected by user for unit’s field, i.e. if User selects Metric System then EditText should change into cm format, for FPS system the EditText should change to Feet+inch format, something like following image
I guess I have to use onClickListener on spinner and then have to change EditText, but I don't know how to do that.
you should use this interface with your spinner. the 'position' corresponds to the spinner array item position, it your spinner array is ["inch","cm"] then case 0 correspond "inch" and 1 is "cm".
I hope it will help.
//set spinner listener
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
//TODO change EditText
break;
case 1:
//TODO change EditText
break;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Thank you for your time I got what I wanted , I just used a workaround,
I deploeyd three feilds Ft,In and cms; Ft for feet , In for inches and cms for centimeters, In spinner ft+in , and cms are two options, I set the visibility of non selected option as VISIBLE.GONE.