Hai i developed a application in android,in my application i used a dynamically created spinner(many spinner),the width in 320
problem
when 1 spinner text is like"aaaa",2 spinner text is like "a".the two spinner width is varied.i need the same width in all spinner.how to do?Anybody kindly solve my problem.
Spinner spinner = new Spinner(this);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
you need to set width attribute of the spinner.I think it is "set to wrap content".You should provide width in dp.Foe ex-100dp
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
yourspinner.setLayoutParams(params);
you can also give height and width in DP. Check this method.
Try to create the spinner in XML layout with the width value and set visibility "GONE".
When you have to use it, you can retrieve it by using findviewbyid method and then you have to set the visibility at VISIBLE.
Spinner spinner = (Spinner)findViweById(R.id.spinnerInvisible);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setVisibility(View.VISIBLE);
Related
I have an activity that recieves two variables called l_source and l_destination and I want to set the default values of two spinners equal to those variables. To do so, I have made a search and I understand that I need to get the position of the value of the variable (will give a number) and then set the new position of the spinner. The problem is that I am trying to use getAdapterPosition and I am sent Cannot resolve method getAdapterPosition(l_source).
EDIT
I finally managed to do it, but it is only working in one spinner. Have you got any idea of why? Here is my code:
Spinner spinner = (Spinner) findViewById(R.id.lista_origen);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getPosition(l_source));
Spinner spinner2 = (Spinner) findViewById(R.id.lista_destino);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Main2Activity.this,
R.array.l_source, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(adapter2.getPosition(l_target));
Thank you for your time.
get position of selected item is spinner by int spinnerPosition = spinner.getSelectedItemPosition();
I have a spinner that's populated like this:
Spinner spinner = (Spinner) findViewById(R.id.m_spinner);
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, spinnerValuesList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setPrompt("CPU Frequency Governor");
It's created like this:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="#+id/my_spinner"/>
Everything works fine, but I'd like to align the text of the dialog title and the spinner items. I attached a picture to show what it currently looks like and what I'm looking to achieve.
Thanks
Instead of
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, spinnerValuesList);
Try this:
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1
, spinnerValuesList);
Let me know if it works for you.
I have created a Spinner dynamically, which is easy with new Spinner(context).
But now I need to set the spinnerMode dynamically, and I find no method to do so.
What should I do?
in android >= 3.0 (API 11) you can use this :
Spinner spinner = new Spinner(this,null,android.R.style.Widget_Spinner,Spinner.MODE_DROPDOWN);
OK you can try with..
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yourArrayAdapter);
spinner.setAdapter(spinnerArrayAdapter);
Or,
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, yourArrayAdapter);
spinner.setAdapter(spinnerArrayAdapter);
Also you can see..
How to set spinnerMode in Android 2.2?
I've been trying to solve a problem which i been having for past week , i am trying to a access spinner inside different xml layout, all i want is to access the spinner and add array to the spinner.
This is the code i am using and it doesn't work
setContentView(R.layout.mainreg);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout_id);
View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,false);
Spinner spin = (Spinner) view123.findViewById(R.id.spinnerproblem);
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
mainLayout.addView(view123);
pass true for attach to root while inflating layout, like this.
View view123 = getLayoutInflater().inflate(R.layout.one2reg, mainLayout,true);
Hi friends wanna remove the radio button appearing on spinner.. Which is increasing the layout size of my dialog.. Thanks in advance..
set your spinner's adapter as follow:
adapterSpinner = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[]{"1", "2"});
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
For this you need to change your Spinner Style
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Registration.this,android.R.layout.simple_spinner_item, Your Array);
spinner.setAdapter(adapter);