0

I am working on an Android project in which I am fetching addresses using Places API and displaying them using AutoCompleteTextView. Right now, it is working properly, but I want to add dot dot at the end of address, so it's not chopped.

I checked other SO links and changed some values in XML, but it is not working. Also, I would like to know what can I do to set the results to fit the width of the screen, the height seems okay.

Here is the XML code :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login_travel_register"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="32dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="32dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/register_travel_transparent_background"
        android:orientation="vertical"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingTop="22dp">
  <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <internetlegion.com.restaurantlunchify.Templates.MaterialDesignIconsTextView
                style="@style/TextViewAppearance.Title1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="0dp"
                android:text="@string/material_icon_account"
                android:textColor="@color/main_color_grey_300"
                android:textSize="22dp" />

            <AutoCompleteTextView
                android:id="@+id/restaurant_address_autocomplete"
                android:layout_width="270dp"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:ellipsize="end"
                android:scrollHorizontally="true"
                android:background="@drawable/edit_text_background_accent"
                android:ems="10"
                android:hint="Address"
                android:lineSpacingExtra="1dp"
                android:textColor="@color/main_color_grey_600"
                android:textCursorDrawable="@drawable/edit_text_cursor_drawable_main">
            </AutoCompleteTextView>

        </LinearLayout>
// Other fields and all
</All layout endings>

Here is how it looks right now :

enter image description here

Kindly let me know what I should do. Thank you.

Edit

The adapter code for AutoComplete :

  autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.search_list_item));
        //autoCompView.setOnItemClickListener(this);

        autoCompView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selection = (String) parent.getItemAtPosition(position);

                for (int i = 0; i < resultList.size(); i++) {
                    if (resultList.get(i).equals(selection)) {
                        String selectedAddress = (String) resultList.get(i);
                        String[] strings = selectedAddress.split(",");
                        if (strings.length > 2) {
                            autoCompView.setText(strings[0]);
                            restRestaurant.setRestaurantAddress(strings[0]);

                            restaurantCityField.setText(strings[1]);
                            restRestaurant.setCity(strings[1]);


                        } else {
                            autoCompView.setText(selectedAddress);
                            restRestaurant.setRestaurantAddress(selectedAddress);
                        }
                        autoCompView.requestFocus();
                        break;
                    }
                }
            }
        });

        autoCompView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autoCompView.requestFocus();
            }
        });



  class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {

        public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

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

        @Override
        public String getItem(int index) {
            return (String) resultList.get(index);
        }

        @Override
        public Filter getFilter() {
            return new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        resultList = autocomplete(constraint.toString());

                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };

        }
    }

search_list_item.xml :

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
We are Borg
  • 5,117
  • 17
  • 102
  • 225

2 Answers2

0

You need to add android:ellipsize="end" in search_list_item.xml

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:layout_height="wrap_content" />
Shoeb Siddique
  • 2,805
  • 1
  • 22
  • 42
0

Please use as the following:

listitem.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

My code:

String[] myData = new String[]{"Africa (AF) Africa (AF) Africa (AF) Africa (AF)", "America (AFM)", "Apple (AMP)"};
AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.first_state);
CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.listitem, myData);

Screenshot:

BNK's screenshot

BNK
  • 23,994
  • 8
  • 77
  • 87
  • I will try this and get back to you. Thanks a lot.. :-) – We are Borg Jan 12 '16 at 15:29
  • Actually, the xml's content in the answer of @ShoebSiddique works in my project too – BNK Jan 12 '16 at 15:35
  • Thank you for the answer, but you are using a CustomArrayAdapter. Can I know it's implementation, you can see the adapter I am using as results are retrieved using Google Places API. – We are Borg Jan 12 '16 at 16:09
  • Please read my answer at http://stackoverflow.com/questions/32926034/autocompletetextview-not-completing-words-inside-parentheses/32928446?s=0|5.2693#32928446 and http://stackoverflow.com/questions/33047156/how-to-create-custom-baseadapter-for-autocompletetextview too – BNK Jan 13 '16 at 00:21
  • That is because of your CustomArrayAdapter, replace that with the Google adapter that I have, and then can you let me know. Thank you,. – We are Borg Jan 13 '16 at 08:00
  • I don't think so, you can try a new sample project with CustomArrayAdapter – BNK Jan 13 '16 at 09:10
  • The same thing was suggested by pskink as well, and it worked, but not with the Google Adapter.. – We are Borg Jan 13 '16 at 09:20
  • Ok, I will try tomorrow if I have free time – BNK Jan 13 '16 at 09:23
  • Sure. Thanks a lot. :-) – We are Borg Jan 13 '16 at 09:24