Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.xml
- <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" >
- <com.test_autocomplete_text_and_image.CustomAutoCompleteTextView
- android:id="@+id/autocomplete"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:textColor="@android:color/black"
- android:hint="autocomplete"
- android:completionThreshold="1"
- />
- <TextView
- android:id="@+id/tv_currency"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_below="@id/autocomplete"
- />
- </RelativeLayout>
- autocomplete_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- >
- <ImageView
- android:id="@+id/flag"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:contentDescription="@string/hello_world"
- android:padding="10dp"
- />
- <TextView
- android:id="@+id/txt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="15dp"
- android:padding="10dp"
- />
- </LinearLayout>
- import android.content.Context;
- import android.util.AttributeSet;
- import android.widget.AutoCompleteTextView;
- import java.util.HashMap;
- /** Customizing AutoCompleteTextView to return Country Name
- * corresponding to the selected item
- */
- public class CustomAutoCompleteTextView extends AutoCompleteTextView {
- public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- /** Returns the country name corresponding to the selected item */
- @Override
- protected CharSequence convertSelectionToString(Object selectedItem) {
- /** Each item in the autocompetetextview suggestion list is a hashmap object */
- HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
- return hm.get("txt");
- }
- }
- import android.content.Context;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- public class SearchItemArrayAdapter extends ArrayAdapter<CountryEntry>
- {
- private static final String tag = "SearchItemArrayAdapter";
- private CountryEntry listEntry;
- private TextView autoItem;
- private ImageView categoryIcon;
- private List<CountryEntry> countryEntryList = new ArrayList<CountryEntry>();
- /**
- *
- * @param context
- * @param textViewResourceId
- * @param objects
- */
- public SearchItemArrayAdapter(Context context, int textViewResourceId, List<CountryEntry> objects)
- {
- super(context, textViewResourceId, objects);
- countryEntryList = objects;
- Log.d(tag, "Search List -> journalEntryList := " + countryEntryList.toString());
- }
- @Override
- public int getCount()
- {
- return this.countryEntryList.size();
- }
- @Override
- public CountryEntry getItem(int position)
- {
- CountryEntry journalEntry = this.countryEntryList.get(position);
- Log.d(tag, "*-> Retrieving JournalEntry @ position: " + String.valueOf(position) + " : " + journalEntry.toString());
- return journalEntry;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent)
- {
- View row = convertView;
- LayoutInflater inflater = LayoutInflater.from(getContext());
- if (row == null)
- {
- row = inflater.inflate(R.layout.autocomplete_layout, parent, false);
- }
- listEntry = this.countryEntryList.get(position);
- String searchItem = listEntry.title;
- autoItem = (TextView) row.findViewById(R.id.txt);
- autoItem.setText(searchItem);
- // Get a reference to ImageView holder
- categoryIcon = (ImageView) row.findViewById(R.id.flag);
- categoryIcon.setImageBitmap(listEntry.image);
- return row;
- }
- }
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AutoCompleteTextView;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- import android.widget.ImageView;
- import java.io.ByteArrayInputStream;
- import java.util.ArrayList;
- public class MainActivity extends ActionBarActivity {
- AutoCompleteTextView mAutoCompleteTextView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Bitmap theImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
- // Add the country details
- // Getting a reference to CustomAutoCompleteTextView of activity_main.xml layout file
- CustomAutoCompleteTextView autoComplete = ( CustomAutoCompleteTextView) findViewById(R.id.autocomplete);
- ArrayList<CountryEntry> list = new ArrayList<CountryEntry>();
- // Add it to array
- list.add(new CountryEntry("india", theImage));
- list.add(new CountryEntry("usa", theImage));
- SearchItemArrayAdapter adapter = new SearchItemArrayAdapter(this, R.layout.autocomplete_layout, list);
- autoComplete.setAdapter(adapter);
- }
- }
- import android.graphics.Bitmap;
- public class CountryEntry {
- public String title;
- public Bitmap image;
- public CountryEntry(String title, Bitmap image) {
- this.title = title;
- this.image = image;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement