Search code examples
androidandroid-layoutspinnerandroid-spinner

Changing the text size of a spinner


I am a beginner in Android programming.

I am trying to change the text size of the spinner as mentioned in some question here. I have created my_spinner_style.xml in res/values/ as below:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerTarget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="13sp"
/>

and used an adapter like this:

spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.answers,android.R.layout.my_spinner_style);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);

and also tried creating style.xml in the layout folder. My adapter does not recognize my_spinner_style. Have I placed it in the wrong directory?


Solution

  • It should be R.layout.my_spinner_style, remove the android at the start, that makes it point to the Android package and not your project.

    Note the R you're importing is your project's Resources, saying android.R points to Android resources, not your project.

    // Edit this is how you have sizes based on the screen size

    dimens.xml This is under values (default):

    <resources>
    
    <!-- Font sizes in SP -->
    <dimen name="small_font">14sp</dimen>
    <dimen name="big_font">20sp</dimen>
    
    </resources>
    

    dimens.xml this is under my values-fr (French, in your case, you'll have screen size qualifiers)

    <resources>
    
    <!-- Font sizes in SP -->
    <dimen name="small_font">16sp</dimen>
    <dimen name="big_font">22sp</dimen>
    
    </resources>
    

    Then in my TextView for example, this is my font size:

    <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/small_font">
    
        </TextView>
    

    Now the font size is dependent on the language (English goes to values and French goes to values-fr) You should have the same thing but using screen size qualifiers.