15

I am using the PlaceAutocompleteFragment that is provided by google in a project that I am working on right now. But the problem is I need to display the place name that I've selected from the autocomplete widget but the whole text in not showing because the default textsize in the widget is too big. So, is there any way I can change the textsize in the PlaceAutocompleteFragment without creating my own custom search UI?

My XML code:

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.baldysns.capedbaldy.materialdesigntest.activity.DrawerMainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/toolbar_drawer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay">

                <LinearLayout
                    android:id="@+id/lnr_google_searchbar"
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:background="@drawable/btn_white_notboerder">

                    <fragment
                        android:id="@+id/place_autocompletehome_fragment"
                        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </android.support.v7.widget.Toolbar>
        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.ibaax.com.ibaax.FragmentDrawer"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_drawer"
        tools:layout="@layout/fragment_drawer" /></android.support.v4.widget.DrawerLayout>

And a screenshot of the problem:

enter image description here

Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
Shafayat Mamun
  • 439
  • 1
  • 6
  • 22

6 Answers6

37

Using fragment instance you can change the font size and whatever you want with the Search box EditText.. this is how you need to do

// placeAutocompleteFragment - is my PlaceAutocompleteFragment instance
((EditText)placeAutocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setTextSize(10.0f);

UPDATED: 05/26/20

Looks like in latest SDK view id is changed to R.id.places_autocomplete_search_input

Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • 2
    What if my `placeAutocompleteFragment` is inside `Linear Layout` and i need to set text to `placeAutocompleteFragment` because this is not working in this scenario. Any suggestions. – Jay Rathod Oct 19 '16 at 12:38
  • 1
    It fails..java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.EditText – geeky_monster Jul 03 '17 at 18:14
  • @dontknow may be you are using different id or from different fragment view. please recheck. – Bharatesh Jul 04 '17 at 13:20
  • @skadoosh do you know why setHint() only works on current screen, when the placeAutocompleteFragment pops up, it sets to default hint text again. But when I setText(), the value is not reset. – thanhbinh84 Aug 21 '17 at 09:56
  • @thanhbinh84 no idea why. please post a comment as a Question. – Bharatesh Aug 21 '17 at 12:32
  • I understand now, when clicking on placeAutocompleteFragment, it will pass the text from edittext to the PlaceAutocomplete intent. It seems that we cannot change anything on PlaceAutocomplete intent. – thanhbinh84 Aug 21 '17 at 13:53
  • 1
    it is 'R.id.places_autocomplete_search_input' instead of 'R.id.place_autocomplete_search_input' for newer version of places SDK, there is a 's' – Jovin May 25 '20 at 01:57
7

The autoCompleSupportFragment has an EditText Property. Which can be modified accordingly. The name of the editText is however not very clear. In my case, it was "a"

autoCompleteFragment.a.gravity = Gravity.START

so to modify the text size, i did:

autoCompleteFragment.a.textSize = 14.0f
4
View fView = autocompleteFragment.getView();

Set Text, Hint Color & Size:

EditText etTextInput = fView.findViewById(R.id.places_autocomplete_search_input);
etTextInput.setTextColor(Color.WHITE);
etTextInput.setHintTextColor(Color.WHITE);
etTextInput.setTextSize(12.5f);

Set Search Custom Image:

ImageView ivSearch = fView.findViewById(R.id.places_autocomplete_search_button);
ivSearch.setImageResource(R.drawable.ic_search_white);

Set Search Text Clear Image:

ImageView ivClear = fView.findViewById(R.id.places_autocomplete_clear_button);
ivClear.setImageResource(R.drawable.ic_close_white);

Output:

enter image description here

Ahamadullah Saikat
  • 4,437
  • 42
  • 39
2

Simplest

 ((EditText)findViewById(R.id.place_autocomplete_search_input)).setTextSize(10.0f);

in case of skadosh answer you need to first get instance of fragment.

Muhammad Adil
  • 4,358
  • 3
  • 32
  • 36
1

Assuming your fragment in the Javacode is autocompleteFragment, you can use

autocompleteFragment.a.setTextSize(12.0f);
kimoduor
  • 504
  • 6
  • 16
0

Your screen shot shows that you set margin or padding at search icon and cancel icon. remove it so it make more space for text view.

You can also use autofittextview library(https://github.com/grantland/android-autofittextview) which will make text size according to your text view size.

iTag
  • 1
  • 2
  • But I am using the default placeautocomplete widget that is provided by google. I am asking is there any way to change these things in the widget. – Shafayat Mamun Apr 04 '16 at 10:17