RSS

How to create ActionBarSherlock spinner dropdown multicheckitem

20 Feb


We will be using the same layout (i.e. R.layout.sherlock_spinner_item) which is present in sherlock for ArrayAdapter
Where as we will create new layout R.layout.sherlock_spinner_dropdown_multicheckitem in sherlock lib project

private void bindUploadSpinnerData(){
		try {			
			if(AppVariables.arrUploadAlbums != null){								
				ArrayAdapter<MyJSONObject> adapter = new ArrayAdapter<MyJSONObject>(UploadSetting.this,R.layout.sherlock_spinner_item, AppVariables.arrUploadAlbums){				
					@Override
					public View getView(int position, View convertView,
							ViewGroup parent) {
						View v= super.getView(position, convertView, parent);
						Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF");
						((TextView) v).setTypeface(externalFont);
						((TextView) v).setTextColor(Color.parseColor("#666666"));					
						return v;
					}

					@Override
					public View getDropDownView(final int position, View convertView,
							ViewGroup parent) {						
						View v =  super.getDropDownView(position, convertView, parent);
						Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF");
						((TextView) v).setTypeface(externalFont);
						((TextView) v).setPadding(50, 0, 0, 0);
						((CheckBox)v).setOnCheckedChangeListener(new OnCheckedChangeListener() {							
							@Override
							public void onCheckedChanged(CompoundButton compoundButton, boolean arg1) {							
								if(compoundButton.isChecked()){
									MyJSONObject album = (MyJSONObject)_spnUploadIn.getAdapter().getItem(position);								
									Toast.makeText(getContext(), album.toString(false), Toast.LENGTH_LONG).show();	
								}																
							}
						});						
						return v;
					}
				};
				adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_multicheckitem);
				sortAlbumList(adapter);														
				_spnUploadIn.setAdapter(adapter);	
			}											
		} 
		catch (Exception e) {
			e.toString();
		}
	}

The code for R.layout.sherlock_spinner_dropdown_multicheckitem given below

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?attr/spinnerDropDownItemStyle"    
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?attr/dropdownListPreferredItemHeight"
    android:ellipsize="marquee" />

The above code will result into checkbox present in every row of the spinner

Advertisements
 
Leave a comment

Posted by on February 20, 2013 in ActionBarSherlock

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: