EagleOne EagleOne - 23 days ago 8
Android Question

Text in a spinner changes height and width selecting another item of the spinner

I wrote this code for an Android spinner:

<Spinner
android:id="@+id/lstCommunity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/community_string"
android:layout_alignLeft="@+id/txtName"
android:layout_below="@+id/txtName"
android:minHeight="0dp"
android:textSize="10sp"
android:background="@drawable/box" />


But the text in the spinner changes is height and width after selecting another item in the list.

So, initially it is a bit smaller than the height and width of the spinner, after selecting another item, it becomes really bigger than the spinners size.

How should i change my code?

FIRST EDIT:
I am trying to use the spinner in order to choose some text from a list.

(I can't post an image)

The text initially fits the spinner but after selecting another option in the dropdown menu the text becomes greater than the spinner box.

EDIT 2:
Java code related to the spinner:

Spinner mySpinner = (Spinner) findViewById(R.id.lstCommunity);
ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(this, R.array.array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);

Answer

1) Creare spinnertext.xml in res/layout

For example:

  xml version="1.0" encoding="utf-8"?>
 <TextView xmlns:android="http://schemas.android.com/.    apk/res/android"
  android:text="@+id/TextView01"
  android:id="@+id/TextView01"
  android:layout_width="wrap_content"   
  android:layout_height="wrap_content"
  android:textSize="12sp"
  android:textColor="#FFFFFFFF"
  android:gravity="center"/>

2)Declare the spinner in the file main.xml (in res\layout), to manage its size :

  <Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="32dip"
    android:drawSelectorOnTop="true"
    android:prompt="@string/spinner_prompt"
     />

3) Declare the spinner in the activity

 Spinner s = (Spinner) findViewById(R.id.spinner);
  ArrayAdapter adapter =   ArrayAdapter.createFromResource(this, R.array.sensors, R.layout.spinnertext);      
   adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
 //when an item is selected call the selectSensor method
s.setOnItemSelectedListener(new OnItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView parent, View view,     int position, long id) {
        selectSensor(parent.getItemAtPosition(position));
  }
  @Override
        public void onNothingSelected(AdapterView arg0) {
        }
     });

Notice the change in the createfromresource function.

Source: http://android2ee.blogspot.it/2011/11/spinner-managing-size-and-text.html

Comments