0

First I have used default Spinner to show the data in spinner but not able to change the Font style of Spinner .So I have used Custom Spinner to show data and able to change Font style

Here is the Snapshots of Dropdown ScreenShots dropdown arrow is an image

So when I tap the dropdown I want dropdown arrow should hide. Here is the xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:fontFamily="@font/montserrat_regular"
        android:textSize="@dimen/_12sdp"
        android:padding="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_1sdp"
        android:textAlignment="center"
        />
</LinearLayout>

Here is activity xml code

<Spinner
    style="@style/SpinnerTheme"
    android:id="@+id/languageDropdown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:background="@null"
    android:entries="@array/Languages"
    android:spinnerMode="dropdown"
    android:textAlignment="center"
    android:gravity="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/relativeLayout15"

  />

<ImageButton
    android:layout_width="10dp"
    android:layout_height="6dp"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="33dp"
    android:layout_marginTop="29dp"
    android:layout_toEndOf="@+id/languageDropdown"
    android:background="@null"
    android:scaleType="fitCenter"
    android:src="@drawable/dropdownarrow"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.589"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/relativeLayout15" />

private class MyArrayAdapter extends BaseAdapter {

private LayoutInflater mInflater;

public MyArrayAdapter(LoginActivity con) {
    // TODO Auto-generated constructor stub
    mInflater = LayoutInflater.from(con);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return spinnerData.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ListContent holder;
    View v = convertView;
    if (v == null) {
        v = mInflater.inflate(R.layout.my_spinner_style, null);
        holder = new ListContent();

        holder.name = (TextView) v.findViewById(R.id.textView1);

        v.setTag(holder);
    } else {

        holder = (ListContent) v.getTag();
    }

  //  holder.name.setTypeface(myFont);
    holder.name.setText("" + spinnerData[position]);

    return v;
}



}

static class ListContent {

TextView name;

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ext1 Dev
  • 33
  • 2
  • 6
  • 1
    check answer of this link https://stackoverflow.com/questions/16694786/how-to-customize-a-spinner-in-android it will help to u – PushpikaWan Oct 23 '18 at 17:54
  • if u want to quick fix ImageButton btn = (ImageButton)findViewById(R.id....btnid); btn.setImageResource(R.drawable.setanysourceyouwant to display); execute this line when dropdown expand state – PushpikaWan Oct 23 '18 at 18:01

2 Answers2

0

Create a custom spinner view as below which has a listener that you can listen to, and be notified when the spinner is being closed or open.

Reference of customSpinner class: https://stackoverflow.com/a/18636385/8551764

public class CustomSpinner extends Spinner {

   /**
    * An interface which a client of this Spinner could use to receive
    * open/closed events for this Spinner. 
    */
    public interface OnSpinnerEventsListener {

        /**
         * Callback triggered when the spinner was opened.
         */
         void onSpinnerOpened(Spinner spinner);

        /**
         * Callback triggered when the spinner was closed.
         */
         void onSpinnerClosed(Spinner spinner);

    }

    private OnSpinnerEventsListener mListener;
    private boolean mOpenInitiated = false;


    public CustomSpinner(Context context) {
        super(context);
    }

    public CustomSpinner(Context context, int mode) {
        super(context, mode);
    }

    @Override
    public boolean performClick() {
        // register that the Spinner was opened so we have a status
        // indicator for when the container holding this Spinner may lose focus
        mOpenInitiated = true;
        if (mListener != null) {
            mListener.onSpinnerOpened(this);
        }
        return super.performClick();
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        if (hasBeenOpened() && hasFocus) {
            performClosedEvent();
        }
    }

    /**
    * Register the listener which will listen for events.
    */
    public void setSpinnerEventsListener(
            OnSpinnerEventsListener onSpinnerEventsListener) {
        mListener = onSpinnerEventsListener;
    }

    /**
     * Propagate the closed Spinner event to the listener from outside if needed.
     */
    public void performClosedEvent() {
        mOpenInitiated = false;
        if (mListener != null) {
            mListener.onSpinnerClosed(this);
        }
    }

    /**
     * A boolean flag indicating that the Spinner triggered an open event.
     * 
     * @return true for opened Spinner 
     */
    public boolean hasBeenOpened() {
        return mOpenInitiated;
    }

}

Then use its listener as below to hide and display arrow key. In order to hide and display spinner, you should change your custom spinner's background.

CustomSpinner customSpinner = new CustomSpinner(context);
final Drawable drawable = customSpinner.getBackground();

        customSpinner.setSpinnerEventsListener(new CustomSpinner.OnSpinnerEventsListener() {
            @Override
            public void onSpinnerOpened(Spinner spinner) {
                spinner.setBackground(null);
            }

            @Override
            public void onSpinnerClosed(Spinner spinner) {
                spinner.setBackground(drawable);
            }
        });
Mostafa Arian Nejad
  • 1,278
  • 1
  • 19
  • 32
0

use this property in Spinner widget

 android:background="@null"
Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37