Wednesday 14 May 2014

How to set custom font, Font size, Font color and Text background for spinner in android

Customize your spinner as you like with simple steps. Change your font color and size of your spinner text and even background of your text. Make your app look your way.

You will go through some simple steps to make changes to your android spinner's font color, font size, background color and font type.


For customizing your font type for 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"
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="44dp" />
</RelativeLayout>


Step 2: In your layout file create two android xml layout file.

First : 
selected_item.xml with TextView.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#FF5050"
android:textSize="30sp">
</TextView>


Second :
drpdown_item with CheckedTextView.

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="#0099CC"
android:textSize="30sp"
android:background="#4d4d4d" >
</CheckedTextView>


Step 3: This is your main activity class according to your need call the method.
MainActivity.java:

package com.exam.customspinner;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

String list[]={"Jan","Feb","March","April"};

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

/* Call the method you wish */

spinnermeth();
// spinnermeth1(); // Make changes to those two file to get desired result
}

public void spinnermeth(){
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.selected_item, list);
mySpinner.setAdapter(adapter);
}

public void spinnermeth1(){
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.selected_item, list);
adapter.setDropDownViewResource(R.layout.dropdown_item);
mySpinner.setAdapter(adapter);
}
}


No comments:

Post a Comment