Thursday 30 January 2014

How to set custom font for spinner in android

To set custom font for spinner we need to create our own SpinnerAdapter with our Typeface for getView and getDropDownView().

You will get your font style for spinner in just three simple steps.

To make other customization to your spinner check this link







Step 1: Just create a spinner in your layout

activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>

Step 2: Create a folder in Assets and name it as fonts. Then paste your typeface (fontname.ttf) file in fonts folder.

Step 3: create a ViewGroup spinner method and call the method.
MainActivity.java:


package com.exam.spintypeface;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

String list[]={"ϪÂah","jäH‹"};
Typeface tfavv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner2meth();
}

public <ViewGroup> void spinner2meth(){
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list)
{
public View getView(int position, View convertView, android.view.ViewGroup parent) {
tfavv = Typeface.createFromAsset(getAssets(),"fonts/Avvaiyar.ttf");
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(tfavv);
v.setTextColor(Color.RED);
v.setTextSize(35);
return v;
}

public View getDropDownView(int position, View convertView, android.view.ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(tfavv);
v.setTextColor(Color.RED);
v.setTextSize(35);
return v;
}
};

adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I have tried many other variations of custom spinners from other sites and this is the only one I could get to work exactly the way I needed it to.

    Thank you very much for this post as it has helped me out greatly!

    ReplyDelete
    Replies
    1. Great! Glad to hear that :)
      Good luck Randy.

      Delete
  3. Thanks for the great help :)

    ReplyDelete
  4. Thank you. you code is help me.

    ReplyDelete
  5. Really nice code , thanks ...

    ReplyDelete
  6. Appreciate it, this was very helpful!

    ReplyDelete
  7. Thanks a lot .You saved my day.

    ReplyDelete
  8. Thank you so much !! it worked really well

    ReplyDelete