Skip to main content

Customized Spinners in Android

See more basics on Android along with interview questions

The Android Spinner Control is definitely the equivalent of the drop-down selector . By making use of spinner control you basically obtain the capability to choose from a list without taking up all of the display screen space of a ListView.

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.

You can add a spinner to your layout with the Spinner object. You should usually do so in your xml layout with a  <spinner>  element. For example:
 <Spinner
    android:id="@+id/myspinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" /> 

Here we are going to deal with Customized Spinners.Here we go with java code samples.

in java file:
public class Custom_spinner extends Activity {
	String[] data1 = { "Java", "Android","Blackberry", "Apple", "Windows" };
	
	String[] data2 = { "Developed by Sun Microsystems", "Android is a Linux-based operating system",
			"Designed and marketed by Research In Motion Limited ","Designed and marketed by Apple Inc" 
	        ,"developed, marketed, and sold by Microsoft."};
	
	Integer[] images = { R.drawable.java, R.drawable.android,
			R.drawable.blackberry, R.drawable.apple, R.drawable.windows };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_spinner);
		Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
		mySpinner.setAdapter(new MyAdapter(this, R.layout.cus_row, data1));
	}

	public class MyAdapter extends ArrayAdapter<String> {

		public MyAdapter(Context context, int textViewResourceId,
				String[] objects) {
			super(context, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
		}

		@Override
		public View getDropDownView(int position, View convertView,
				ViewGroup parent) {
			return getCustomView(position, convertView, parent);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return getCustomView(position, convertView, parent);
		}

		public View getCustomView(int position, View convertView,
				ViewGroup parent) {

			LayoutInflater inflater = getLayoutInflater();//inflate xml for each row
			View row = inflater.inflate(R.layout.cus_row, parent, false);
			TextView label = (TextView) row.findViewById(R.id.textView1);
			label.setText(data1[position]);

			TextView sub = (TextView) row.findViewById(R.id.textView2);
			sub.setText(data2[position]);

			ImageView icon = (ImageView) row.findViewById(R.id.imageView1);
			icon.setImageResource(images[position]);

			return row;
		} }}

in custom_spinner.xml (main xml file) :
<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:padding="10dp" >
    <Spinner
        
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:background="@drawable/purple"
        >
    </Spinner>
</RelativeLayout>
in cus_row.xml (inflated xml file) :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="3dip" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" >
        </ImageView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="70dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TextView>
    </LinearLayout>

</RelativeLayout>
@drawable/purple.xml :


    
            

            

            
        
    
            

            

            
        

Comments

Popular posts from this blog

Use Your Own SQLite in ANDROID Application or Using 'SQLite Asset Helper' to read your own databases stored in Asset folder

See more basics on Android along with interview questions See SQLite tutorial here If you want to read your own databases in ANDROID APPLICATION, this post may help you. You may be familiar by creating SQLite in Android use  SQLiteOpenHelper . Here we are going to deal with using our own databases.  I have a database named 'mydatabase.sqlite', which consist of a set of name and age of 5 students. Suppose I want to view the database, I can use SQLite database browser which you can download from here.                                                     download browser..... Through this browser we can view the databases, can even modify delete or create a new one. 'mydatabase.sqlite' with name and age of five students is browsed and viewed as shown below Now  make a zip format of database and copy this database to asse...

Spannable String in Android - URL Span ,Clickable Span, Rich-Style Formatting of Textview .....

See more Android Tutorials here....... Faster Loading images in GridViews or ListViews Spannable brings lots of possibility to TextView, includes displaying various appearance of a Text and onClick callbak. The SpannableString class allows you to easily format certain pieces which are called spans of a string, by applying CharacterStyle ie,color, font, ormake it a link . Here is an example where, explained how to use spannable string to give font size, color, linking a text via clickable span and through URL Span and to strike through the text. Lets go through the example : import android.os.Bundle; import android.text.SpannableString; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.text.style.StrikethroughSpan; import android.text.style.URLSpan; import android.view.View; import android.widget.TextView; import android.widget.Toast; ...

Passing Images between Activities in Android

in First Activity: Intent intent=new Intent(FirstClass.this, SecondClass.class); Bundle bundle=new Bundle(); bundle.putInt("image",R.drawable.ic_launcher); intent.putExtras(bundle); startActivity(intent); in Second Acticity: Bundle bundle=this.getIntent().getExtras(); int pic=bundle.getInt("image"); v.setImageResource(pic); another method: in First Activity: Drawable drawable=imgv.getDrawable(); Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); Intent intent=new Intent(Passimage.this,myclass.class); intent.putExtra("picture", b); startActivity(intent); in Second Acticity: Bundle extras = getIntent().getExtras(); byte[] b = extras.getByteArray("picture"); Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.lengt...