Tuesday, December 6, 2016

Custom Spinner using BaseAdapter

Custom Spinner


Here I have implemented custom spinner example using Model class.

Here is my activity_main.xml file


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"    
     xmlns:tools="http://schemas.android.com/tools"    
     android:id="@+id/activity_main"    
     android:layout_width="match_parent"    
     android:layout_height="match_parent"    
     tools:context="example.com.spinnercustom.MainActivity">


    <Spinner        
      android:id="@+id/sprCountry"        
      android:layout_width="match_parent"        
      android:layout_height="wrap_content"/>


</RelativeLayout>

Now we need to create custom layout for spinner.


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"    
  android:orientation="vertical" 
  android:layout_width="match_parent"    
  android:layout_height="match_parent">

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

</LinearLayout> 


After that we have to create  model class for spinner.


public class SpinnerModel implements Serializable {
    
    private String strCountryName;
    
    public String getStrCountryName() {
        return strCountryName;
    }

    public void setStrCountryName(String strCountryName) {
        this.strCountryName = strCountryName;
    }
}

and the code of MainActivity is given below.


public class MainActivity extends AppCompatActivity {

    private Spinner sprCountry;
    private ArrayList<SpinnerModel> alCountry;
    private SpinnerModel model;
    private SpinnerAdapter adapter;


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

        sprCountry = (Spinner) findViewById(R.id.sprCountry);

        alCountry = new ArrayList<>();

        model = new SpinnerModel();
        model.setStrCountryName("Select Coutry");
        alCountry.add(model);

        // set First coutry name

        model = new SpinnerModel();
        model.setStrCountryName("INDIA");
        alCountry.add(model);

        model = new SpinnerModel();
        model.setStrCountryName("USA");
        alCountry.add(model);

        model = new SpinnerModel();
        model.setStrCountryName("CANADA");
        alCountry.add(model);


        adapter = new SpinnerAdapter(MainActivity.this,alCountry);
        sprCountry.setAdapter(adapter);

        sprCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Log.e("You select ","=====> "+alCountry.get(i).getStrCountryName());
            }

            @Override            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        
    }
}


and finally I adapter of spinner is


public class SpinnerAdapter extends BaseAdapter {

    private ArrayList<SpinnerModel> alCountry;
    private Context context;
    private LayoutInflater inflater;

    public SpinnerAdapter(MainActivity mainActivity, ArrayList<SpinnerModel> alCountry) {
        context= mainActivity;
        this.alCountry = alCountry;
    }

    @Override    public int getCount() {
        return alCountry.size();
    }

    @Override    public Object getItem(int i) {
        return i;
    }

    @Override    public long getItemId(int i) {
        return i;
    }

    class Holder{
        private TextView tvCountryName;
    }

    @Override    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View myView = null;
        try {
            Holder holder;
            myView = convertView;

            if (myView == null) {
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                myView = inflater.inflate(R.layout.spinner_layout, null);

                holder = new Holder();
                holder.tvCountryName = (TextView) myView.findViewById(R.id.tvCountryName);
                myView.setTag(holder);
            } else {
                holder = (Holder) myView.getTag();
            }

            holder.tvCountryName.setText(alCountry.get(i).getStrCountryName());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return myView;
    }

    // Below code is used to hide first item.

    @Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    View v = null;
    if (position == intHideIndex) {
        TextView tv = new TextView(context);
        tv.setVisibility(View.GONE);
        tv.setHeight(0);
        v = tv;
        v.setVisibility(View.GONE);
    }
    else        v = super.getDropDownView(position, null, parent);
    return v;

}
}




16 comments:

  1. What about that intHideIndex? where is it defined? What am I missing here?

    ReplyDelete
  2. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  3. I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
    python training institute in chennai
    python training in velachery
    python training institute in chennai

    ReplyDelete
  4. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai
    Devops Training in Bangalore

    ReplyDelete
  5. This is very good content you share on this blog. it's very informative and provide me future related information.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    ReplyDelete
  6. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    selenium training in chennai
    selenium training in bangalore
    selenium training in Pune
    selenium training in pune
    Selenium Online Training

    https://sqldbawalkin.blogspot.com/2011/09/training-and-placement-to-dba-pune.html

    ReplyDelete
  7. Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  8. Nice blog..! I really loved reading through this article. Thanks for sharing.You done a great job.Automation Anywhere Training in Bangalore






    ReplyDelete
  9. We are well established IT and outsourcing firm working in the market since 2013. We are providing training to the people ,
    like- Web Design , Graphics Design , SEO, CPA Marketing & YouTube Marketing.Call us Now whatsapp: +(88) 01537587949
    Digital marketing training
    good post Mobile XPRESS
    Free bangla sex video:Delivery Companies in UK
    good post Mobile XPRESS

    ReplyDelete
  10. Some may stag in Interviews!!! OOPS!! More than 50% of students do this in their career. Instead, do Hadoop Training in Chennai at Infycle. Those students can easily clear this Interview session because more than 5 times at INFYCLE practicing mock-interview sessions, Hence students are Getting out of their interview fear.

    ReplyDelete