Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main.xml
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent" >
  6.  
  7.     <com.test_autocomplete_text_and_image.CustomAutoCompleteTextView
  8.         android:id="@+id/autocomplete"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:layout_centerHorizontal="true"
  12.         android:textColor="@android:color/black"
  13.         android:hint="autocomplete"
  14.         android:completionThreshold="1"
  15.         />
  16.  
  17.     <TextView
  18.         android:id="@+id/tv_currency"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:layout_centerHorizontal="true"
  22.         android:layout_below="@id/autocomplete"
  23.         />
  24.  
  25. </RelativeLayout>
  26.  
  27. autocomplete_layout.xml
  28.  
  29. <?xml version="1.0" encoding="utf-8"?>
  30. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  31.     android:layout_width="match_parent"
  32.     android:layout_height="match_parent"
  33.     android:orientation="horizontal"    
  34.      >
  35.      
  36.     <ImageView
  37.             android:id="@+id/flag"
  38.             android:layout_width="wrap_content"
  39.             android:layout_height="wrap_content"
  40.             android:contentDescription="@string/hello_world"
  41.             android:padding="10dp"
  42.     />
  43.        
  44.     <TextView
  45.             android:id="@+id/txt"
  46.             android:layout_width="wrap_content"
  47.             android:layout_height="wrap_content"
  48.             android:textSize="15dp"
  49.             android:padding="10dp"         
  50.     />  
  51.        
  52. </LinearLayout>
  53.  
  54.  
  55. import android.content.Context;
  56. import android.util.AttributeSet;
  57. import android.widget.AutoCompleteTextView;
  58.  
  59. import java.util.HashMap;
  60.  
  61. /** Customizing AutoCompleteTextView to return Country Name  
  62.  *  corresponding to the selected item
  63.  */
  64. public class CustomAutoCompleteTextView extends AutoCompleteTextView {
  65.    
  66.     public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
  67.         super(context, attrs);
  68.     }
  69.  
  70.     /** Returns the country name corresponding to the selected item */
  71.     @Override
  72.     protected CharSequence convertSelectionToString(Object selectedItem) {
  73.         /** Each item in the autocompetetextview suggestion list is a hashmap object */
  74.         HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
  75.         return hm.get("txt");
  76.     }
  77. }
  78.  
  79.  
  80.  
  81. import android.content.Context;
  82. import android.util.Log;
  83. import android.view.LayoutInflater;
  84. import android.view.View;
  85. import android.view.ViewGroup;
  86. import android.widget.ArrayAdapter;
  87. import android.widget.ImageView;
  88. import android.widget.TextView;
  89. import java.util.ArrayList;
  90. import java.util.List;
  91.  
  92. public class SearchItemArrayAdapter extends ArrayAdapter<CountryEntry>
  93. {
  94.     private static final String tag = "SearchItemArrayAdapter";
  95.     private CountryEntry listEntry;
  96.     private TextView autoItem;
  97.     private ImageView categoryIcon;
  98.     private List<CountryEntry> countryEntryList = new ArrayList<CountryEntry>();
  99.  
  100.     /**
  101.      *
  102.      * @param context
  103.      * @param textViewResourceId
  104.      * @param objects
  105.      */
  106.     public SearchItemArrayAdapter(Context context, int textViewResourceId, List<CountryEntry> objects)
  107.     {
  108.         super(context, textViewResourceId, objects);
  109.         countryEntryList = objects;
  110.         Log.d(tag, "Search List -> journalEntryList := " + countryEntryList.toString());
  111.     }
  112.  
  113.     @Override
  114.     public int getCount()
  115.     {
  116.         return this.countryEntryList.size();
  117.     }
  118.  
  119.     @Override
  120.     public CountryEntry getItem(int position)
  121.     {
  122.         CountryEntry journalEntry = this.countryEntryList.get(position);
  123.         Log.d(tag, "*-> Retrieving JournalEntry @ position: " + String.valueOf(position) + " : " + journalEntry.toString());
  124.         return journalEntry;
  125.     }
  126.  
  127.     @Override
  128.     public View getView(int position, View convertView, ViewGroup parent)
  129.     {
  130.         View row = convertView;
  131.         LayoutInflater inflater = LayoutInflater.from(getContext());
  132.  
  133.         if (row == null)
  134.         {
  135.             row = inflater.inflate(R.layout.autocomplete_layout, parent, false);
  136.         }
  137.  
  138.         listEntry = this.countryEntryList.get(position);
  139.         String searchItem = listEntry.title;
  140.         autoItem = (TextView) row.findViewById(R.id.txt);
  141.         autoItem.setText(searchItem);
  142.  
  143.         // Get a reference to ImageView holder
  144.         categoryIcon = (ImageView) row.findViewById(R.id.flag);
  145.         categoryIcon.setImageBitmap(listEntry.image);
  146.  
  147.         return row;
  148.     }
  149. }
  150.  
  151.  
  152.  
  153. import android.content.Context;
  154. import android.graphics.Bitmap;
  155. import android.graphics.BitmapFactory;
  156. import android.graphics.drawable.BitmapDrawable;
  157. import android.graphics.drawable.Drawable;
  158. import android.support.v7.app.ActionBarActivity;
  159. import android.os.Bundle;
  160. import android.view.LayoutInflater;
  161. import android.view.Menu;
  162. import android.view.MenuItem;
  163. import android.view.View;
  164. import android.view.ViewGroup;
  165. import android.widget.AutoCompleteTextView;
  166.  
  167. import android.os.Bundle;
  168. import android.app.Activity;
  169. import android.view.Menu;
  170. import android.widget.ArrayAdapter;
  171. import android.widget.AutoCompleteTextView;
  172. import android.widget.ImageView;
  173.  
  174. import java.io.ByteArrayInputStream;
  175. import java.util.ArrayList;
  176.  
  177. public class MainActivity extends ActionBarActivity {
  178.  
  179.  
  180.     AutoCompleteTextView mAutoCompleteTextView;
  181.  
  182.     @Override
  183.     protected void onCreate(Bundle savedInstanceState) {
  184.         super.onCreate(savedInstanceState);
  185.         setContentView(R.layout.activity_main);
  186.  
  187.  
  188.         Bitmap theImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
  189.  
  190.         // Add the country details
  191.  
  192.         // Getting a reference to CustomAutoCompleteTextView of activity_main.xml layout file
  193.         CustomAutoCompleteTextView autoComplete = ( CustomAutoCompleteTextView) findViewById(R.id.autocomplete);
  194.  
  195.         ArrayList<CountryEntry> list = new ArrayList<CountryEntry>();
  196.  
  197.         // Add it to array
  198.         list.add(new CountryEntry("india", theImage));
  199.         list.add(new CountryEntry("usa", theImage));
  200.         SearchItemArrayAdapter adapter = new SearchItemArrayAdapter(this, R.layout.autocomplete_layout, list);
  201.  
  202.         autoComplete.setAdapter(adapter);
  203.  
  204.     }
  205.  
  206. }
  207.  
  208. import android.graphics.Bitmap;
  209. public class CountryEntry {
  210.     public String title;
  211.     public Bitmap image;
  212.  
  213.     public CountryEntry(String title, Bitmap image) {
  214.         this.title = title;
  215.         this.image = image;
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement