Welcome Android Developpers

Welcome Android Developpers
My name is Mathias Séguy, I am an Android Expert and Trainer and created the Android2EE company.

For full information, it’s here : Android2EE's Training
You can meet me on Google+, follow me on Twitter or on LinkedIn

Monday, November 21, 2011

Spinner, Managing size and text


Bandeau Conference

Hello,
Sometimes, when using a spinner, you want to manage its size and the font of the displayed text.
To do that you need to do 3 actions (I do that change on the Android spinner example : http://developer.android.com/guide/tutorials/views/hello-spinner.html
1.      Create a spinnertext.xml in your layout folder and define how the spinner’s title has to be displayed
2.      Change your spinner size in your main.xml (where your spinner is defined)
3.      Change your spinner declaration in your activity

The file spinnertext.xml creates in the res\layout folder :
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"/>

The declaration of 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"
    />

And the declaration of the spinner in the Activity (in yellow highltight the change to do):
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) {
            }
});



So, Thanks who?
Thanks, Android2ee, the Android Programming Ebooks :o)

Mathias Séguy
mathias.seguy.it@gmail.com
Auteur Android2EE
Ebooks to learn Android Programming.
AndroidEvolution
Retrouvez moi sur Google+
Suivez moi sur Twitter
Rejoignez mon réseau LinkedIn ou Viadeo

8 comments:

  1. pls make your blog proper user readbale, coz i cant able to read right hand side all blue links.

    ReplyDelete
  2. Is it better?
    For the right side, the external background don't want to change... I don't know why, it should be gray (almost white).

    ReplyDelete
  3. How is change spinner prompt textSize,textColor and backgroundcolor.

    ReplyDelete
  4. You define every thing in res\layout\spinnertext.xml as explained

    ReplyDelete
  5. Hi

    i wish to change the style for this line

    android:prompt="@string/spinner_prompt"

    in strings.xml i have to wrote below code:
    Choose a status

    Here i wish to change the textSize,textColor and background color for Choose a Status text message.

    the spinnertext.xml having one textview.that textview change the style for spinner items only.i wish to change the style for spinner prompt message .

    ReplyDelete
  6. Should try somethin like that:

    <resources>

    <style name="Theme.Light.JCertif" parent="android:style/Theme.Holo.Light">
    <item name="android:dropDownItemStyle">@style/Widget.DropDownItemLight</item>
    </style>

    <style name="Theme.Light.JCertif.preHC" parent="android:style/Theme.NoTitleBar">
    <item name="android:dropDownItemStyle">@style/Widget.DropDownItemLight</item>
    </style>

    <style name="Widget.DropDownItemLight" parent="@android:style/Widget.DropDownItem">
    <item name="android:textColor">@color/deep_blue</item>
    <item name="android:textSize">@dimen/textSize</item>
    <item name="android:textStyle">italic</item>
    </style>

    </resources>

    ReplyDelete
  7. ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.sensors, R.layout.spinnertext);

    Can you please explain why R.array.sensors was used?

    ReplyDelete