I am working on a project where I have to implement a spinner. My spinner code is below:
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, CatagoryName);
// create a spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// add adapter to spinner
spinner.setAdapter(stringArrayAdapter);
// create listener and add to spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// put code which recognize a selected element
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Basically, I want to use my custom spinner layout at
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, CatagoryName);
R.layout.simple_spinner_dropdown is not what I want. How to replace this layout with mine?
this is worked for me,
firstly define spinner items in strings.xml which in values directory as above,
<string-array name="spinner_array">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
then try above code,
Spinner customSpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_array, R.layout.custom_spinner_dropdown);
customSpinner.setAdapter(adapter);
Try this approach:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
String[] CatagoryName = {"A","B","C","D"};
ArrayAdapter<CharSequence> langAdapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.your_layout, CatagoryName);
langAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown);
spinner.setAdapter(langAdapter);
Related
I am trying to set and get value to a spinner for item dynamically ?
any ideas ?
I just need help with the Spinner behavior right now, the rest should be quite easy.
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add("value");
spinnerAdapter.notifyDataSetChanged();
spinner.setSelection(0);
String text = spinner.getSelectedItem().toString();
1
2
3
XML file:
<Spinner android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Java file:
public class SpinnerExample extends Activity {
private String[] arraySpinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.arraySpinner = new String[] {
"1", "2", "3", "4", "5"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);
}
// To get value from spenner
spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView parent) {
}
});
}
Try this this will help you.
Spinner mySpinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(myAdapter);
If you want to add the elements dynamically, you can by doing this:
myAdapter.add("newelement");
myAdapter.notifyDataSetChanged();
Spinner spinn = findViewById(R.id.socialmedia_spinner_adsocmeda);
ArrayList<String> fam = new ArrayList<>();
fam.add("INSTAGRAM");
fam.add("FACEBOOK");
fam.add("GOODWALL");
fam.add("TWITTER");
fam.add("YOUTUBE");
fam.add("LINKEDIN");
fam.add("SNAPCHAT");
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (ProfileActivity.this,
android.R.layout.simple_list_item_1, fam.toArray(new String[0]));
spinn.setAdapter(myAdapter);
That's a way we can add data to spinner.
I have an issue of loading view dynamically. I have three dynamic spinner view when i select first item from the spinner it should create a text view above second spinner but its creating a view below third spinner. Can any one guide me how to achieve it.I have shared my code below.
/add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
rl.setOrientation(LinearLayout.VERTICAL);
// add spinner
String[] spinnerArray = {"Android","ios","other"};
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
String[] spinnerArray1 = {"flowers","vegetables","other"};
Spinner spinner1 = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray1); //selected item will look like a spinner set from XML
spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(spinnerArrayAdapter1);
String[] spinnerArray2 = {"cat","dog","other"};
Spinner spinner2 = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray2); //selected item will look like a spinner set from XML
spinnerArrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(spinnerArrayAdapter2);
rl.addView(spinner);
rl.addView(spinner1);
rl.addView(spinner2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(position==1)
{
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
rl.addView(tv);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Do you know how LinearLayout works?
http://developer.android.com/reference/android/widget/LinearLayout.html
You should call
addView(tv, 1);
Specifying position on where to insert view.
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
}
});
I am trying to populate entries in a Spinner based on a selected value in another Spinner.
Spinner spin1 = (Spinner) findViewById(R.id.Spinner1);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object Obj = parent.getItemAtPosition(pos);
String str = Obj.toString();
Log.i(str, str);
if (str == "B") {
Spinner spin2 = (Spinner) findViewById(R.id.Spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getApplicationContext(), R.array.array1,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter);
} else if (str=="M") {
Spinner spin2 = (Spinner) findViewById(R.id.Spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getApplicationContext(), R.array.array2,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter);
}
}
I have assembled this code after going through different forums. But this is not working for me. The Log.i is giving me the correct city as selected but the second spinner is not getting populated with the new array.
You can not compare strings using ==, and your condition if(str == "B") will never be true.
Try using equalsIgnoreCase or equals
if(str.equals("B"))
{
//populate second spinner
}
I'm trying to set the index of the item that needs to be selected in the spinner by default, but it always defaults to 0 (1st item)
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter spinnerAdapter =
new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item,
names);
int selectedIndex = actionBar.getSelectedNavigationIndex();
if (selectedIndex != targetIndex) {
actionBar.setSelectedNavigationItem(targetIndex);
}
Above if block is called always. Even after setting index 2, next time I check it returns 0.
Edit: I suspect getSelectedNavigationIndex gives index of actionBar item rather than Spinner dropdown item. If that is the case, what method sets the index of selected item inside dropdown?
Make sure you call setListNavigationCallbacks method before changing selected element. I can't see it in your example, so I think that's the problem.
Here is an example:
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);
It works in my app without any problems.
Have you tried using shared prefences to save the value of the selected spinner. I used this code to save the users selection with shared preferences so the next time they opened the app the spinner was set to the last value they chose:
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = getSpinnervalues();
ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, makes);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(spinnerAdapter);
int position = Utils.getIndex(getSpinnerval(), makes);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selected = (String) parentView.getSelectedItem();
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = Filter.this.getSpinnerval(selected);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(Filter.this, android.R.layout.simple_spinner_item, models);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(adapter);
}
public void onNothingSelected(AdapterView<?> parentView) {}
});
Spinner.setSelection(makeposition, true);
int position = Utils.getIndex(getSpinnerval());
if (position >= 0) {
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
Spinner.setSelection(position, true);
}
Then the get Util:
public String getSpinnerval() {
return getSharedPreferences().getString("val", "");
}
I altered the code a little so it might not be 100% right but may give you an idea.