Saturday, 1 December 2012

Android Custom Spinner with Intent


          Android Custom Spinner with Intent  


 Following is a example program for custom spinner in android and user can select a particular item and leads to specific activity.


NewspinnerActivity.java

package adsdfd.ghgh;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
 
public class NewspinnerActivity extends Activity {
    String[] strings = {"CoderzHeaven","Google",
            "Microsoft", "Apple", "Yahoo","Samsung"};
 
    String[] subs = {"Heaven of all working codes ","Google sub",
            "Microsoft sub", "Apple sub", "Yahoo sub","Samsung sub"};
 
 
    int arr_images[] = { R.drawable.icon,
                         R.drawable.icon, R.drawable.icon,
                         R.drawable.icon, R.drawable.icon, R.drawable.icon};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        mySpinner.setAdapter(new MyAdapter(NewspinnerActivity.this, R.layout.row, strings));
    }
 
    public class MyAdapter extends ArrayAdapter<String>{
 
        public MyAdapter(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }
 
        @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) {
          System.out.println("abcdddddddddddd"+position);
          
          
          Intent i=new Intent(getApplicationContext(),abcd.class);
          startActivity(i);
          
          
          
            return getCustomView(position, convertView, parent);
            
        }
 
        public View getCustomView(int position, View convertView, ViewGroup parent) {
 
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.company);
            label.setText(strings[position]);
 
            TextView sub=(TextView)row.findViewById(R.id.sub);
            sub.setText(subs[position]);
 
            ImageView icon=(ImageView)row.findViewById(R.id.image);
            icon.setImageResource(arr_images[position]);
 
            return row;
            }
        }
   }

then following are the xml  file and in main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
      android:background="@drawable/icon"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawSelectorOnTop="true"
    android:prompt="@string/prompt"
    />
</LinearLayout>


then the row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="3dip"
>
    <ImageView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/icon"/>
    <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="3dip"
         android:layout_marginTop="2dip"
         android:textColor="@drawable/red"
         android:textStyle="bold"
         android:id="@+id/company"
         android:text="CoderzHeaven"
         android:layout_marginLeft="5dip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="2dip"
         android:textColor="@drawable/darkgrey"
         android:layout_marginLeft="5dip"
         android:id="@+id/sub"
         android:layout_below="@+id/company"
         android:text="Heaven of all working codes"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
</RelativeLayout>


abcd.class

package adsdfd.ghgh;

import android.app.Activity;
import android.os.Bundle;

public class abcd extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abcd);
}

}







Download Android Codes From SkyBlueAndroid

No comments:

Post a Comment